layout | title |
---|---|
default |
web.py 0.3 API |
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()
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 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(app.page):
def GET(self):
return "hello, world!"
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)
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 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()
- 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 web.py core. For example, good auth module (port from django?) and OpenID support.