Python Flask - Introduction and Installation
Flask is a microframework for developing web applications using Python Programming Language.
Flask was created in 2010 by Armin Ronacher and is released under BSD license.
Flask is based on Werkzeug and Jinja2. Werkzeug enables Flask listen to and answer requests.
Jinja2 is a templating language and allows us pass data to our HTML user interface and perform basic expressions.
Most web frameworks do implement the MVC(Model View Controller) paradigm. However, as a micro-framework, Flask caters for the
So Flask is leaving out
M(Model) intentionally since it's a micro-framework and aims to lean. The model is normally the data layer and Flask leaves you to choose a wealth of thrid-party extensions for yourself.
Flask wants to be as small and thin as possible so the feature set has to be limited. This makes Flask more unopinionated such that it caters for a wide variety of developers.
For instance with Flask, you can write a simple but fully working and deployable web app in a single file. Name the frameworks you know that can do that?
Fundamental Features Flask provides
As a Framework, Flask provides a set of libaries that provide generic solutions via various abstractions to ease our work as developers.
Some of these include:
- Development Server and Debugger.
- URL Routing.
- Templating through Jinja2.
- Request Handling.
- Unicode Support.
These are just some and already provide us with powerful capabilities for making web applications.
For instance we can handle browser requests, route HTTP requests to controllers, manage sessions, handle Ajax requests and even evaluate Form data etc.
Features Flask leaves out
Flask, we said is a micro-framework so intentionally does not come bundled with all functionalities for instance other alternatives like Django normally include.
These features include:
- Database integration.
- Forms library.
- Administration interface etc.
However, it is important to understand that each of these and other features can be installed via single three-word command.
We said Flask is based on Werkzeug and Jinja2. Let's introduce the two.
What is Werkzeug ?
Werkzeug is a utility library for WSGI. WSGI stands for Web Server Gateway Interface and is a protocol for ensuring that:
- A web application can communicate effectively with web server.
- That web applications can work nicely together.
In short, WSGI is a python interface between web applications and a variety of servers for both development and deployment.
More information can be found here.
What is Jinja2 ?
Jinja2 is a modern templating language for Python and was influenced by Django's templates.
It's fast and secure as it provides an optional sandboxed template execution environment.
More information about jinja2 can be found here.
Flask is a python framework so first ensure that you have Python installed.
Flask requires Python 2.6 or newer.
Virtualenv allows multiple side by side installations of Python one for each project. It's important to note that it doesn't install separate copies of Python but has a way of keeping the projects isolated,
Installing Virtualenv in Mac OS X, Linux, Ubuntu
For Mac OS X or Linux use the following command:
sudo pip install virtualenv
If you are using Ubuntu use this:
sudo apt-get install python-virtualenv
If you are on windows, first install
- First download get-pip.py.
- Then double click it to install.
If you want to upgrade use the following command:
pip install --upgrade pip setuptools
Installing Virtualenv in Windows
Make sure you have
pip as discussed above.
pip install virtualenv
Then once Virtualenv is installed, you can create a project:
Navigate into it:
venv folder into it:
virtualenv venv New python executable in venv/bin/python Installing setuptools, pip............done.
Now to work with your project you can just activate the corresponding environment. On Mac OS X and Linux:
And on windows:
If you want to deactivate it:
Now we can install Flask:
pip install Flask