Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

140 lines (97 sloc) 3.398 kB
layout title
default
web.py 0.3 API

web.py 0.3 API

Hello world

print will be replaced with return.

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()

database

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='')

db.select('todo')
db.select('todo', where='id=$id', vars={'id': 2})
db.query('SELECT * FROM todo')

application

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

web.application

Application to delegate requests based on path.

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

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

web.auto_application

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

app = web.auto_application()

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

web.subdir_application

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

import wiki
import blog
import auth

mapping = (
    "/wiki", wiki.app, 
    "/blog", blog.app,
    "/auth", auth.app)

app = web.subdir_application(mapping)

web.subdomain_application

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

import mainsite
import usersite

mapping = (
    "(www\.)?example.com", mainsite.app,
    ".*\.example.com", usersite.app
)

app = web.subdomain_application(mapping)

testing

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

doctest:

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

class hello:
    """Hello world example.

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

unittest:

import unittest
from helloworld import app

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

if __name__ == "__main__":
    unittest.main()

templates

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

Contrib

  • New module, web.contrib with contributed utilities, which are not part of the web.py core. For example, good auth module (port from django?) and OpenID support.
Jump to Line
Something went wrong with that request. Please try again.