Flask is a free and open-source micro web framework for Python designed to help developers build secure, scalable, and maintainable web applications.
Flask is based on Werkzeug and uses Jinja2 as a template engine.
Also, it is known as a micro-framework because it doesn't require any particular third-party libraries and tools.
However, Flask can support extensions that are implemented in Flask itself to add features into a Flask application.
In this context, we shall look into method to install the Flask framework on your Ubuntu 20.04 machine.
How to install Flask on Ubuntu ?
In order to install Flask on Ubuntu 20.04, let’s update the system first with the following command:
$ sudo apt update
Once the update is complete, go ahead to the next steps.
By default, Ubuntu 20.04 comes with Python 3.8. You can verify this by the following command:
$ python3 -V
Next, you have to install python3-venv package to create a virtual environment for the Flask application:
$ sudo apt install python3-venv
After the package is installed, let's create a virtual environment for the Flask application.
It is recommended to create a new directory for the application and navigate into it:
$ mkdir flask-dir && cd flask-dir
Now, let’s run the following command in flask-dir to create the virtual environment:
$ python3 -m venv venv
The command creates a directory named venv in flask-dir directory.
In order use the virtual environment, you have to activate it as follows:
$ source venv/bin/activate
Once the virtual environment is activated, you can install Flask using the Python package manager pip:
(venv) $ pip install Flask
Verify that the Flask is successfully installed by running the following command:
(venv) $ python -m flask --version
Congratulations, now you can create some Flask applications on your Ubuntu 20.04.
How to create a simple application with Flask on Ubuntu ?
Here, we will create a simple “Hello world” application with Flask.
Using your favorite editor to create a Python file named as: hello.py in flash-dir:
from flask import Flask
app = Flask(__name__)
return 'Hello world'
In your virtual environment, run the following commands:
(venv) $ export FLASK_APP=hello.py
(venv) $ flask run
You can use your web browser or curl command to hit http://127.0.0.1:5000, you will get the "Hello world" text output.