Python uses two common web frameworks, Django and Flask. Django provides users with MVC framework so that they can simplify their web application with lesser, simpler code that has reusable components.
Whereas, Flask is a microframework that allows users to build secure and maintainable web applications. It comes with basic tools and built in python packages to help you get started with developing web applications and add additional functionality to the flask application.
Here, in this article we’ll focus on the installation of Flask on Ubuntu 20.04 as well as we will build a simple application to demonstrate you how to use it. A step by step guide is provided to you so follow it and get error free installation.
Step By Step Guide for Installation
Installation of flask is pretty simple, and I’m assuming that you’ve installed python on your system. Before starting the installation, check the version of python:
$ python3 -V
After this just start following the given steps.
Step 1: Installation of Required Packages
We need to create a virtual environment through and for this we will use venv module. This module is provided by python3-venv package, and we need to install that package:
$ sudo apt install python3-venv
Once installation of the package is completed, we’re all set to create the virtual environment for the Flask application.
Step 2: Create Virtual Environment
Now we need to navigate to the directory we need to store the virtual environment for Python. I’ll navigate to the Desktop directory:
$ cd Desktop
Here create a new directory by running the following command:
$ mkdir flask_app && cd flask_app
This will create a new directory and you’ll be switched to that directory:
Now that you’re inside the directory, run the command to create a virtual environment:
$ python3 -m venv venv
This has created a directory “venv” which will contain a copy of all files and libraries of Python. In order to start the virtual environment:
$ source venv/bin/activate
After activating it, the directory will be added at the beginning of the path and will be shown as (venv).
Step 3: Install Flask
Now, the virtual environment is activated and all we have to do is install flask by using “pip”.
$ pip install Flask
You’ve successfully installed flask. Verify it by checking the version:
$ python -m flask --version
Create an Application with Flask
Here to show you the working, we’ll create a simple application that’ll display “Hello, Welcome to Our Website.” For this Open your Python IDE and create the following file:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, Welcome to Our Website.'
The file should be saved in the “flask_app” directory that we created:
Here, in the first line we imported the Flask class and in the second line we created an instance of the Flask class. route() is used to register the text inside the function to the route / in order to display the message when the function is called.
Now, firstly set the flask_app environment variable:
$ export FLASK_APP=hello.py
Now run the flask application:
$ flask run
This will provide you with the following, you need to copy the link and open it in your browser:
After you’ll copy and run it in your browser, following will be displayed:
In this way the flask will work. Once you’re done, run the following command to go back to your normal directory:
$ deactivate
In this way you have not only installed flask on your Ubuntu 20.04 but also learned to use it.
Conclusion
Flask provides us with an environment to build web applications. It provides us with basic tools and python libraries in order for us to get started with the basic development of web applications. In this how to guide we learned the installation of Ubuntu 20.04 along with its use.
Furthermore, we created a simple hello application using flask on Ubuntu. If you followed the above steps you must have installed it on your systems without any errors. So, get started with it and use it to build various web applications.
TUTORIALS ON LINUX, PROGRAMMING & TECHNOLOGY