In the next article we are going to take a look at Flask. East micro framework Free and open source has been written in Python. It will allow us to create web applications quickly and with a minimum number of lines of code. It has been designed to help developers create secure, scalable and maintainable web applications. Flask is based on Werkzeug and uses Jinja2 as the template engine.
Unlike DjangoBy default, Flask does not include ORM, form validation or any other functionality provided by third party libraries. This microframework has been built with extensions in mind. These are Python packages, with which we can add functionalities to a Flask application.
Depending on what each user needs, there are different methods to install Flask. It can be installed system wide or in a virtual Python environment using pip. In the following lines we are going to see how we can perform the installation in a virtual environment.
The main purpose of Python virtual environments is create an isolated environment for different projects. In this way, you can have multiple different flask environments on a single computer. So we can also install a specific version of a module to a project without having to worry about whether it will affect the other installations we have.
Install Flask on Ubuntu 18.04
I hope the following lines provide enough information to be able to install Flask in a Python virtual environment using Ubuntu 18.04.
Install Python 3 and venv
Ubuntu 18.04 comes with Python 3.6 by default. You can verify the installation by opening a terminal (Ctrl + Alt + T) and typing:
python3 -V
As of Python 3.6, the recommended way to create a virtual environment is to use the venv module. For install the python3-venv package provided by the venv module, in the same terminal you have to execute:
sudo apt install python3-venv
After the installation, we are ready to create the virtual environment.
Creating a virtual environment
We will start by going to the directory where we are interested in storing our Python 3 virtual environments. It can be your main directory or any other directory where the user has read and write permissions.
For this example I am going to create a new directory for the Flask application. Then I will access it:
mkdir mis_flask_app cd mis_flask_app
Once inside the directory, you just have to run the following command to create the new virtual environment:
python3 -m venv venv
The above command creates a directory called venv. It contains a copy of the Python binary, the Pip package manager, the Python standard library, and other support files. Any name can be used for the virtual environment.
To start using this virtual environment, we will have to activate it by running the activation script:
source venv/bin/activate
Once activated, the bin directory of the virtual environment will be added to the beginning of the $ PATH variable. As well it will change your shell command prompt and show the name of the virtual environment you are currently using. In this example, we are going to see something like the following:
Installing Flask
Now that the virtual environment is activated, we can use python package manager to install Flask:
pip install Flask
Within the virtual environment, we can use pip command instead of pip3 and python instead of python3.
It can check the installed version of the microframework using the following command:
python -m flask --version
As you can see in the previous screenshot, at the time of writing this article, the latest official version of Flask is 1.0.2
Creating a minimal application
Now we are going to create the typical application of "Hello World". It will only display one text per screen. To create it we will use our favorite text editor:
vim ~/mis_flask_app/hola.py
Inside the file paste the following lines:
from flask import Flask app = Flask(__name__) @app.route('/') def hola_ubunlog(): return 'Hola Ubunlog'
In the first line we are importing the Flask class. Next, we create an instance of the Flask class. Then we use the route() decorator to register the hello_ functionubunlog() for path /. When this route is requested, hello_ is calledubunlog() and the message 'Hello Ubunlog'is returned to the client.
By the end we save the file like hello.py.
Testing the development server
We will use the flask command to run the application, but before that we need tell Flask how to load the application by specifying the FLASK_APP environment variable:
export FLASK_APP=hola flask run
The above command will start the embedded development server. The output will be similar to the following:
If you open your browser and type http://127.0.0.1:5000 the message of our application will appear, "Hello Ubunlog".
For stop development server, hit Ctrl + C in the terminal.
Disabling the virtual environment
Once we finish the job, we will deactivate the virtual environment to return to our shell normal, typing:
deactivate
If you are new to Flask, visit the page of official documentation by Flask and learn how to develop your applications further.