Toast Driven

← Back to March 6, 2009

itty - A Sinatra-inspired micro-framework

A couple nights ago, I was talking with Matt and Christian. They were in the process of looking at Heroku and Sinatra. Matt lamented that Sinatra was perfect for low-level, web service-y things and how it'd be nice if there were a Python equivalent. Jokingly, they tossed around some fake code samples of what they were thinking.

I've been thinking about this for a long time now and had followed Sinatra since its initial release. WSGI also gets you a long way toward this goal (just as Sinatra uses Rack). I couldn't get it out of my head, so in-between working on work-related bits, I started throwing together itty.

What is itty?

itty is a little Python micro-framework, strongly influenced by Sinatra (though not a port). It's a WSGI app so deployment options are numerous. The intent is to make basic and/or high-performance web apps, especially web services.

It is not an extremely serious project (just a fun little hack for me) but it has worked well so far. It makes no assumptions about what kind of ORM/templating/etc. you want to use, you simply plug it in.

Sample Code

Since code talks, itty_ code looks like this:


from itty import *

@get('/')
def index(request):
    return 'Hello World!'

@get('/hello/(?P\w+)')
def personal_greeting(request, name=', world'):
    return 'Hello %s!' % name

@get('/ct')
def ct(request):
    ct.content_type = 'text/plain'
    return 'Check your Content-Type headers.'

@get('/test_get')
def test_get(request):
    return "'foo' is: %s" % request.GET.get('foo', 'not specified')

@post('/test_post')
def test_post(request):
    return "'foo' is: %s" % request.POST.get('foo', 'not specified')

@get('/test_redirect')
def test_redirect(request):
    raise Redirect('/hello')

run_itty()

You use various decorators (get/post/put/delete/error) to mark functions that can handle web requests. Process how ever you need to within those functions. For output, your function should return a string.

If you're interested, the source is available on GitHub as itty. Enjoy!

Toast Driven