Commits

Anonymous committed 1559f36

add descriptive names to the tutorials

Comments (0)

Files changed (19)

cherrypy/tutorial/01_helloworld.py

+"""
+Tutorial 01 - Hello World
+
+The most basic (working) CherryPy application possible.
+"""
+
+# Import CherryPy global namespace
+from cherrypy import cpg
+
+class HelloWorld:
+    """ Sample request handler class. """
+
+    def index(self):
+        # CherryPy will call this method for the root URI ("/") and send
+        # its return value to the client. Because this is tutorial
+        # lesson number 01, we'll just send something really simple.
+        # How about...
+        return "Hello world!"
+
+    # Expose the index method through the web. CherryPy will never
+    # publish methods that don't have the exposed attribute set to True.
+    index.exposed = True
+
+# CherryPy always starts with cpg.root when trying to map request URIs
+# to objects, so we need to mount a request handler object here. A request
+# to '/' will be mapped to cpg.root.index().
+cpg.root = HelloWorld()
+
+# Start the CherryPy server using the configuration file tutorial.conf.
+cpg.server.start(configFile = 'tutorial.conf')
+

cherrypy/tutorial/02_expose_methods.py

+"""
+Tutorial 02 - Multiple methods
+
+This tutorial shows you how to link to other methods of your request
+handler.
+"""
+
+from cherrypy import cpg
+
+class HelloWorld:
+
+    def index(self):
+        # Let's link to another method here.
+        return 'We have an <a href="showMessage">important message</a> for you!'
+
+    index.exposed = True
+
+
+    def showMessage(self):
+        # Here's the important message!
+        return "Hello world!"
+
+    showMessage.exposed = True
+
+cpg.root = HelloWorld()
+cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/03_get_and_post.py

+"""
+Tutorial 03 - Passing variables
+
+This tutorial shows you how to pass GET/POST variables to methods.
+"""
+
+from cherrypy import cpg
+
+class WelcomePage:
+
+    def index(self):
+        # Ask for the user's name.
+        return '''
+            <form action="greetUser" method="GET">
+            What is your name?
+            <input type="text" name="name" />
+            <input type="submit" />
+            </form>
+        '''
+
+    index.exposed = True
+
+
+    def greetUser(self, name = None):
+        # CherryPy passes all GET and POST variables as method parameters.
+        # It doesn't make a difference where the variables come from, how
+        # large their contents are, and so on.
+        #
+        # You can define default parameter values as usual. In this
+        # example, the "name" parameter defaults to None so we can check
+        # if a name was actually specified.
+
+        if name:
+            # Greet the user!
+            return "Hey %s, what's up?" % name
+        else:
+            # No name was specified
+            return 'Please enter your name <a href="./">here</a>.'
+
+    greetUser.exposed = True
+
+
+cpg.root = WelcomePage()
+cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/04_complex_site.py

+"""
+Tutorial 04 - Multiple objects
+
+This tutorial shows you how to create a site structure through multiple
+possibly nested request handler objects.
+"""
+
+from cherrypy import cpg
+
+class HomePage:
+    def index(self):
+        return '''
+            <p>Hi, this is the home page! Check out the other
+            fun stuff on this site:</p>
+
+            <ul>
+                <li><a href="/joke/">A silly joke</a></li>
+                <li><a href="/links/">Useful links</a></li>
+            </ul>
+        '''
+
+    index.exposed = True
+
+
+class JokePage:
+    def index(self):
+        return '''
+            <p>"In Python, how do you create a string of random
+            characters?" -- "Read a Perl file!"</p>
+            <p>[<a href="../">Return</a>]</p>
+        '''
+
+    index.exposed = True
+
+
+class LinksPage:
+    def __init__(self):
+        # Request handler objects can create their own nested request
+        # handler objects. Simply create them inside their __init__
+        # methods!
+        self.extra = ExtraLinksPage()
+
+    def index(self):
+        # Note the way we link to the extra links page (and back).
+        # As you can see, this object doesn't really care about its
+        # absolute position in the site tree, since we use relative
+        # links exclusively.
+        return '''
+            <p>Here are some useful links:</p>
+
+            <ul>
+                <li><a href="http://www.cherrypy.org">The CherryPy Homepage</a></li>
+                <li><a href="http://www.python.org">The Python Homepage</a></li>
+            </ul>
+
+            <p>You can check out some extra useful
+            links <a href="./extra/">here</a>.</p>
+
+            <p>[<a href="../">Return</a>]</p>
+        '''
+
+    index.exposed = True
+
+
+class ExtraLinksPage:
+    def index(self):
+        # Note the relative link back to the Links page!
+        return '''
+            <p>Here are some extra useful links:</p>
+
+            <ul>
+                <li><a href="http://del.icio.us">del.icio.us</a></li>
+                <li><a href="http://www.mornography.de">Hendrik's weblog</a></li>
+            </ul>
+
+            <p>[<a href="../">Return to links page</a>]</p>
+        '''
+
+    index.exposed = True
+
+
+# Of course we can also mount request handler objects right here!
+cpg.root = HomePage()
+cpg.root.joke = JokePage()
+cpg.root.links = LinksPage()
+
+# Remember, we don't need to mount ExtraLinksPage here, because
+# LinksPage does that itself on initialization. In fact, there is
+# no reason why you shouldn't let your root object take care of
+# creating all contained request handler objects.
+
+cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/05_derived_objects.py

+"""
+Tutorial 05 - Object inheritance
+
+You are free to derive your request handler classes from any base
+class you wish. In most real-world applications, you will probably
+want to create a central base class used for all your pages, which takes
+care of things like printing a common page header and footer.
+"""
+
+from cherrypy import cpg
+
+class Page:
+    # Store the page title in a class attribute
+    title = 'Untitled Page'
+
+    def header(self):
+        return '''
+            <html>
+            <head>
+                <title>%s</title>
+            <head>
+            <body>
+            <h2>%s</h2>
+        ''' % (self.title, self.title)
+
+    def footer(self):
+        return '''
+            </body>
+            </html>
+        '''
+
+    # Note that header and footer don't get their exposed attributes
+    # set to True. This isn't necessary since the user isn't supposed
+    # to call header or footer directly; instead, we'll call them from
+    # within the actually exposed handler methods defined in this
+    # class' subclasses.
+
+
+class HomePage(Page):
+    # Different title for this page
+    title = 'Tutorial 5'
+
+    def __init__(self):
+        # create a subpage
+        self.another = AnotherPage()
+
+    def index(self):
+        # Note that we call the header and footer methods inherited
+        # from the Page class!
+        return self.header() + '''
+            <p>
+            Isn't this exciting? There's
+            <a href="./another/">another page</a>, too!
+            </p>
+        ''' + self.footer()
+
+    index.exposed = True
+
+
+class AnotherPage(Page):
+    title = 'Another Page'
+
+    def index(self):
+        return self.header() + '''
+            <p>
+            And this is the amazing second page!
+            </p>
+        ''' + self.footer()
+
+    index.exposed = True
+
+
+cpg.root = HomePage()
+
+cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/06_aspects.py

+"""
+Tutorial 06 - Aspects
+
+CherryPy2 aspects let you dynamically alter a request handler object's
+behaviour by adding code that gets executed before and/or after
+requested methods. This is useful in situations where you need common
+method behavior across multiple methods or even objects (aspects can
+be part of derived classes).
+"""
+
+from cherrypy import cpg
+
+# We need to import some additional stuff for our aspect code.
+from cherrypy.lib.aspect import Aspect, STOP, CONTINUE
+
+
+class Page(Aspect):
+    title = 'Untitled Page'
+
+    def header(self):
+        # this is the same as in tutorial05.py
+        return '''
+            <html>
+            <head>
+                <title>%s</title>
+            <head>
+            <body>
+            <h2>%s</h2>
+        ''' % (self.title, self.title)
+
+    def footer(self):
+        # this is the same as in tutorial05.py
+        return '''
+            </body>
+            </html>
+        '''
+
+    def _before(self, methodName, method):
+        # The _before aspect method gets executed whenever *any*
+        # other method is called, including header and footer -- which
+        # is something we don't want, so we check the called method
+        # first.
+        if methodName not in ['header', 'footer']:
+            return CONTINUE, self.header()
+        else:
+            return CONTINUE, ''
+
+    def _after(self, methodName, method):
+        # Same as above, except _after gets called after the actually
+        # requested method was executed. Its results are appended to
+        # the output string.
+        if methodName not in ['header', 'footer']:
+            return CONTINUE, self.footer()
+        else:
+            return CONTINUE, ''
+
+
+
+class HomePage(Page):
+    title = 'Tutorial 6 -- Aspect Powered!'
+
+    def __init__(self):
+        self.another = AnotherPage()
+
+    def index(self):
+        # Note that we don't call the header and footer methods
+        # anymore! The aspect methods inherited from the Page class
+        # take care of that now.
+        return '''
+            <p>
+            Isn't this exciting? There's
+            <a href="./another/">another page</a>, too!
+            </p>
+        '''
+
+    index.exposed = True
+
+
+class AnotherPage(Page):
+    title = 'Another Page'
+
+    def index(self):
+        # See above. No header or footer methods called!
+        return '''
+            <p>
+            And this is the amazing second page!
+            </p>
+        '''
+
+    index.exposed = True
+
+
+cpg.root = HomePage()
+
+cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/07_default_method.py

+"""
+Tutorial 07 - The default method
+
+Request handler objects can implement a method called "default" that
+is called when no other suitable method/object could be found.
+Essentially, if CherryPy2 can't find a matching request handler object
+for the given request URI, it will use the default method of the object
+located deepest on the URI path.
+
+Using this mechanism you can easily simulate virtual URI structures
+by parsing the extra URI string, which you can access through
+cpg.request.virtualPath.
+
+The application in this tutorial simulates an URI structure looking
+like /users/<username>. Since the <username> bit will not be found (as
+there are no matching methods), it is handled by the default method.
+"""
+
+from cherrypy import cpg
+
+class UsersPage:
+    def index(self):
+        # Since this is just a stupid little example, we'll simply
+        # display a list of links to random, made-up users. In a real
+        # application, this could be generated from a database result set.
+        return '''
+            <a href="./remi">Remi Delon</a><br/>
+            <a href="./hendrik">Hendrik Mans</a><br/>
+            <a href="./lorenzo">Lorenzo Lamas</a><br/>
+        '''
+
+    index.exposed = True
+
+
+    def default(self, user):
+        # Here we react depending on the virtualPath -- the part of the
+        # path that could not be mapped to an object method. In a real
+        # application, we would probably do some database lookups here
+        # instead of the silly if/elif/else construct.
+        if user == 'remi':
+            out = "Remi Delon, CherryPy lead developer"
+        elif user == 'hendrik':
+            out = "Hendrik Mans, CherryPy co-developer & crazy German"
+        elif user == 'lorenzo':
+            out = "Lorenzo Lamas, famous actor and singer!"
+        else:
+            out = "Unknown user. :-("
+
+        return '%s (<a href="./">back</a>)' % out
+
+    default.exposed = True
+
+
+cpg.root = UsersPage()
+
+cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/08_sessions.py

+"""
+Tutorial 08 - Sessions
+
+Storing session data in CherryPy applications is very easy: cpg.request
+provides a dictionary called sessionMap that represents the session
+data for the current user. If you use RAM based sessions, you can store
+any kind of object into that dictionary; otherwise, you are limited to
+objects that can be pickled.
+"""
+
+from cherrypy import cpg
+
+
+class HitCounter:
+    def index(self):
+        # Increase the silly hit counter
+        count = cpg.request.sessionMap.get('count', 0) + 1
+
+        # Store the new value in the session dictionary
+        cpg.request.sessionMap['count'] = count
+
+        # And display a silly hit count message!
+        return '''
+            During your current session, you've viewed this
+            page %s times! Your life is a patio of fun!
+        ''' % count
+
+    index.exposed = True
+
+
+cpg.root = HitCounter()
+
+cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/09_generators_and_yield.py

+"""
+Bonus Tutorial: Using generators to return result bodies
+
+Instead of returning a complete result string, you can use the yield
+statement to return one result part after another. This may be convenient
+in situations where using a template package like CherryPy or Cheetah
+would be overkill, and messy string concatenation too uncool. ;-)
+"""
+
+from cherrypy import cpg
+from cherrypy.lib.filter import generatorfilter
+
+class GeneratorDemo:
+    _cpFilterList = [generatorfilter.GeneratorFilter()]
+    def header(self):
+        return "<html><body><h2>Generators rule!</h2>"
+    
+    def footer(self):
+        return "</body></html>"
+    
+    def index(self):
+        # Let's make up a list of users for presentation purposes
+        users = ['Remi', 'Carlos', 'Hendrik', 'Lorenzo Lamas']
+
+        # Every yield line adds one part to the total result body.
+        yield self.header()
+        yield "<h3>List of users:</h3>"
+        
+        for user in users:
+            yield "%s<br/>" % user
+            
+        yield self.footer()
+
+    index.exposed = True
+
+cpg.root = GeneratorDemo()
+cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/Readme.txt

 applications using CherryPy. A couple of notes:
 
   - Each of these tutorials builds on the ones before it. If you're
-    new to CherryPy, we recommend you start with tutorial01.py and
+    new to CherryPy, we recommend you start with 01_helloworld.py and
     work your way upwards. :)
 
   - In most of these tutorials, you will notice that all output is done

cherrypy/tutorial/tutorial01.py

-"""
-Tutorial 01 - Hello World
-
-The most basic (working) CherryPy application possible.
-"""
-
-# Import CherryPy global namespace
-from cherrypy import cpg
-
-class HelloWorld:
-    """ Sample request handler class. """
-
-    def index(self):
-        # CherryPy will call this method for the root URI ("/") and send
-        # its return value to the client. Because this is tutorial
-        # lesson number 01, we'll just send something really simple.
-        # How about...
-        return "Hello world!"
-
-    # Expose the index method through the web. CherryPy will never
-    # publish methods that don't have the exposed attribute set to True.
-    index.exposed = True
-
-# CherryPy always starts with cpg.root when trying to map request URIs
-# to objects, so we need to mount a request handler object here. A request
-# to '/' will be mapped to cpg.root.index().
-cpg.root = HelloWorld()
-
-# Start the CherryPy server using the configuration file tutorial.conf.
-cpg.server.start(configFile = 'tutorial.conf')
-

cherrypy/tutorial/tutorial02.py

-"""
-Tutorial 02 - Multiple methods
-
-This tutorial shows you how to link to other methods of your request
-handler.
-"""
-
-from cherrypy import cpg
-
-class HelloWorld:
-
-    def index(self):
-        # Let's link to another method here.
-        return 'We have an <a href="showMessage">important message</a> for you!'
-
-    index.exposed = True
-
-
-    def showMessage(self):
-        # Here's the important message!
-        return "Hello world!"
-
-    showMessage.exposed = True
-
-cpg.root = HelloWorld()
-cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/tutorial03.py

-"""
-Tutorial 03 - Passing variables
-
-This tutorial shows you how to pass GET/POST variables to methods.
-"""
-
-from cherrypy import cpg
-
-class WelcomePage:
-
-    def index(self):
-        # Ask for the user's name.
-        return '''
-            <form action="greetUser" method="GET">
-            What is your name?
-            <input type="text" name="name" />
-            <input type="submit" />
-            </form>
-        '''
-
-    index.exposed = True
-
-
-    def greetUser(self, name = None):
-        # CherryPy passes all GET and POST variables as method parameters.
-        # It doesn't make a difference where the variables come from, how
-        # large their contents are, and so on.
-        #
-        # You can define default parameter values as usual. In this
-        # example, the "name" parameter defaults to None so we can check
-        # if a name was actually specified.
-
-        if name:
-            # Greet the user!
-            return "Hey %s, what's up?" % name
-        else:
-            # No name was specified
-            return 'Please enter your name <a href="./">here</a>.'
-
-    greetUser.exposed = True
-
-
-cpg.root = WelcomePage()
-cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/tutorial04.py

-"""
-Tutorial 04 - Multiple objects
-
-This tutorial shows you how to create a site structure through multiple
-possibly nested request handler objects.
-"""
-
-from cherrypy import cpg
-
-class HomePage:
-    def index(self):
-        return '''
-            <p>Hi, this is the home page! Check out the other
-            fun stuff on this site:</p>
-
-            <ul>
-                <li><a href="/joke/">A silly joke</a></li>
-                <li><a href="/links/">Useful links</a></li>
-            </ul>
-        '''
-
-    index.exposed = True
-
-
-class JokePage:
-    def index(self):
-        return '''
-            <p>"In Python, how do you create a string of random
-            characters?" -- "Read a Perl file!"</p>
-            <p>[<a href="../">Return</a>]</p>
-        '''
-
-    index.exposed = True
-
-
-class LinksPage:
-    def __init__(self):
-        # Request handler objects can create their own nested request
-        # handler objects. Simply create them inside their __init__
-        # methods!
-        self.extra = ExtraLinksPage()
-
-    def index(self):
-        # Note the way we link to the extra links page (and back).
-        # As you can see, this object doesn't really care about its
-        # absolute position in the site tree, since we use relative
-        # links exclusively.
-        return '''
-            <p>Here are some useful links:</p>
-
-            <ul>
-                <li><a href="http://www.cherrypy.org">The CherryPy Homepage</a></li>
-                <li><a href="http://www.python.org">The Python Homepage</a></li>
-            </ul>
-
-            <p>You can check out some extra useful
-            links <a href="./extra/">here</a>.</p>
-
-            <p>[<a href="../">Return</a>]</p>
-        '''
-
-    index.exposed = True
-
-
-class ExtraLinksPage:
-    def index(self):
-        # Note the relative link back to the Links page!
-        return '''
-            <p>Here are some extra useful links:</p>
-
-            <ul>
-                <li><a href="http://del.icio.us">del.icio.us</a></li>
-                <li><a href="http://www.mornography.de">Hendrik's weblog</a></li>
-            </ul>
-
-            <p>[<a href="../">Return to links page</a>]</p>
-        '''
-
-    index.exposed = True
-
-
-# Of course we can also mount request handler objects right here!
-cpg.root = HomePage()
-cpg.root.joke = JokePage()
-cpg.root.links = LinksPage()
-
-# Remember, we don't need to mount ExtraLinksPage here, because
-# LinksPage does that itself on initialization. In fact, there is
-# no reason why you shouldn't let your root object take care of
-# creating all contained request handler objects.
-
-cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/tutorial05.py

-"""
-Tutorial 05 - Object inheritance
-
-You are free to derive your request handler classes from any base
-class you wish. In most real-world applications, you will probably
-want to create a central base class used for all your pages, which takes
-care of things like printing a common page header and footer.
-"""
-
-from cherrypy import cpg
-
-class Page:
-    # Store the page title in a class attribute
-    title = 'Untitled Page'
-
-    def header(self):
-        return '''
-            <html>
-            <head>
-                <title>%s</title>
-            <head>
-            <body>
-            <h2>%s</h2>
-        ''' % (self.title, self.title)
-
-    def footer(self):
-        return '''
-            </body>
-            </html>
-        '''
-
-    # Note that header and footer don't get their exposed attributes
-    # set to True. This isn't necessary since the user isn't supposed
-    # to call header or footer directly; instead, we'll call them from
-    # within the actually exposed handler methods defined in this
-    # class' subclasses.
-
-
-class HomePage(Page):
-    # Different title for this page
-    title = 'Tutorial 5'
-
-    def __init__(self):
-        # create a subpage
-        self.another = AnotherPage()
-
-    def index(self):
-        # Note that we call the header and footer methods inherited
-        # from the Page class!
-        return self.header() + '''
-            <p>
-            Isn't this exciting? There's
-            <a href="./another/">another page</a>, too!
-            </p>
-        ''' + self.footer()
-
-    index.exposed = True
-
-
-class AnotherPage(Page):
-    title = 'Another Page'
-
-    def index(self):
-        return self.header() + '''
-            <p>
-            And this is the amazing second page!
-            </p>
-        ''' + self.footer()
-
-    index.exposed = True
-
-
-cpg.root = HomePage()
-
-cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/tutorial06.py

-"""
-Tutorial 06 - Aspects
-
-CherryPy2 aspects let you dynamically alter a request handler object's
-behaviour by adding code that gets executed before and/or after
-requested methods. This is useful in situations where you need common
-method behavior across multiple methods or even objects (aspects can
-be part of derived classes).
-"""
-
-from cherrypy import cpg
-
-# We need to import some additional stuff for our aspect code.
-from cherrypy.lib.aspect import Aspect, STOP, CONTINUE
-
-
-class Page(Aspect):
-    title = 'Untitled Page'
-
-    def header(self):
-        # this is the same as in tutorial05.py
-        return '''
-            <html>
-            <head>
-                <title>%s</title>
-            <head>
-            <body>
-            <h2>%s</h2>
-        ''' % (self.title, self.title)
-
-    def footer(self):
-        # this is the same as in tutorial05.py
-        return '''
-            </body>
-            </html>
-        '''
-
-    def _before(self, methodName, method):
-        # The _before aspect method gets executed whenever *any*
-        # other method is called, including header and footer -- which
-        # is something we don't want, so we check the called method
-        # first.
-        if methodName not in ['header', 'footer']:
-            return CONTINUE, self.header()
-        else:
-            return CONTINUE, ''
-
-    def _after(self, methodName, method):
-        # Same as above, except _after gets called after the actually
-        # requested method was executed. Its results are appended to
-        # the output string.
-        if methodName not in ['header', 'footer']:
-            return CONTINUE, self.footer()
-        else:
-            return CONTINUE, ''
-
-
-
-class HomePage(Page):
-    title = 'Tutorial 6 -- Aspect Powered!'
-
-    def __init__(self):
-        self.another = AnotherPage()
-
-    def index(self):
-        # Note that we don't call the header and footer methods
-        # anymore! The aspect methods inherited from the Page class
-        # take care of that now.
-        return '''
-            <p>
-            Isn't this exciting? There's
-            <a href="./another/">another page</a>, too!
-            </p>
-        '''
-
-    index.exposed = True
-
-
-class AnotherPage(Page):
-    title = 'Another Page'
-
-    def index(self):
-        # See above. No header or footer methods called!
-        return '''
-            <p>
-            And this is the amazing second page!
-            </p>
-        '''
-
-    index.exposed = True
-
-
-cpg.root = HomePage()
-
-cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/tutorial07.py

-"""
-Tutorial 07 - The default method
-
-Request handler objects can implement a method called "default" that
-is called when no other suitable method/object could be found.
-Essentially, if CherryPy2 can't find a matching request handler object
-for the given request URI, it will use the default method of the object
-located deepest on the URI path.
-
-Using this mechanism you can easily simulate virtual URI structures
-by parsing the extra URI string, which you can access through
-cpg.request.virtualPath.
-
-The application in this tutorial simulates an URI structure looking
-like /users/<username>. Since the <username> bit will not be found (as
-there are no matching methods), it is handled by the default method.
-"""
-
-from cherrypy import cpg
-
-class UsersPage:
-    def index(self):
-        # Since this is just a stupid little example, we'll simply
-        # display a list of links to random, made-up users. In a real
-        # application, this could be generated from a database result set.
-        return '''
-            <a href="./remi">Remi Delon</a><br/>
-            <a href="./hendrik">Hendrik Mans</a><br/>
-            <a href="./lorenzo">Lorenzo Lamas</a><br/>
-        '''
-
-    index.exposed = True
-
-
-    def default(self, user):
-        # Here we react depending on the virtualPath -- the part of the
-        # path that could not be mapped to an object method. In a real
-        # application, we would probably do some database lookups here
-        # instead of the silly if/elif/else construct.
-        if user == 'remi':
-            out = "Remi Delon, CherryPy lead developer"
-        elif user == 'hendrik':
-            out = "Hendrik Mans, CherryPy co-developer & crazy German"
-        elif user == 'lorenzo':
-            out = "Lorenzo Lamas, famous actor and singer!"
-        else:
-            out = "Unknown user. :-("
-
-        return '%s (<a href="./">back</a>)' % out
-
-    default.exposed = True
-
-
-cpg.root = UsersPage()
-
-cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/tutorial08.py

-"""
-Tutorial 08 - Sessions
-
-Storing session data in CherryPy applications is very easy: cpg.request
-provides a dictionary called sessionMap that represents the session
-data for the current user. If you use RAM based sessions, you can store
-any kind of object into that dictionary; otherwise, you are limited to
-objects that can be pickled.
-"""
-
-from cherrypy import cpg
-
-
-class HitCounter:
-    def index(self):
-        # Increase the silly hit counter
-        count = cpg.request.sessionMap.get('count', 0) + 1
-
-        # Store the new value in the session dictionary
-        cpg.request.sessionMap['count'] = count
-
-        # And display a silly hit count message!
-        return '''
-            During your current session, you've viewed this
-            page %s times! Your life is a patio of fun!
-        ''' % count
-
-    index.exposed = True
-
-
-cpg.root = HitCounter()
-
-cpg.server.start(configFile = 'tutorial.conf')

cherrypy/tutorial/tutorial09.py

-"""
-Bonus Tutorial: Using generators to return result bodies
-
-Instead of returning a complete result string, you can use the yield
-statement to return one result part after another. This may be convenient
-in situations where using a template package like CherryPy or Cheetah
-would be overkill, and messy string concatenation too uncool. ;-)
-"""
-
-from cherrypy import cpg
-from cherrypy.lib.filter import generatorfilter
-
-class GeneratorDemo:
-    _cpFilterList = [generatorfilter.GeneratorFilter()]
-    def header(self):
-        return "<html><body><h2>Generators rule!</h2>"
-    
-    def footer(self):
-        return "</body></html>"
-    
-    def index(self):
-        # Let's make up a list of users for presentation purposes
-        users = ['Remi', 'Carlos', 'Hendrik', 'Lorenzo Lamas']
-
-        # Every yield line adds one part to the total result body.
-        yield self.header()
-        yield "<h3>List of users:</h3>"
-        
-        for user in users:
-            yield "%s<br/>" % user
-            
-        yield self.footer()
-
-    index.exposed = True
-
-cpg.root = GeneratorDemo()
-cpg.server.start(configFile = 'tutorial.conf')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.