
In this tutorial you’ll learn how to build a web app with Python. We’ll use a micro-framework called Flask.
Why Flask?
- easy to use.
- built in development server and debugger
- integrated unit testing support
- RESTful request dispatching
- uses Jinja2 templating
- support for secure cookies (client side sessions)
- 100% WSGI 1.0 compliant
- Unicode based
- extensively documented
Related course:
Python Flask: Make Web Apps with Python
$ pip install Flask |
Create a file called hello.py
from flaskimport Flask |
Finally run the web app using this command:
$ python hello.py |
Openhttp://localhost:5000/ in your webbrowser, and “Hello World!” should appear.
Creating URL routes
URL Routing makes URLs in your Web app easy to remember. We will now create some URL routes:
/hello |
Copy the code below and save it as app.py
from flaskimport Flask |
Restart the application using:
$ python hello.py |
Try the URLs in your browser:
- http://127.0.0.1:5000/
- http://127.0.0.1:5000/hello
- http://127.0.0.1:5000/members
- http://127.0.0.1:5000/members/Jordan/

Related course:
Python Flask: Make Web Apps with Python
Style Flask Pages
We will separate code and User Interface using a technique called Templates. We make the directory called /templates/ and create the template:
|
The Python Flask app with have a new URL route. We have changed the default port to 80, the default HTTP port:
from flaskimport Flask, flash, redirect, render_template, request, session, abort |
You can then open :http://127.0.0.1/hello/Jackson/
Styling the template
Do you want some better looking template? We modify the file:
{% extends "layout.html" %} |
We then create layout.html which defines the look of the page. (You may want to split the stylesheet and layout.html file). Copy this as layout.html
|
Restart the App and open the url.http://127.0.0.1/hello/Jackson/
You can pick any name other than Jackson.

Related course:
Python Flask: Make Web Apps with Python
Passing Variables
Lets display random quotes instead of always the same quote. We will need to pass both thename variable and the quote variable. To pass multiple variables to the function, we simply do this:
return render_template( |
Our new test.html template will look like this:
{% extends "layout.html" %} |
We will need to pick a random quote. To do so, we use this code:
quotes = ["'If people do not believe that mathematics is simple, it is only because they do not realize how complicated life is.' -- John Louis von Neumann ", |
The first thing you see is we have defined an array of multiples quotes. These can be accessed as quote[0], quote[1], quote[2] and so on. The functionrandint() returns a random number between 0 and the total number of quotes, one is subtracted because we start counting from zero. Finally we set the quote variable to the quote the computer has chosen. Copy the code below to app.py:
from flaskimport Flask, flash, redirect, render_template, request, session, abort |
When you restart the application it will return one of these quotes at random.

Download App and more Flask Examples
Whats next?
You could link your site with a database system such as MySQL, MariaDb or SQLite. You can find anSQLite tutorial here. Enjoy creating your application!