Removed spurious static_path.
[smonitor.git] / monitor / cherrypy / tutorial / tut08_generators_and_yield.py
bloba6fbdc24d562478c6f0524b5356cf0dfd48abe08
1 """
2 Bonus Tutorial: Using generators to return result bodies
4 Instead of returning a complete result string, you can use the yield
5 statement to return one result part after another. This may be convenient
6 in situations where using a template package like CherryPy or Cheetah
7 would be overkill, and messy string concatenation too uncool. ;-)
8 """
10 import cherrypy
13 class GeneratorDemo:
15 def header(self):
16 return "<html><body><h2>Generators rule!</h2>"
18 def footer(self):
19 return "</body></html>"
21 def index(self):
22 # Let's make up a list of users for presentation purposes
23 users = ['Remi', 'Carlos', 'Hendrik', 'Lorenzo Lamas']
25 # Every yield line adds one part to the total result body.
26 yield self.header()
27 yield "<h3>List of users:</h3>"
29 for user in users:
30 yield "%s<br/>" % user
32 yield self.footer()
33 index.exposed = True
36 import os.path
37 tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')
39 if __name__ == '__main__':
40 # CherryPy always starts with app.root when trying to map request URIs
41 # to objects, so we need to mount a request handler root. A request
42 # to '/' will be mapped to HelloWorld().index().
43 cherrypy.quickstart(GeneratorDemo(), config=tutconf)
44 else:
45 # This branch is for the test suite; you can ignore it.
46 cherrypy.tree.mount(GeneratorDemo(), config=tutconf)