Welcome aboard, Sailor! If you are on this tutorial, it’s probably because you would like to become a captain. In other words, you heard about Flask and how great it is, and you would like to learn everything about this python framework, ohh, sorry, this python micro-framework. Don’t worry, I’m here for that! In few tutorials, I will teach you how to use Flask and I really hope one day, you will create your own crew as a true captain :-).
The first thing you need to know is that Flask is a micro-framework. For a quick definition, a framework is a collection of libraries and modules that allows you to build a web application without writing basic low level functions. That’s great! But why flask is MICRO-framework? Oh, that is because Flask has the minimum of libraries to build a web application. Most of the time, if you want something more, you will need to add it manually because it won’t be in the core of the framework.
Hold on! What do we find in this micro-framework?
The quick answer is : WSGI, Werkzeug and Jinja2. Easy right? Ok, it’s not, let me explain. WSGI (Web Server Gateway Interface) is a python specification to standardize the way your web server will communicate with the web application. Most of the python frameworks use this specification like Django, Falcon, Pyramid and, of course, our star Flask. Werkzeug is a complete WSGI toolkit. It means, it’s a collection of functions that follows the WSGI specification. This layer implements HTTP requests, HTTP responses, HTTP errors and many useful functions to build a web application in Flask. And finally, Jinja2 is a powerful templating engine in python that allows you to render dynamic pages in HTML.
Ok now you know what Flask is, let’s see how to install it. The first thing you need to do is to install python on your computer. Before doing it, let me introduce the virtual environnement. And finally, we will install Flask.
Because, I’m a modern man (Yes I am :-)), let’s install the latest stable python version. So, at the moment I wrote this tutorial, the latest version is 3.7.
Hold on! You should have a question for me. What the hell is a virtual environnement?
A virtual environnement in python is a tool that allows you to isolate your python dependencies throw your different projects. Indeed, if you use python globally in your computer for all of your projects, you could have dependencies issues. Thanks to virtualenv, each project has its own python installation.
Before installing virtualenv, let’s install pip, the package-management system used in python.
For linux users:
$ sudo apt-get update $ sudo apt-get install python-pip $ sudo pip install --upgrade pip
Then we install virtualenv like this:
Sudo apt-get install virtualenv
For mac users:
$ sudo easy_install pip $ sudo pip install --upgrade pip $ sudo pip install virtualenv
Once it’s done, you can create your project by doing:
$ mkdir firstapp $ cd firstapp $ virtualenv -p python3.7 venv
In that way, we specify we want a python virtual environnement in python 3.7. Then we must activate this environnement like this:
$ source venv/bin/activate
Now, you are in the virtual environnement, you can finally install Flask by doing this:
$ pip install Flask
Ok, now Flask is installed in your environnement, let’s create a ‘Hello World’ Example.
‘Hello World’ Example
In this section, we will create our first Flask application. In the ‘firstapp’ directory, create the file ‘app.py’ with the following lines of code:
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!'
Then to execute your code, open your terminal and type:
$ source venv/bin/activate $ export FLASK_APP=app.py $ flask run
If you correctly installed flask in the virtual environnement, you should see this in the terminal:
* Serving Flask app "app.py" * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
And, that’s it, you are just created your first Flask application and it is running on the port 5000. If you click on this link http://127.0.0.1:5000/, a new tab should open with the message ‘Hello, World!’ on it.
Maybe, you would like to understand what happen in theses few lines of code:
- from flask import Flask (we import the Flask module, easy, right?)
- app = Flask(__name__) (we instantiate a Flask object with the python global variable __name__, which is the name of the current module ‘__main__’)
- @app.route(‘/’) (we call the annotation ‘route’ throw the Flask API, this is the route we should call to go inside the function ‘hello_world()’)
- def hello_world(): (we define the function, which must have an unique name)
- return ‘Hello, World!’ (This is the result of the function and the server response)
In this section, we learn how to install and run a Flask web application. In the next section, let’s see Flask Routing that allows you to handle requests.