Sunday, November 17, 2013

Book review: Instant Flask Web Development

By Vasudev Ram



Flask is a Python micro-framework that is fairly popular.
I had first blogged about it a couple of years ago, here:

Flask, new Python microframework

Recently, I had been working on a commercial Flask project for some time, when Packt Publishing asked me if I would review one of their books, Instant Flask Web Development. I did it. The review is below.

Review of book "Instant Flask Web Development", author Ron DuPlain, publisher Packt Publishing:

The book seems to be meant for people who already have some experience with Python.
Some parts of it that cover using Twitter Bootstrap and CSS in Flask templates, will also need knowledge of those topics.

Starts with a simple Hello World Flask app and explains some of the concepts involved.

Some of the topics covered in the book are:
- making a simple Flask app
- mapping URLs to functions (a.k.a. routing)
- HTTP request and response handling
- using databases, static file handling, and form and file uploads
- database CRUD (Create / Read / Update / Delete) operations
- sessions and authentication
- error handling
- deploying Flask apps using nginx and gunicorn

Uses Flask-Script to create command line tools to manage the Flask apps created.

Flask-Script is a Flask extension that provides support for writing external scripts in Flask.

(Flask has an facility for writing extensions that add to the functionality of the base Flask package, and there are multiple such extensions available.)

The book then starts on a scheduling application, which is used as a tutorial to illustrate various Flask features. This app allows the user to Create, Read, Update, Delete (i.e. CRUD) and List appointment records.

It shows how the use the route decorator of the Flask app object, to map various URLs that the app supports, to functions of the app that implement the actions specified by those URLs.

I noticed what seems to be an error in this section; in the middle of the code that maps the URLs to functions, there is this line:
@app.route(...) and def appointment_edit(...).
which is probably an inadvertent copy/paste from some of the text. But it would make the code fail to run, if copied as is from the ebook.

They do specify URLs (on the Packt site) from where you can download the source code for the program examples in book, though.

The use of the Flask url_for() function is described.

There appears to be an error in this section of code:
@app.route('/appointments/<int:appointment_id>/')
def appointment_detail(appointment_id):
    edit_url = url_for('appointment_edit',
    appointment_id=appointment_id)
    # Return the URL string just for demonstration.
    return edit_url
The function is called appointment_detail, but the url_for function is passed an argument 'appointment_edit'. In this case it would work, because it is just returning the URL string for display, not actually doing the appointment detail display.

Then it moves on to talk about how to handle different HTTP methods in Flask, as defined by the HTTP protocol, including GET, POST, etc.

It also mentions that instead of using the Flask route decorator, you can use a method, app.add_url_rule, as an alternative.

The section on using Jinja templates requires knowledge of CSS and JavaScript, and also Twitter Bootstrap and jQuery. The book gets a bit into Jinja features like macros.

Simple, Intermediate and Advanced sections are interspersed through the book (except for the first few sections, which are all Simple).

The book shows how to use some of Flask's error handling techniques, including how to generate a custom error page.

It ends with describing how to deploy a Flask app to production using nginx and gunicorn.

Other posts about Flask on this blog.

Other posts about Python on this blog.

- Vasudev Ram - Consulting and training on Python, Linux, open source




O'Reilly 50% Ebook Deal of the Day


4 comments:

Ben Chapman said...

So, would you recommend the book? Everything you mention seems like it is straight from the regular Flask documentation.

Thank you!

Ben Chapman said...

So, would you recommend the book? Is it worth purchasing?

Many thanks,

Ben

Vasudev Ram said...

Everything? Maybe you should read the post again.

Vasudev Ram said...

Mu