Tuesday, February 14th, 2012

Too many micro webframeworks

There is a large amount of Python micro web frameworks out in the wild, and some of them feel similar to eachother, at least when I first approach them. I plan on going through a few of these micro web frameworks to see what the exact differences are. I will not be comparing them to full frameworks such as Django or Web2py.

Bottle

First up, Bottle. It is very micro in that it comes in a single Py file and is very portable across platforms. If you need a simple Python web app running on say an embedded system with very low resources and disk space, this will be a good option for you. Here is a snippet of code from their home page of an example application:

from bottle import route, run

@route('/hello/:name')
def index(name='World'):
    return '<b>Hello %s!</b>' % name

run(host='localhost', port=8080)

Simple, is it not. One could deploy a Python console application which merely displays output for diagnose of a server or heartbeat in a matter of minutes. I wouldn't recommend it for full web applications, but no one is stopping you.

web.py

Next we have web.py. I have heard many good things about this micro framework, that doesn't make it sounds so micro. A good advantage of this web framework is the ability to separate the requests from both GET and POST. This is great when you need to develop an application which depends on the type of request coming through, such as a REST API. Here is a snippet from their home page:

import web
        
urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:        
    def GET(self, name):
        if not name: 
            name = 'World'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":
    app.run()

A little more complex than Bottle, but it's still simpler than say Django or Web2py.

Flask

Looking for something even simpler than Bottle? Try Flask, there are less imports to remember, but it is ever so similar to Bottle. But, it comes with more than bottle, so you will have more than a single file to work with. It comes with the Jinja2 template engine, RESTful dispatching, and other goodies that other micro frameworks lack. Here's a snippet from their homepage:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

Notice how similar this code is to Bottle's example code? If your looking for a little extra in a micro web framework, without all the extras of Django or Web2py, flask may be a good option for you.

CherryPy

Finally we get to CherryPy. I have heard many great things about this framework and many Python developers seem to favor it. I have not personally tried it myself, but I do plan on trying it out to see how it differs from other frameworks out there, micro and otherwise. They say the framework is a minimalistic one, but I still believe that Bottle is the smallest one around. It supports similar components to Django, such as caching, sessions, static files, and even authorization. It also appears to run on many platforms including Android. Here is the example code snippet from their home page:

import cherrypy
class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

cherrypy.quickstart(HelloWorld())

It does look fairly simple, but the last statement quickstart sort of scares me. This would imply that there are other methods of starting the application, in effect for larger applications, this simple code snippet above may not be the real deal. I will look into this one further and see how complex web applications are built and how it would differ from other micro frameworks listed here.

Final thoughts

Now that this entry is coming to a close, I would like to express my final thoughts on so-called micro frameworks. Are all of these micro frameworks truly needed? Does having this many cause fragmentation in how Python web development should be done? Does it cause developer confusion, on which one a developer should learn or avoid? Let me know what you think about this micro-framework situation us Python developers are faced with in the comments below.

Comment #1: Posted 1 year, 10 months ago by ken

I like ur blog it has good articles coz its hard to find good python blogs around the net. Personally i dont think there is any problem with python having many web frameworks. It will depend on the programmers needs and taste on which to use.It is just like the way PHP has alot of frameworks available.

About Me

My Photo
Names Kevin, hugely into UNIX technologies, not just Linux. I've dabbled with the demons, played with the Sun, and now with the Penguins.




Kevin Veroneau Consulting Services
Do you require the services of a Django contractor? Do you need both a website and hosting services? Perhaps I can help.

If you like what you read, please consider donating to help with hosting costs, and to fund future books to review.

Python Powered | © 2012-2013 Kevin Veroneau