In today’s article we will talk about how to install Flask and run the first program to test its operation. Flask is a micro-framework for the creation of web applications, written in the programming language Python has in its basic installation only the necessary for the construction of a basic project, however, having a wide range of modules ready to fill any need. In the following lines we will see how to install it and we will make a first small application to test its operation.
Create the workbook
Flash installation is very simple and is performed as follows:
First you need to create a folder that will contain the entire project. The folder can be placed in any location of the disk, and will have a name chosen at the discretion of the developer, possibly appropriate to the purpose of the project. Once created, you need to access it in order to start installing an instance of Flash.
We open any terminal and type the commands:
md newProject
cd newProject
For users who use Windows.
mkdir newProject
cd newProject
For Mac and Linux users.
To move in the file system of the various systems, I refer you to other guides that can be simply found on the internet.
Create a virtual environment.
A virtual Python environment is a virtual installation of the Python system of the same or different version from the one installed in the system, isolated and independent from all the rest of the surrounding world, working in real time, which simulates in all respects the functions of the real version. The purpose of virtual environments is to be able to manage packages of different Python versions and avoid conflicts between them. That is, if a developer has to maintain software written with a version of Python different from the one installed on the system, using a virtual environment you can solve the problem without creating any kind of conflict between the various environments. Another purpose of virtual environments is to be able to test modules without dirtying the original version. There are various modules to create virtual environments, in our example we will use the venv module.
To create a virtual environment just enter the folder where it will reside from the terminal and type the following command:
Python -m venv venv
To activate it:
venv\Scripts\activate
For Windows.
source venv/bin/activate
For Mac and Linux.
We now have an environment independent of the rest of the system, and the packages installed on it will only exist within it.
Installazione pacchetti Flask.
Once we have created the workbook and created a new virtual environment we can install Flask. Also from the terminal within the workbook with the virtual environment active, type:
Python -m pip install — upgrade pip
pip install Flask
The first line is for upgrading the pip package installer, the second is for installing the Flask module.
To verify correct installation, type the command:
pip list
All packages installed in that environment will be shown on the screen.
Let’s create the first program.
Let’s test the functioning of the Flask module by creating a first small application that will show us the classic word “Hello World” on the screen. We open our favorite ide which can be pycharm, atom, visual studio code, sublime or whatever you want and we create a first file inside our working folder that we can also call main.py, and inside it we insert the following code block:
from flask import Flask // I create the flask object app = Flask(__name__) // I create the views @app.route("/") def homePage(): return "<h1>Hello World</h1>" // Launch the application without the debug system‹› if __name__ == '__main__': app.run()
Once saved and from the terminal type the following command to launch the program:
python main.py
A series of messages will be shown that will show the status of the program. If there are no errors, open a browser and type the address in the URL bar:
127.0.0.1:5000
Or
Localhost:5000
And here is the final result.
We are now ready to start developing any application.
SviluppoMania
StayTuned