Building a simple web application using Flask
- :Chandan Purohit
- :06 Aug, 2021
- : Web Development
What is Flask?
Flask is a very popular “micro” web development framework built with Python. The framework is most popular especially for small to medium size applications. For me, this framework has been THE framework of choice for most of my python projects.
Flask is the framework for beginners, especially if you do not have solid understanding of how the web works. I personally started with Django, but got stuck as it does many things behind the scenes and one must use ORM almost compulsorily to use django. Flask on the other hand is quite simplistic and the purpose of this article is to show you how to use it to build a simple CRUD app with sqlite database.
What are we building?
We are build a simple application to add and display car information. The whole code is available in github link
Spinning up a basic app
Here is the code to build a simple hello world flask app
|
|
The line app = Flask(__name__)
creates a Flask class instance which is used throughout the code. Url routing is defined through the @app
decorative in line 3. The line says that whenever a http GET request is made to the / url call the function index
. The last line starts the app in debug mode so that the app auto restarts whenver we make changes and save.
Now, if you open a browser and type localhost:5000
and hit enter
key you will see a “hello world” on the screen.
Creating tables
We create another file database.py
to house the entire database related code. Here is the code to create the tables company
and car
. Each car belongs to a company, thus, there is a foreign key relationship
|
|
We take a connection and execute the sql query for creating tables. The database used here is sqlite3 which comes preinstalled with python 3+.
The code for other functions is self explainatory. The main task everytime is
Take database connection
Execute query
Return result / status
Rendering html
Flask requires a folder called templates. You can create html files and store in this folder. Jinja template engine is used by flask to generate dynamic pages on the server side. Here is an example.
|
|
We need to set up a url to render this page. We need to use render_template
function to render the page
|
|
Now, if you open the url localhost:5000/search?name=ram
you will get Hello ram
10 times. You can download and playaround with the code. To add static files (css,js etc) just add another folder called static
and use in the html files.
You may be interested in some of our courses
- 01 Month
- Programming
Python For Data Science
Improve programming skills through live coding solution for programming puzzles and application …
View Details- 01 Month
- Programming
Building cloud ready apps with Golang
Go programming language is amongst the most popular languages these days. The only language with …
View Details- 01 Month
- Programming
C Programming - zero to hero
Gain deep understanding of programming and computer working through this beginner C Programming …
View Details