Joel  Rivera  avatar Joel Rivera committed b907ffb

Improve the examples in the tutorial.

Comments (0)

Files changed (3)

sphinx/source/tutorial/basics.rst

 
     import cherrypy
 
+
     class HelloWorld:
         def index(self):
             return "Hello world!"
    ``index()`` method will be **exposed**. Only exposed methods can be called
    to answer a request. This feature allows the user to select which methods
    of an object will be accessible via the Web; non-exposed methods can't be
-   accessed.
+   accessed. Another way to **expose** a method is to use the decorator
+   :func:`cherrypy.expose`.
  * ``cherrypy.quickstart(HelloWorld())`` mounts an instance of the HelloWorld
    class, and starts the embedded webserver. It runs until explicitly
    interrupted, either with ``Ctrl-C`` or via a suitable signal (a simple
 
 For example, to find out what "host" to which the client intended to connect::
 
-    @cherrypy.expose
-    def index(self):
-        host = cherrypy.request.headers('Host')
-        return "You have successfully reached " + host
+    import cherrypy
+
+
+    class HelloWorld:    
+        @cherrypy.expose
+        def index(self):
+            host = cherrypy.request.headers('Host')
+            return "You have successfully reached " + host
+
+    cherrypy.quickstart(HelloWorld())
 
 Or to set headers on the response::
 
-    @cherrypy.expose
-    def index(self):
-        cherrypy.response.headers['Content-Type'] = 'application/jpeg'
-        return my_jpeg_data()
+    import cherrypy
+
+
+    class HelloWorld:    
+        def _get_jpeg_data(self):
+	    """This method should return the jpeg data"""
+	    return ""
+    
+        @cherrypy.expose
+        def index(self):
+            cherrypy.response.headers['Content-Type'] = 'application/jpeg'
+            return self._get_jpeg_data()
+
+    cherrypy.quickstart(HelloWorld())

sphinx/source/tutorial/dispatching.rst

 In our HelloWorld example, adding the ``http://localhost/onepage/`` mapping
 to ``OnePage().index`` could be done like this::
 
+    import cherrypy
+
+
     class OnePage(object):
         def index(self):
             return "one page!"
         index.exposed = True
 
+
     class HelloWorld(object):
         onepage = OnePage()
 
 CherryPy can directly call methods on the mounted objects, if it receives a
 URL that is directly mapped to them. For example::
 
-    def foo(self):
+
+    """This example can handle the URIs
+    /    -> OnePage.index
+    /foo -> OnePage.foo -> foo
+    """
+    import cherrypy
+
+
+    class OnePage(object):
+        def index(self):
+            return "one page!"
+        index.exposed = True
+
+
+    def foo():
         return 'Foo!'
     foo.exposed = True
 
-    root.foo = foo
+    if __name__ == '__main__':
+        root = OnePage()
+	root.foo = foo
+        cherrypy.quickstart(root)
+
 
 In the example, ``root.foo`` contains a function object, named ``foo``. When
 CherryPy receives a request for the ``/foo`` URL, it will automatically call
 
     class Root:
         def doLogin(self, username=None, password=None):
-            # check the username & password
-            ...
+            """Check the username & password"""
         doLogin.exposed = True
 
 Both arguments have to be declared as *keyword arguments*. The default value
 
     class Root:
         def blog(self, year, month, day):
-            ...
+	    """Deliver the blog post. According to *year* *month* *day*.
+	    """
         blog.exposed = True
 
     root = Root()
 
     class Blog:
         def default(self, year, month, day):
-            ...
+            """This method catch the positional arguments 
+             *year*,*month*,*day* to delivery the blog content.
+            """
         default.exposed = True
 
-    class Root: pass
+
+    class Root:
+        pass
 
     root = Root()
     root.blog = Blog()

sphinx/source/tutorial/exposing.rst

 
     class Root:
         def index(self):
-            ...
+	    """Handle the / URI"""
         index.exposed = True
 
 
 or use a decorator::
 
+    class Root:
         @cherrypy.expose
         def index(self):
-            ...
+	    """Handle the / URI"""
 
 
 When it is a special method, such as ``__call__``, that is to be invoked,
     class Node:
         exposed = True
         def __call__(self):
-            ...
+            """ """
 
+The technics can be mixed, for example::
 
+    """This example can handle the URIs:
+    /       ->  Root.index
+    /page   ->  Root.page
+    /node   ->  Node.__call__
+    """
+    import cherrypy
+
+
+    class Node(object):
+        exposed = True
+    
+        def __call__(self):
+	    return "The node content"
+
+
+    class Root:
+        node = Node()
+
+        @cherrypy.expose       
+        def index(self):
+            return "The index of the root object"
+
+        def page(self):
+            return 'This is the "page" content'
+        page.exposed = True
+    
+
+    if __name__ == '__main__':
+        cherrypy.quickstart(Root())
+       
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.