Flask is a micro web framework written in Python that widely used. Here the Micro doesn’t mean that your entire web application fits into a single python file, nor it’s lacking in functionality compared to other web frameworks.
The intention of saying the micro framework is, it aims the core platform as simple and extendable. It doesn’t come with any heavy inbuilt package abstractions such as database layers and templating frameworks etc. It lets you decide the things you want to use and it supports extensions to add such dependencies into your application.
How to install the Flask framework?
It’s always highly recommended to install Flask on the latest Python at least 3.6 and newer.
Installing Python is different for different operating systems, I have written a separate article for windows installation.
Python on Mac:
On Mac OS you can use the brew command.
% brew install python3
The above command installs the python3 latest version.
Python on Linux:
$ sudo apt-get install python3 python-env
The above Linux command will install the python3 and python virtual environment.
Create Virtual Environment:
After you install the Python, let’s install the Flask in your application. Its always recommended to create a python virtual environment whenever we start a new project.
Create a Virtual environment:
% mkdir flask-hello-world # create application root folder % cd flask-hello-world # get into application folder % python3 -m venv venv # create virtual environment(venv)
The above command
python -m venv venv creates the virtual environment with
venv name. It’s up to you to decide the name of the virtual environment.
Activate Virtual environment:
% source venv/bin/activate # Mac/Linux users > venv\Scripts\activate # Windows users
Soon after activating the virtual environment, you would have noticed that your prompt is being modified with your virtual environment name like below.
(venv) flask-hello-world %
Now we are all set to install the Flask web framework.
Flask can be installed by using python package installer that is
(venv) flask-hello-world % pip install flask Collecting flask Using cached Flask-2.0.1-py3-none-any.whl (94 kB) Requirement already satisfied: Jinja2>=3.0 in ./venv/lib/python3.8/site-packages (from flask) (3.0.1) Requirement already satisfied: itsdangerous>=2.0 in ./venv/lib/python3.8/site-packages (from flask) (2.0.1) Requirement already satisfied: Werkzeug>=2.0 in ./venv/lib/python3.8/site-packages (from flask) (2.0.1) Requirement already satisfied: click>=7.1.2 in ./venv/lib/python3.8/site-packages (from flask) (8.0.1) Requirement already satisfied: MarkupSafe>=2.0 in ./venv/lib/python3.8/site-packages (from Jinja2>=3.0->flask) (2.0.1) Installing collected packages: flask Successfully installed flask-2.0.1
pip install flask command is going to bring Flask from the python package repository along with the number of other packages that Flask depends on.
Dependencies of Flask:
- Werkzeug – implements WSGI framework
- Jinja – A templating language that used to render the web pages on the server
- MarkupSafe – Cones along with jinja
- ItsDangerous – For security integrity
- Click – For writing command-line applications
You can verify the Flask installation by importing the flask into the python interpreter like below.
(venv) flask-hello-world % python Python 3.8.5 (v3.8.5:580fbb018f, Jul 20 2020, 12:11:27) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import flask >>>
import flask will bring the flask packages into the python session, if you did not get any errors while importing the flask that means it successfully installed 🙂
Happy Learning 🙂