26 Apr Python Flask Tutorial: Build Your Flask Application
Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.
In this tutorial, we’ll show you how to write a basic python flask application and a step-by-step process on how to build it.
Requirements
- Python: Python must be installed on your machine, and it’s recommended you use the latest version of Python. Flask supports Python 3.7 and newer.
- IDE: Must have an IDE to work with( VSCODE, Pycharm community IDE e.t.c.)
- Set up a Virtual Environment: You can set up your virtual environment based on the provided OS Specifications.
macOS/Linux
1$ mkdir myproject 2$ cd myproject 3$ python3 -m venv venv
Windows
1> mkdir myproject 2> cd myproject 3> py -3 -m venv venv
- Activate Virtual Environment: Before working on your project, you must activate the corresponding environment
macOS/Linux
$ . venv/bin/activate
Windows
> venv\Scripts\activate
- Install Flask: After you have activated your environment, install flask with pip install flask
Now, we’ll move to develop our web application
Create an Hello World Flask Application
In building a basic Flask app, we’ll create a simple hello world web app.
We will do that by creating an app.py file and writing a function that returns an HTTP-based response.
This Hello World Application shows us how to handle HTTP requests.
Code
The code below was used in creating our app.py file
app.py
1# import our flask library 2from flask import Flask 3# create our app instance 4app = Flask(__name__) 5# we create our app route as /, we can use something else as well probably "/hello" 6@app.route("/") 7# we create our function hello here 8def hello(): 9# we assign the return value which is "hello world" 10 return "Hello World!" 11# this is to automatically run the app 12if __name__ == "__main__": 13 app.run()
Running the Application
We run our application by running ‘flask run’ . By default, flask runs a local server at port 5000.
Web Interface
On clicking http://127.0.0.1:5000/ by pressing the ctrl button down along with a click, it opens a web browser immediately as we can see below.
Creating HTML templates
We can as well use render_templates in flask to beautify our app. This render_template
is used to generate output from a template file based on the Jinja2 engine that is found in the application’s templates folder. This makes managing our HTML codes easier.
Now, we will create a folder called “templates” in the current folder we’re working with. Inside this newly created “templates” folder, all of our HTML files will reside there(index.html). Now let us create a basic HTML template.
Note:
The folder hierarchy will be in this order;
todo |_ venv |_ app.py |_ templates |_____ index.html
templates\index.html
1 2<!DOCTYPE html> 3<html> 4 5<head> 6 <title>My app test</title> 7</head> 8 9<body> 10<h2>Hello World</h2> 11 12<p>You're welcome to my Blog!!!.</p> 13 14</body> 15 16</html
We’ll modify the code we have in our app.py file by importing render_template and also render our response as the Html file we just created.
app.py
Web Interface
We have our app displayed as what we see here, whenever we click on the link http://127.0.0.1:5000/
No Comments