Friday, August 26, 2011


Flask  is awesome.

Why do I think so? Well, read on.

Firstly, what is it? If you didn't click the Flask link at the top of the page despite the pain I took in putting it there, here's what the website says
Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions. And before you ask: It's BSD Licensed!
Whenever you buy anything, you usually consider the brand behind it. For example, if you have to choose between two pairs of shoes priced the same, one being Nike and the other being some unknown brand that has a golden colored "Made in China" sticker, it is obvious which one you would select. Similarly, for Flask, I see that it has been developed by the Pocoo team, whose code is excellent quality and they are also the people behind Sphinx, which, as most of the people in the Python crowd know, is used for the Python Documentation.

Next up, is installation. Not much explaining to do here, the instructions are on the front page! Flask is installed with easy_install, and runs with no configuration (this matters when you've done J2EE in the past *shudders*).

The Flask people know their way around docs. There's a quickstart, which gets you up and running quickly, and shows you enough for you to write a few simple pages. Then, there's a tutorial which builds a blog system from ground up using Flask. Both of these are extremely well written, and are very easy to follow. Also, Flask is dependent on two other Pocoo projects, Jinja2 (templating) and Werkzeug (WSGI), which are both also extremely well documented. As for the API, there are few projects that have everything so well documented. Almost every method call has an example, and all of them are coded very well.

Another thing I like (which Ruby on Rails folks may not), is that Flask doesn't tie you in, it doesn't make you name certain things certain ways, it doesn't assume things about the way you work, and everything is very transparent in the way it works, and this helps you find bugs quickly, even if you are new to the system.

Even though Flask is a microframework, it doesn't make you write all the code on your own. There's proper AJAX support, there's encrypted sessions, etc. etc.

Of course, no programming related article is complete without code, so, lets get to it.

(in case you can't see the embedded Gist,

This is the code on the Flask main page. As you can see, the idea is dead simple. Flask maps requests onto the functions you tell it to, and your functions return HTML (or whatever the heck you want). There isn't a separate routes file.

Okay, so, say you've gotten tired of cramming all the HTML onto a string, you can use this:


Just stuff "index.html" into the "templates/" folder in the same directory. The templating engine is Jinja2, and you can change this, too, if you'd like. Say, you want to parse a form and put some stuff on the session cookie, like you would when someone logs in: (I'm going to omit the stuff, its getting annoying):


Where you use request.form[] to get data from the form (with the key being the name of field), and session[] to set/get things on the session cookie.

And, that's not all. Flask does message flashing, logging, WSGI middlewares, cookies, redirects, per-request objects, etc. all out of the box. And, it doesn't stop there. You can write extensions, or use extensions people have already written, such as the SQLAlchemy extension.

Database access is not part of the framework, which keeps it away from all the junk that involves getting multiple databases to work, but, the SQLAlchemy extension is quite well tested. My favorite database these days is MongoDB (with pyMongo), mostly because of its ease of use and its compatibility with the way Flask works.

Last, but, definitely not the least, is the code quality of Flask. The project is hosted on Github, and has over 1300 watchers. The code is layed out excellently, everything is broken into quite digestible pieces. If you encounter a problem (that for some odd reason you can't find in the docs), a few glances through the code will help you out. The code is very readable, isn't overengineered, and quite small to top it off. If you spend two hours, you could probably understand nearly all of the things that make up Flask, and this gives you something that you don't get with something as large as Django, a complete understanding. You know what happens when you call a blueprint, and you know how globals work, so, when something goes wrong, you know how to fix it, or at least an idea of where the problem is occurring. Another thing is that unlike "one file frameworks" (I'm looking at you, Bottle), that stuff all their dependencies in one file, Flask spreads everything out evenly, so people can actually read the code.

All in all, I really love Flask, and I hope you will too, just give it a try.

In case you're looking for a webhost that supports it, I like Webfaction (I'm not affiliated with them or anything).