Flask is a free and open source micro web framework for Python designed to help developers build secure, scalable and maintainable web applications. It based on the Werkzeug, Jinja, MarkupSafe. It was originally created in 2010 and has since powered some of the largest websites on the internet.
Unlike Django, by default Flask doesn’t include ORM, form validation or any other functionalities provided by third party libraries. Flask is built with extensions in mind, which are Python packages that add functionality to a Flask application.
There are different methods to install Flask, depending on your needs. It can be installed system-wide or in a Python virtual environment using pip.
The main purpose of Python virtual environments is to create an isolated environment for different Python projects. In the way multiple different Flask environments on a single computer and install a specific version of a module on a per project basis without worrying that it will affect your other Flask installations. But If you install Flask into the global environment then you can install only one Flask version on your System.
These distributions will be installed automatically when installing Flask.
- Werkzeug implements WSGI, the standard Python interface between applications and servers.
- Jinja is a template language that renders the pages your application serves.
- MarkupSafe comes with Jinja. It escapes untrusted input when rendering templates to avoid injection attacks.
- ItsDangerous securely signs data to ensure its integrity. This is used to protect Flask’s session cookie.
- Click is a framework for writing command line applications. It provides the
flaskcommand and allows adding custom management commands.
You may like also : What is Jinja
Step 1. Install Python 3 and venv
Ubuntu 18.04 ships with Python 3.6 by default. You can verify that Python 3 is installed on your system by typing:
Create a virtual environment is to use the
venv module. To install the
python3-venv package that provides the
venv module by this command:
sudo apt install python3-venv
Now You can create a virtual environment for our Flask application.
Step 2. Create Virtual Environment
Navigate to the directory where you would like to store your Python 3 virtual environments. It can be any location where your user has read and write permissions.
Create a new directory for your Flask application and navigate into it:
mkdir my_flask_appcd my_flask_app
Create your new virtual environment:
python3 -m venv venv
The command above creates a directory called
venv, which contains a copy of the Python binary, the Pip package manager, the standard Python library and other supporting files. You can use any name you want for the virtual environment.
Now you need to activate it by running the
Once activated, the virtual environment’s bin directory will be added at the beginning of the
$PATH variable. Also your shell’s prompt will change and it will show the name of the virtual environment you’re currently using. In our case that is
Step 3. Install Flask
Now that the virtual environment is activated, you can use the Python package manager pip to install Flask:
pip install Flask
Within virtual environment, you can use the command
pip instead of
python instead of
Verify the installation
python -m Flask --version
How To use Flask
We will create a simple hello world application which will just display the text “Hello World!”.
Open your text editor or PyCharm and create the following file:
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!'
Save the file as
We’ll use the
flask command to run the application but before that we need to tell Flask how to load the application by specifying the
FLASK_APP environment variable:
export FLASK_APP=helloflask run
The command above will launch the development builtin server.
* Serving Flask app "hello" * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
If you use Flask with virtual machine and you want to access Flask development server then you cnn make the server publicly available by appending
--host=0.0.0.0 to the
flask run command.
http://127.0.0.1:5000 in your web browser and you will be presented with the “Hello World!” message.
To stop the development server type
CTRL-C in your terminal.
How To Deactivating the Virtual Environment
After finishing your work, deactivate the environment.
Visit the Flask documentation page and learn how to develop your first Flask app. If you have any question, please leave a comment below.
You may like also: