Friday, October 24, 2014

A Review of An Introduction to Flask

Given that I am new to Flask, I found An Introduction to Flask a great initiation to the various aspects of this python based microframework. The videos, which as almost entirely code based demonstrations,  provide a format where you can stop and start the content to investigate the topic at hand as well as replay the productions to review the content at one’s own pace.

What I do below is to provide a video by video review with each topical area.

Introduction - Welcome and Set Up
As the title of the section states, this is where you are given a basic overview of how to setup your environment for the course. For me, I desired to get the latest stable python install (as of this review) which is python3.4.2 on my Ubuntu 12.04 64bit system. Instead of building from source I grabbed a PPA thinking all would be well. Unfortunately, that did not work correctly and I ended up building the latest from source. Of note was when I realized that I had a problem with the configuration I submitted an issue on the course github site. Within a few hours Miguel responded and worked with me to discover the problem and to correct it. You can see the submission and follow the discussion at That is great customer service!
Hello, Flask!
In A Simple "Hello World" Application, this lesson tests that your setup from the previous video is correct.
The Introduction to Template videos take you through the use of Jinja2 Templates and Flask-Bootstrap as well as the creation of Custom Error Pages and Page Links. For me, this video not only provided a quick and understandable overview, it was the springboard for looking into each one of these technologies.

Web Forms
In Working with Web Forms, the video provides an introduction to HTTP Request Methods GET and POST and their basic uses. A basic form with a textbox and submit button and the posting of the text is discussed.
With the Form Management and Validation with Flask-WTF video, Flask-WTF is installed followed by instruction on its use with basic validation as well as basic protection against CSRF (cross site request forgeries) .
Next, the Rendering Forms with Flask-Bootstrap lesson explains how to take advantage of bootstrap when styling forms.
The final Web Forms video is File Uploads which, as the name implies, is a brief overview of using Flask to validate a filename and format and then to upload that validated file.

Flask In Depth
The Understanding Contexts video detailed the session context variable with an example application that counts number of visits.
Request Dispatching - Flask's URL map is shown and the handling of static files is introduced while covering the URL map for them.
Request Hooks - The before and after request hooks are discussed and demonstrated with an improved application that counts visits across multiple pages.
Responses - Several routes are shown for many different types of responses Flask supports, including HTML, text, XML and JSON. Responses that send cookies to the client are also demonstrated.
In Using Flask-SQLAlchemy, you will see the use of this extension that is a wrapper for the SQLAlchemy database ORM. Here you see an improved application that greets users by name and remembers their names via a SQLite3 database.

User Logins
The Password Security lecture shows the database user model expanded to support password hashing with best practices.
[pip install flask-login]
Next, the Using Flask-Login video shows how the app from the previous video is expanded to support user registration from the console, and login and logout from the web browser.
Scalable Application Structure
With in the Scalable Application Structure video there is a discussion of single-script application limitations. The previous login application is refactored into a multi-file structure that is more appropriate to medium and large applications by an demonstrating an example application structure using the concept of blueprints.
The Writing Unit Tests & Code Coverage lecture provides a demonstration of unit testing best practices in flask. Unit tests for the login application are shown, along with a runner script for them. Use of a code coverage tool is demonstrated as well.
In End-to-end testing with the Flask Test Client, that takes the place of a web browser, the test suite for the login application is expanded with route tests that use the Flask test client.
In the Running in Production Mode video a discussion of debugging being turned off and the need for error notification is presented. Here, the login application is modified to send an email to the administrators when a crash occurs in production mode.
In Deploying to Heroku, a demo is provided that shows the weather application from the templates section being deployed to Heroku.
The Deploying to a Linux Server shows a live demonstration using an Ubuntu virtual machine and a deployment script that sets up nginx and gunicorn as web servers.

In short, I recomend this video series to anyone interested in getting an introductory look at Flask.