|
Revision 567
(checked in by mikerobi, 3 years ago)
|
The second session filte was removed and the tutorials are renumberd. The sessionFitler no longer has any way to support multiple named sessions
|
| Line | |
|---|
| 1 |
""" |
|---|
| 2 |
Bonus Tutorial: Using generators to return result bodies |
|---|
| 3 |
|
|---|
| 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 |
""" |
|---|
| 9 |
|
|---|
| 10 |
import cherrypy |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
class GeneratorDemo: |
|---|
| 14 |
|
|---|
| 15 |
def header(self): |
|---|
| 16 |
return "<html><body><h2>Generators rule!</h2>" |
|---|
| 17 |
|
|---|
| 18 |
def footer(self): |
|---|
| 19 |
return "</body></html>" |
|---|
| 20 |
|
|---|
| 21 |
def index(self): |
|---|
| 22 |
|
|---|
| 23 |
users = ['Remi', 'Carlos', 'Hendrik', 'Lorenzo Lamas'] |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
yield self.header() |
|---|
| 27 |
yield "<h3>List of users:</h3>" |
|---|
| 28 |
|
|---|
| 29 |
for user in users: |
|---|
| 30 |
yield "%s<br/>" % user |
|---|
| 31 |
|
|---|
| 32 |
yield self.footer() |
|---|
| 33 |
index.exposed = True |
|---|
| 34 |
|
|---|
| 35 |
cherrypy.root = GeneratorDemo() |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
if __name__ == '__main__': |
|---|
| 39 |
cherrypy.config.update(file = 'tutorial.conf') |
|---|
| 40 |
cherrypy.server.start() |
|---|
| 41 |
|
|---|