Yesterday 6th June, I participated in a tutorial session on Flask, via google hangout (What happened to the chat option?). Ramaseshan arranged the session; he invited Abhinav a flask user, as the speaker. What is Flask?
Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions. And before you ask: It’s BSD licensed!
It is a lightweight web framework for people who think Django is a way too complicated tool for the job. Most of my code in Deep learning is exclusively written in python (numpy, theano, keras, scipy). I am currently building a Dialogue system based on Seq2Seq architecture. I would like to run it as a chatbot and serve multiple clients. And I don’t want to get too deep into the “web” side of things. Hence, Flask is an ideal tool for me.
Abhinav covered the following topics in yesterday’s session.
- Getting the Hello World Application running
- Serving static files
- Rendering Templates
- Receiving URL encoded parameters
- Basic Authentication System
I would like to add the installation procedure that I had to go through before getting started with hello world.
Setting up Flask in Virtual Environment
We have successfully installed flask in a virtual environment. Now lets jump to the hello world application.
Hello World!
Create a file helloworld.py. Copy paste the following code into it. And run it.
When you load localhost:5001 in your browser, you will see a page with “hello world” in it.
The code is fairly simple; the imports, main function, flask app intitialization and routing. @app.route(“/”) runs the function hello( ) whenever a user loads the root address, that is localhost:5001/.
Serving Static Files
In this section, we will be serving a file app.js. Create a directory static, where all the static files will go. Create app.js inside static/.
Copy paste the alert call into app.js.
Add static_url_path=”/static” parameter to Flask( ) constructor in line 3 of helloworld.py. Run helloworld.py. Access the path http://localhost:5001/static/app.js in your browser. It should display the contents of app.js.
Rendering Templates
When we have the need to render html pages/templates, we can use the function render_template( ). Create a folder templates. Create a html file index.html inside it.
Notice that we have embedded our javascript file app.js inside it.
To render index.html, the function hello() should return render_template(“index.html”).
When you access localhost:5001, you will see the heading “A HTML File from templates”, along with an alert that says “you are feeling app.js!”.
Receiving URL encoded parameters
In order to process requests with parameters encoded in the url, we need to setup the corresponding route and the parameters are provided to a function that handles it.
The snippet above handles the url “/user/
Basic Authentication System
Sometimes we need to authenticate a user before serving him. Follow the code below carefully.
require_auth( ) is a decorator which takes the function secret_page( ) as input and returns a replacement function. To put it simply, we check if the user is already authenticated. If he is, index.html is rendered. If not, we ask for user credentials (username and password). If username and password match ours (check_auth( ) takes care of it), we render index.html, else we show a 401 response (see authenticate( )).
This tutorial covers the bare essentials of Flask. To learn more, go to Discover Flask that contains video tutorials covering pretty much everything in Flask. Also follow this thread for more Flask resources.
Feel free to leave a comment.