Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 87 lines (63 sloc) 2.191 kB
d2e88de anand
anonymous authored
1 ---
2 layout: default
7d17af5 anand
anonymous authored
3 title: Django style multiple apps with web.py
d2e88de anand
anonymous authored
4 ---
5
7d17af5 anand
anonymous authored
6 # Django style multiple apps with web.py
d2e88de anand
anonymous authored
7
7d17af5 anand
anonymous authored
8 Here is a hack to support decoupling urls for multiple applications using web.py
9
10
1a26f09 anand
anonymous authored
11 suppose we have two applications `blog` and `wiki` and we want to put them in same project.
7d17af5 anand
anonymous authored
12
13 This is the main driver.
14
15 """run.py""" import blog
16 import wiki
17 import delegate
18
19 mapping = (
20 ("/blog", blog.urls, blog),
21 ("/wiki", wiki.urls, wiki)
22 )
23
24 if __name__ == "__main__":
25 delegate.run(mapping)
26
27
28 The wiki application:
29
30 """wiki.py"""
31 urls = (
32 "/", "welcome",
33 "/hello", "hello" )
34
35 class welcome:
36 def GET(self):
37 print "welcome wiki"
38 class hello:
39 def GET(self):
40 print "hello wiki"
41 The blog application:
42
43 """blog.py"""
44 urls = (
45 "/", "welcome",
46 "/hello", "hello" )
47
48 class welcome:
49 def GET(self):
50 print "welcome blog"
51 class hello:
52 def GET(self):
53 print "hello blog"
54
55 And this is the hack for supporting multiple application abstration.
56
57
58 """delegate.py""" import web
59 import types
60
61 def delegate_apps(mapping):
62 """Delegates appropriate app based on prefix.
63 `mapping` should be tuple of (prefix, urls, fvars).
64 """ def f():
65 for prefix, urls, fvars in mapping:
66 if type(fvars) == types.ModuleType:
67 fvars = fvars.__dict__
68
69 if web.ctx.path.startswith(prefix):
70 path = web.ctx.path[len(prefix):]
71
72 # it will be better if web.request.handle takes path also as argument.
73 # return web.request.handle(mapping, fvars, path)
74
75 web.ctx.path = path
76 return web.request.handle(urls, fvars)
77 else:
78 return web.notfound()
79
80 return f
81
82 def run(mapping):
83 """Starts web.py server with the specified mapping.
84 `mapping` should be tuple of (prefix, urls, fvars).
85 """ handler = delegate_apps(mapping)
86 web.run(handler, {})
Something went wrong with that request. Please try again.