Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
174 lines (124 sloc) 4.39 KB
layout title



Next release of will be version 0.3. As the number suggests it
will be major release. There will be some changes to API, which
breaks the backward compatability. But I promise, it is not going to
change very much.

Major changes will be

 * return instead of print 
 * moving away from globals 
 * better db api 

Apart from these, the existing subversion repository is be migrated
to bazaar. From now on, the official repository will be 

Please let me know if you have any suggestions, objections or feature
requests. Some of these changes(application stuff and return instead of print)
are already checked in, I request you to play with it and let me know
if there are any bugs.

Here is how the new API is going to look like.

Hello world

print will be replaced with return.

 import web 

 urls = ( 
     '/(.*)', 'hello' 

 # more about this later 
 app = web.application(urls, globals()) 

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

 if __name__ == "__main__": 


db configuration will not be global any more. Multiple databases can
be used at the same time and no web.load() magic required to make
database work.

 import web 

 db = web.database(dbn='postgres', db='todo', user='you', pw='')'todo')'todo', where='id=$id', vars={'id': 2}) 
 db.query('SELECT * FROM todo') 


Application is a new way of mapping urls to classes, coming in 0.3. There will be many different kinds of supported applications.


Application to delegate requests based on path.

 urls = ( 
     "/hello", "hello", 
     "/magic/.*", "magic") 

 app = web.application(urls, globals()) 


Application similar to web.application but urls are constructed
automatiacally using metaclasses.

 app = web.auto_application() 

 class hello( 
     def GET(self): 
         return "hello, world!" 


Application to delegate requests based on subdir. This allows reuse of code easily by taking some existing app and
mounting it at a directory.

 import wiki 
 import blog 
 import auth 

 mapping = ( 

 app = web.subdir_application(mapping) 


Application to delegate requests based on host. This makes virtual hosting very easy.

 import mainsite 
 import usersite 

 mapping = ( 

 app = web.subdomain_application(mapping) 


Testing becomes very easy with applications. Both doctest and
unittest can be used to test web applications.


 urls = ("/hello", "hello") 
 app = web.application(urls, globals()) 

 class hello: 
     """Hello world example. 

         >>> response = app.request("/hello") 
         'hello, world!' 
         >>> response.status 
         '200 OK' 
         >>> response.headers['Content-Type'] 
     def GET(self): 
         web.header('Content-Type', 'text/plain') 
         return "hello, world!" 


 import unittest 
 from helloworld import app 

 class HelloWorldTest(unittest.TestCase): 
     def testHelloWorld(self): 
         response = app.request('GET', '/')
         self.assertEquals(, 'hello, world!')
         self.assertEquals(response.headers['Content-Type'], 'text/plain')
         self.assertEquals(response.status, '200 OK')

 if __name__ == "__main__": 


  • no whitespace magic
  • better error reporting
  • should allow template reuse
  • Probably use Adam Atlas's implementation


  • New module, web.contrib with contributed utilities, which are not
    part of the core. For example, good auth module (port from
    django?) and OpenID support.