Flask
Flask
● Case studies
● First Flask basic application
● Jinja2 Templates
● User interaction
● WSGI (pronounced whiskey) - The Web Server Gateway Interface is a simple calling
convention for web servers to forward requests to web
applications or frameworks written in the Python programming language
● Flask offers suggestions, but doesn't enforce any dependencies or project layout. It is up to
the developer to choose the tools and libraries they want to use. There are many extensions
provided by the community that make adding new functionality easy.
1. https://pypi.org/project/Werkzeug/
2. https://www.palletsprojects.com/p/jinja/
Pr. M. A. BELHAMRA Advanced Python 2022/2023
Werkzeug
● Werkzeug is a comprehensive WSGI web application library. It began as a
simple collection of various utilities for WSGI applications and has become
one of the most advanced WSGI utility libraries.
● Flask wraps Werkzeug, using it to handle the details of WSGI while
providing more structure and patterns for defining powerful applications.
{% extends "layout.html" %}
{% block body %}
<ul>
{% for user in users %}
<li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}
</ul>
{% endblock %}
● Running the application starts the web server (running until you kill it)
if __name__ == '__main__':
app.run()
@app.route('/')
def index():
return "Hello, web world!"
● Must specify
● The (local) URL at which the page will be visible: '/' in the @app.route
decorator
● The name of the page: index
● The (HTML) content of the page: return statement
Pr. M. A. BELHAMRA Advanced Python 2022/2023
Case study 1: First Flask Application
Route: / Route: /slides
</html>
<html>
<head>
<title>SA 2020/2021</title>
</head>
<body>
<h1>Slides</h1>
Route: /slides <ul>
<li>Introduction</li>
<li>What is Software?</li>
</ul>
<p>Back to <a href="/">home</a>.</p>
</body>
</html>
app = Flask(__name__)
@app.route('/')
def index():
return """<html><head><title>SA 2020/2021</title></head><body><h1>Software Applications
2019/2020
</h1><p>This is the web page
of the course</p><p>The slides are available <a href="slides">here</a>.</p></body></html>
"""
@app.route('/slides')
def slides():
return """<html><head><title>SA 2020/2021</title></head><body><h1>Slides</h1><ul>
<li>Introduction</li><li>What is Software?</li></ul><p>Back to <a href="/">home</a>.</
p></body>
</html>
"""
if __name__ == '__main__':
app.run()
● Templating: separating the (fixed) structure of the HTML text (template) from
the variable parts (interpolated variables)
● Flask supports the Jinja2 templating engine
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/slides')
def slides():
return render_template(‘slides.html’)
if __name__ == '__main__':
app.run(debug=True)
<html>
<head>
<title>SA 2020/2021</title>
Route: / </head>
<body>
Located in: templates/index.html <h1>Software Applications 2020/2021</h1>
<p>This is the web page of the course</p>
<p>The slides are available <a href="slides">here</a>.</p>
</body>
</html>
<html>
<head>
<title>SA 2020/2021</title>
</head>
<body>
Route: /slides <h1>Slides</h1>
Located in: templates/slides.html <ul>
<li>Introduction</li>
<li>What is Software?</li>
</ul>
<p>Back to <a href="/">home</a>.</p>
</body>
</html>
http://jinja.pocoo.org/docs/dev/templates/#list-of-control- structures
http://jinja.pocoo.org/docs/dev/templates/#expressions
Logout
Route: /slides
Slides
- Introduction
- What is Software?
Back to home.
Pr. M. A. BELHAMRA Advanced Python 2022/2023
Querying request parameters
● All FORM variable are sent with the HTTP request
user = request.form['user']
myuser = 'Andrea'
Flask return render_template('index.html', user=myuser)
myuser = 'Andrea'
Flask session['user'] = myuser
● You may use a ‘redirect’ method to instruct the browser that the current
response is empty, and it must load the new page (HTTP 302)
return redirect('/index'))
app = Flask(__name__)
app.secret_key = 'softwareapplicationssecret'
@app.route('/')
def index():
return render_template('index2.html')
@app.route('/slides')
def slides():
user = session['user']
topics = ["Introduction", "What is Software?"]
return render_template('slides2.html', user=user, slides=topics)
@app.route('/login', methods=['POST'])
def login():
user = request.form['user']
session['user'] = user
return render_template('welcome.html', user=user)
@app.route('/logout')
def logout():
del session['user']
return redirect('/')
if __name__ == '__main__':
app.run(debug=True)
{% if session.user %}
<p>Welcome {{ session['user'] }}!</p>
<p><img src="static/{{ session['user'] }}.png" /></p>
Route: / <p><a href="logout">Logout</a></p>
Located in: templates/index2.html {% else %}
<p>
<form action="login" method='POST'>
Enter name: <input type='text' name='user'>
<input type='submit' value='Submit'>
</form>
</p>
{% endif %}
</body>
</html>
<html>
<head>
<title>SA 2020/2021</title>
</head>
Route: /login <body>
Located in: templates/welcome.html <h1>Welcome</h1>
<p>Welcome {{ user }}.</p>
<p><a href="/">Continue</a> </p>
</body>
</html>
</ul>
<p>Back to <a href="/">home</a>.</p>
</body>
</html>