Building a local web application
with Flask
Mickaël Mendez - Tech Talk - December 21, 2016
Why would you develop a web
application ?
● Main goal: Summarize and Display information
● Share results with collaborators
● Promote your research
● Blogging
● Why not? Python makes it very easy!
What not to do
● Don’t write html tags within your Python
script. It will become impossible to read as the
project grows
● Don’t write URLs manually. It requires
unnecessary engineering to keep track of the
current location and format the links properly
Some Numbers
● 800 Lines of Code
● 450 Lines of Code
● 1500 Lines of Code (Test)
● 1000 Lines of Code (Test)
● 200 A4 pages of Documentation
● 140 A4 pages of Documentation
2011-2015
Instead use Flask
● Micro-framework for Python web development
● RESTful URL mapping
● Contains development server and debugger
● Flexible MVC design pattern (Jinja2)
● Very good documentation
Hello World
Hello World
Templates
app.py
templates/hello.tpl
static/style.css
Templates
Templates (Jinja2)
For loops
If statements
Variables types
Template inheritance
Child: templates/hello.tpl
Parent: templates/layout.tpl
Plotting
app.py
templates/plot.tpl
Extensions
● Frozen-Flask
● Flask-SQLAlchemy
● Flask-Admin
● Flask-Heroku
● Flask-Babel
● ...
Good links
Flask4scientists:
http://hplgit.github.io/web4sciapps/doc/pub/web4sa_solarized.html
Flask Documentation: http://flask.pocoo.org/docs/0.11/
Jinja2 Documentation: http://jinja.pocoo.org/docs/dev/
Summary
● Flask helps generating a web application
○ Separate Python from HTML
○ Generate URLS
○ Lightweight - It does not store the web pages on disk
● Visualize and share your results
● Very well documented - maybe on of the best documentation
● Few dependencies
● Works with Python 3.3

Building a local web application with Flask