Flask Tutorial 2
Flask Tutorial 2
Flask Tutorial 2
Audience
This tutorial has been prepared for anyone who has a basic knowledge of Python and has
an urge to develop websites. After completing this tutorial, you will find yourself at a
moderate level of expertise in developing websites using Flask.
Prerequisites
Before you start proceeding with this tutorial, we are assuming that you have hands-on
experience on HTML and Python. If you are not well aware of these concepts, then we will
suggest you to go through our short tutorials on HTML and Python.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com
i
Flask
Table of Contents
About the Tutorial ............................................................................................................................................ i
Audience ........................................................................................................................................................... i
Prerequisites ..................................................................................................................................................... i
Copyright & Disclaimer ..................................................................................................................................... i
Table of Contents ............................................................................................................................................ ii
2. Flask – Environment.................................................................................................................................. 2
8. Flask – Templates.................................................................................................................................... 13
ii
Flask
iii
1. FLASK – OVERVIEW Flask
What is Flask?
Flask is a web application framework written in Python. It is developed by Armin Ronacher,
who leads an international group of Python enthusiasts named Pocco. Flask is based on the
Werkzeug WSGI toolkit and Jinja2 template engine. Both are Pocco projects.
WSGI
Web Server Gateway Interface (WSGI) has been adopted as a standard for Python web
application development. WSGI is a specification for a universal interface between the web
server and the web applications.
Werkzeug
It is a WSGI toolkit, which implements requests, response objects, and other utility functions.
This enables building a web framework on top of it. The Flask framework uses Werkzeug as
one of its bases.
Jinga2
Jinga2 is a popular templating engine for Python. A web templating system combines a
template with a certain data source to render dynamic web pages.
Flask is often referred to as a micro framework. It aims to keep the core of an application
simple yet extensible. Flask does not have built-in abstraction layer for database handling,
nor does it have form a validation support. Instead, Flask supports the extensions to add such
functionality to the application. Some of the popular Flask extensions are discussed later in
the tutorial.
4
2. FLASK – ENVIRONMENT Flask
Prerequisite
Python 2.6 or higher is usually required for installation of Flask. Although Flask and its
dependencies work well with Python 3 (Python 3.3 onwards), many Flask extensions do not
support it properly. Hence, it is recommended that Flask should be installed on Python 2.7.
This command needs administrator privileges. Add sudo before pip on Linux/Mac OS. If you
are on Windows, log in as Administrator. On Ubuntu virtualenv may be installed using its
package manager.
mkdir newproj
cd newproj
virtualenv venv
venv/bin/activate
venv\scripts\activate
5
Flask
The above command can be run directly, without virtual environment for system-wide
installation.
6
3. FLASK – APPLICATION Flask
In order to test Flask installation, type the following code in the editor as Hello.py
@app.route('/')
def hello_world():
return 'Hello World’
if __name__ == '__main__':
app.run()
Importing flask module in the project is mandatory. An object of Flask class is our WSGI
application.
The route() function of the Flask class is a decorator, which tells the application which URL
should call the associated function.
app.route(rule, options)
In the above example, ‘/’ URL is bound with hello_world() function. Hence, when the home
page of web server is opened in browser, the output of this function will be rendered.
Finally the run() method of Flask class runs the application on the local development server.
Hostname to listen on. Defaults to 127.0.0.1 (localhost). Set to ‘0.0.0.0’ to have server
host
available externally
port Defaults to 5000
debug Defaults to false. If set to true, provides a debug information
options To be forwarded to underlying Werkzeug server.
7
Flask
Python Hello.py
Open the above URL (localhost:5000) in the browser. ‘Hello World’ message will be
displayed on it.
Debug mode
A Flask application is started by calling the run() method. However, while the application is
under development, it should be restarted manually for each change in the code. To avoid
this inconvenience, enable debug support. The server will then reload itself if the code
changes. It will also provide a useful debugger to track the errors if any, in the application.
The Debug mode is enabled by setting the debug property of the application object to True
before running or passing the debug parameter to the run() method.
app.debug=True
app.run()
app.run(debug=True)
8
4. FLASK – ROUTING Flask
Modern web frameworks use the routing technique to help a user remember application URLs.
It is useful to access the desired page directly without having to navigate from the home
page.
The route() decorator in Flask is used to bind URL to a function. For example:
@app.route(‘/hello’)
def hello_world():
return ‘hello world’
Here, URL ‘/hello’ rule is bound to the hello_world() function. As a result, if a user visits
http://localhost:5000/hello URL, the output of the hello_world() function will be rendered
in the browser.
The add_url_rule() function of an application object is also available to bind a URL with a
function as in the above example, route() is used.
def hello_world():
return ‘hello world’
app.add_url_rule(‘/’, ‘hello’, hello)
9
5. FLASK – VARIABLE RULES Flask
It is possible to build a URL dynamically, by adding variable parts to the rule parameter. This
variable part is marked as <variable-name>. It is passed as a keyword argument to the
function with which the rule is associated.
In the following example, the rule parameter of route() decorator contains <name> variable
part attached to URL ‘/hello’. Hence, if the http://localhost:5000/hello/TutorialsPoint is
entered as a URL in the browser, ‘TutorialPoint’ will be supplied to hello() function as
argument.
Save the above script as hello.py and run it from Python shell. Next, open the browser and
enter URL http://localhost:5000/hello/TutorialsPoint.
Hello TutorialsPoint!
In addition to the default string variable part, rules can be constructed using the following
converters:
10
Flask
app = Flask(__name__)
@app.route('/blog/<int:postID>')
def show_blog(postID):
return 'Blog Number %d' % postID
@app.route('/rev/<float:revNo>')
def revision(revNo):
return 'Revision Number %f' % revNo
if __name__ == '__main__':
app.run()
Run the above code from Python Shell. Visit the URL http://localhost:5000/blog/11 in the
browser.
The given number is used as argument to the show_blog() function. The browser displays
the following output:
Blog Number 11
Enter this URL in the browser: http://localhost:5000/rev/1.1
The revision() function takes up the floating point number as argument. The following result
appears in the browser window:
11
Flask
app.run()
Both the rules appear similar but in the second rule, trailing slash (/) is used. As a result, it
becomes a canonical URL. Hence, using /python or /python/ returns the same output.
However, in case of the first rule, /flask/ URL results in 404 Not Found page.
12
6. FLASK – URL BUILDING Flask
The url_for() function is very useful for dynamically building a URL for a specific function.
The function accepts the name of a function as first argument, and one or more keyword
arguments, each corresponding to the variable part of URL.
The above script has a function user(name) which accepts a value to its argument from the
URL.
The User() function checks if an argument received matches ‘admin’ or not. If it matches,
the application is redirected to the hello_admin() function using url_for(), otherwise to the
hello_guest() function passing the received argument as guest parameter to it.
http://localhost:5000/hello/admin
13
Flask
14