Flask is one of the most popular micro web frameworks for Python. It hinges on WSGI (Web Server Gateway Interface) and Jinja template engine and designed for developing web applications in Python quick and easy. This guide describes how to install Flask on Windows and Linux platforms. You'll also learn how to build your first Flask web app.
Flask supports Python 2 (version 2.7), Python 3 (version 3.5 and later) and PyPy however, Python 3 is recommended. Installing Flask is a 2 step process, assuming that you have Python 3 already installed.
1. Create a virtual environment
Virtual environments keep the Python libraries used in every project separate. This allows you to use different versions of Python libraries in different projects.
To create a virtual environment in Windows, open PowerShell or Command prompt and run the following commands.
PS C:\> mkdir myfirstapp PS C:\> cd myfirstapp PS C:\myfirstapp> py -3 -m venv myenv
What we have done here is first you created a directory named myfirstapp. Then you change directory to myfirstapp folder and launch the latest Python 3.x version. The -m
option instructs Python to run the venv (Virtual Environment) module with myvenv as the path the virtual environment folder. When you run this command, a folder named myvenv is created under myfirstapp folder with the necessary packages, scripts and a config file named pyvenv.cfg
.
Once the virtual environment is created, you can activate it in one of the following ways.
From a Windows PowerShell, run
PS C:\myfirstapp> .\myenv\Scripts\Activate.ps1
or from a Windows Command Prompt, run
C:\myfirstapp> .\myvenv\Scripts\activate.bat
Your Windows command prompt / PowerShell will display the name of the virtual environment as shown in the example below.
(myenv) PS C:\myfirstapp>
Creating a Virtual Environment in Linux
To create and activate a virtual environment in Linux, the commands to run are:
$ mkdir myfirstapp $ cd myfirstapp $ python3 -m venv myenv $ ./myenv/bin/activate
2. Installing Flask
Once you have set up the virtual environment, use Python package installer, pip
, to install Flask.
(myenv) PS C:\myfirstapp> pip install flask
This will install the necessary packages and you will see a message similar to the one below.
Successfully installed Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 flask-1.1.2 itsdangerous-1.1.0
3. Build your first app in Flask
Now that you have installed Flask, let's create a simple "Hello World" app. So basically all this app does is to print show the message "Hello World" in a browser. We'll first create the program and then look into the details of what each line does.
Open any text editor and type in the following code.
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return '<h1>Hello World!!</h1>'
Save this file with the name app.py inside myfirstapp folder. You could give any name for the file, except for flask.py.
- The first line of our code imports
Flask
class from theflask
library. In the second line, we create an instance of the
Flask
class. The__name__
variable is a special Python variable which will either have the value"__main__"
if the program is run directly as a standalone script or it will have the scripts file name as value. So in this case Flask will get the name of the program, which is app.py- Next, we specify a route, which is a URL endpoint for a function.
- And finally, we define the function itself which will be invoked when we access the endpoint. In this simple example, the function returns the string "Hello world".
4. How to run your Flask app
To run the app, first, you need to set the FLASK_APP
environment variable.
To set the environment variable from Windows PowerShell, run
(myenv) PS C:\myfirstapp> $env:FLASK_APP = "app.py"
From a Windows Command Prompt, run the set
command.
C:\myfirstapp> set FLASK_APP=app.py
To set FLASK_APP
environment variable in Linux
$ export FLASK_APP=app.py
You can then run the application using the flask run
in both Windows and Linux.
(myenv) PS C:\myfirstapp> flask run
* Serving Flask app "app.py"
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
App.py __name__ = app
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Now, open a web browser and enter the URL http://127.0.0.1:5000/ and you should see the message "Hello World!!"
To stop the application simply press CTRL+C in the terminal where you started the app.
Enabling Debug Mode
Whenever you make any changes to the app code, you have to stop and start the server. But if you set the environment variable FLASK_ENV
to development and then run the server, it will automatically reload every time you make a change to the app code. This will also enables the debugger and runs the application in debug mode.
> $env:FLASK_ENV = "development" > flask run
On Linux
$ export FLASK_ENV=development $ flask run