Micro web frameworks

If you have ever tried writing a basic web application using the wsgi & cgi modules you’ll know that you still need to do quite a bit of standard code to achieve anything useful. And as is often the case, if you are finding yourself writing standard code time and time again the chances are other people are and have already written a library, or in this case a micro web framework. The aim of a micro web framework is to allow you to do all the common jobs with a minimum of code but not to provide you will bells and whistles, like ORM, that you would find in the bigger web frameworks.

There are quite a few micro web, or non full-stack, frameworks for Python probably because the libraries that come with Python make it easier. The main ones are Bottle, CherryPy, Flask (Python 3 support requires a few dependancy updates), itty (Python 3 has a completely different port) and Fresco. If you want a slightly out-of-date slideshow type review of these see this slideshow battle.

Edit: In the 2 years since I wrote this article, Python 3 support for Flask has been resolved. While the rest of this article still stands, Flask has become the most popular micro web framework partly because its dependency on other modules makes it more powerful and partly because of the extensions that add functionality. If you use bottle to learn, moving to Flask is easy; all the principles are the same.

Probably the best one to get started with is Bottle. Installation is simply pip install bottle. This is just a single file so can be easily copied if your destination system lacks Internet access. It uses function decorators to apply the routing logic which you may not have seen before but is an elegant solution and makes the code very readable.

There is a decent tutorial page on the Bottle homepage but rather than disjointed snippets I have tried to come up a minimal example that actual does something, in this there are two pages, the default page that displays a form asking for a name and the hello page that says hello.

import bottle
boilerplate = '<html><head><title>Welcome to Bottle</title></head><body>{0}</body></html>'

def mainpage():
    return boilerplate.format('''<form action="hello" method="POST"> Hello friend, what is your name?
                              <input type="text" name="name"></form>''')

def greet():
    name = bottle.request.forms.get("name")
    return boilerplate.format('<p>Welcome '+name+'</p>')

bottle.run(host='localhost', port=8082,debug=True)

Saving dictionaries

There is no shortage of ways to save information in Python but if you naturally have the information in a dictionary or an array have a look at a NoSQL database. There are several different types to cover different use cases but a good one to start with is MongoDB. It allows you to save and retrieve arbitrarily complex dictionaries with the minimum of fuss.

One reason to suggest this is they have made learning it easy. They run a free online video based course (M101P) roughly every 2 months with any code outside of Mongo written in Python. I’ve just successfully completed it and I can recommend the course.

If you prefer a book for learning, JSbooks have The Little MongoDB Book you can read. My preference is for the online course but the book is easy to read and useful if you don’t have an Internet connection.