Python Flask - Introduction and Installation

May 6, 2018 Oclemy Python Flask 3 minutes, 19 seconds

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 V(View) and C(Controller).

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:

  1. Development Server and Debugger.
  2. URL Routing.
  3. Templating through Jinja2.
  4. Sessions.
  5. Cookies.
  6. Request Handling.
  7. 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:

  1. Database integration.
  2. Forms library.
  3. 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:

  1. A web application can communicate effectively with web server.
  2. 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.

Installation

Flask is a python framework so first ensure that you have Python installed.

Flask requires Python 2.6 or newer.

Virtualenv

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 easy_install or pip.

Installing pip

  1. First download get-pip.py.
  2. 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 easy_install or pip as discussed above. Then:

pip install virtualenv

Then once Virtualenv is installed, you can create a project:

mkdir myproject

Navigate into it:

cd myproject

Then a 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:

. venv/bin/activate

And on windows:

venv\Scripts\activate

If you want to deactivate it:

deactivate

Installing Flask

Now we can install Flask:

pip install Flask

Best Regards.

Comments