This tutorial is also available in YouTube

Flask is a python web framework. This means flask provides you with tools, libraries and technologies that allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a commercial website.

Flask is part of the categories of the micro-framework. Micro-framework are normally framework with little to no dependencies to external libraries. This has pros and cons. Pros would be that the framework is light, there are little dependency to update and watch for security bugs, cons is that some time you will have to do more work by yourself or increase yourself the list of dependencies by adding plugins. In the case of Flask, its dependencies are:

  • Werkzeug a WSGI utility library. WSGI is basically a protocol defined so that Python application can communicate with a web-server and thus be used as web-application outside of CGI.
  • jinja2 which is its template engine

Installation

Flask depends on some external libraries, like Werkzeug and Jinja2. Werkzeug is a toolkit for WSGI, the standard Python interface between web applications and a variety of servers for both development and deployment. Jinja2 renders templates.

There are many ways you could get started with quickly, but the most kick-ass method is virtualenv, so let’s have a look at that first.

Virtualenv

This is a python package that allows you to create different environment to develop your application. Each environment have a standalone python installation and packages. It doesn’t actually install separate copies of Python, but it does provide a clever way to keep different project environments isolated. Let’s see how virtualenv works.

To install on Mac OS X or Linux:

$ sudo pip install virtualenv

If you are on Windows and don’t have the easy_install command, you must install it first. Check the pip and setuptools on Windows section for more information about how to do that. Once you have it installed, run the same commands as above, but without the sudo prefix.

Once you have virtualenv installed, just fire up a shell and create your own environment. I usually create a project folder and a venv folder within:

$ mkdir myproject 
$ cd myproject
$ virtualenv venv
New python executable in venv/bin/python Installing setuptools, pip............done.

Now, whenever you want to work on a project, you only have to activate the corresponding environment. On OS X and Linux, do the following:

$ source venv/bin/activate 

If you are a Windows user, the following command is for you:

$ venv\Scripts\activate

Either way, you should now be using your virtualenv (notice how the prompt of your shell has changed to show the active environment).

And if you want to go back to the real world, use the following command:

$ deactivate

After doing this, the prompt of your shell should be as familiar as before.

Now, let’s move on. Enter the following command to get Flask activated in your virtualenv:

$ pip install Flask

A few seconds later and you are good to go.

A Minimal Application

We will create a file run.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

So what did that code do?

  1. First we imported the Flask class. An instance of this class will be our WSGI application.
  2. Next we create an instance of this class. The first argument is the name of the application’s module or package. If you are using a single module (as in this example), you should use __name__ because depending on if it’s started as application or imported as module the name will be different ('__main__' versus the actual import name). This is needed so that Flask knows where to look for templates, static files, and so on. For more information have a look at the Flask documentation.
  3. We then use the route() decorator to tell Flask what URL should trigger our function.
  4. The function is given a name which is also used to generate URLs for that particular function, and returns the message we want to display in the user’s browser.

To run the application you can either use the flask command or python’s -m switch with Flask. Before you can do that you need to tell your terminal the application to work with by exporting the FLASK_APP environment variable:

$ export FLASK_APP=run.py 
$ flask run
* Running on http://127.0.0.1:5000/

If you are on Windows you need to use set instead of export.

Alternatively you can use python -m flask:

$ export FLASK_APP=hello.py 
$ python -m flask run
* Running on http://127.0.0.1:5000/

This launches a very simple builtin server, which is good enough for testing but probably not what you want to use in production. For deployment options see Deployment Options.

Now head over to http://127.0.0.1:5000/, and you should see your hello world greeting.

Debug Mode

The flask script is nice to start a local development server, but you would have to restart it manually after each change to your code. That is not very nice and Flask can do better. If you enable debug support the server will reload itself on code changes, and it will also provide you with a helpful debugger if things go wrong.

To enable debug mode you can export the FLASK_DEBUG environment variable before running the server:

$ export FLASK_DEBUG=1 
$ flask run

(On Windows you need to use set instead of export).

This does the following things:

  1. it activates the debugger
  2. it activates the automatic reloader
  3. it enables the debug mode on the Flask application.

3 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here