diff options
| author | Jason R. Coombs <jaraco@jaraco.com> | 2017-12-17 12:34:32 -0500 |
|---|---|---|
| committer | Jason R. Coombs <jaraco@jaraco.com> | 2017-12-17 12:38:41 -0500 |
| commit | 661dc06ffa9a872824d628975657b21325654f2e (patch) | |
| tree | 36027d71593578354faf50c5a227cf637388a4ff /cherrypy/tutorial | |
| parent | 49bc467811b74e100a6f5e007409e9d9baaa5f6a (diff) | |
| parent | 0e4542295a33fd776f2ca5f1bb151eb8ddf919a0 (diff) | |
| download | cherrypy-git-archive/1285-always-bytes.tar.gz | |
Merge branch 'master' into bb-pr-85archive/1285-always-bytes
Diffstat (limited to 'cherrypy/tutorial')
| -rw-r--r-- | cherrypy/tutorial/README.rst | 16 | ||||
| -rw-r--r-- | cherrypy/tutorial/README.txt | 16 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut01_helloworld.py | 15 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut02_expose_methods.py | 13 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut03_get_and_post.py | 10 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut04_complex_site.py | 14 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut05_derived_objects.py | 10 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut06_default_method.py | 18 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut07_sessions.py | 8 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut08_generators_and_yield.py | 16 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut09_files.py | 22 | ||||
| -rw-r--r-- | cherrypy/tutorial/tut10_http_errors.py | 24 |
12 files changed, 81 insertions, 101 deletions
diff --git a/cherrypy/tutorial/README.rst b/cherrypy/tutorial/README.rst new file mode 100644 index 00000000..3c0791bb --- /dev/null +++ b/cherrypy/tutorial/README.rst @@ -0,0 +1,16 @@ +CherryPy Tutorials +------------------------------------------------------------------------ + +This is a series of tutorials explaining how to develop dynamic web +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 01_helloworld.py and + work your way upwards. :) + +- In most of these tutorials, you will notice that all output is done + by returning normal Python strings, often using simple Python + variable substitution. In most real-world applications, you will + probably want to use a separate template package (like Cheetah, + CherryTemplate or XML/XSL). diff --git a/cherrypy/tutorial/README.txt b/cherrypy/tutorial/README.txt deleted file mode 100644 index 2b877e1f..00000000 --- a/cherrypy/tutorial/README.txt +++ /dev/null @@ -1,16 +0,0 @@ -CherryPy Tutorials
-------------------------------------------------------------------------
-
-This is a series of tutorials explaining how to develop dynamic web
-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 01_helloworld.py and
- work your way upwards. :)
-
- - In most of these tutorials, you will notice that all output is done
- by returning normal Python strings, often using simple Python
- variable substitution. In most real-world applications, you will
- probably want to use a separate template package (like Cheetah,
- CherryTemplate or XML/XSL).
-
diff --git a/cherrypy/tutorial/tut01_helloworld.py b/cherrypy/tutorial/tut01_helloworld.py index 2fa61afa..e86793c8 100644 --- a/cherrypy/tutorial/tut01_helloworld.py +++ b/cherrypy/tutorial/tut01_helloworld.py @@ -4,6 +4,8 @@ Tutorial - Hello World The most basic (working) CherryPy application possible. """ +import os.path + # Import CherryPy global namespace import cherrypy @@ -12,19 +14,17 @@ class HelloWorld: """ Sample request handler class. """ + # Expose the index method through the web. CherryPy will never + # publish methods that don't have the exposed attribute set to True. + @cherrypy.expose 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!" + 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 - -import os.path tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') if __name__ == '__main__': @@ -32,6 +32,3 @@ if __name__ == '__main__': # to objects, so we need to mount a request handler root. A request # to '/' will be mapped to HelloWorld().index(). cherrypy.quickstart(HelloWorld(), config=tutconf) -else: - # This branch is for the test suite; you can ignore it. - cherrypy.tree.mount(HelloWorld(), config=tutconf) diff --git a/cherrypy/tutorial/tut02_expose_methods.py b/cherrypy/tutorial/tut02_expose_methods.py index 64a10f26..8afbf7d8 100644 --- a/cherrypy/tutorial/tut02_expose_methods.py +++ b/cherrypy/tutorial/tut02_expose_methods.py @@ -5,22 +5,24 @@ This tutorial shows you how to link to other methods of your request handler. """ +import os.path + import cherrypy class HelloWorld: + @cherrypy.expose def index(self): # Let's link to another method here. return 'We have an <a href="show_msg">important message</a> for you!' - index.exposed = True + @cherrypy.expose def show_msg(self): # Here's the important message! - return "Hello world!" - show_msg.exposed = True + return 'Hello world!' + -import os.path tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') if __name__ == '__main__': @@ -28,6 +30,3 @@ if __name__ == '__main__': # to objects, so we need to mount a request handler root. A request # to '/' will be mapped to HelloWorld().index(). cherrypy.quickstart(HelloWorld(), config=tutconf) -else: - # This branch is for the test suite; you can ignore it. - cherrypy.tree.mount(HelloWorld(), config=tutconf) diff --git a/cherrypy/tutorial/tut03_get_and_post.py b/cherrypy/tutorial/tut03_get_and_post.py index a487a909..0b3d4613 100644 --- a/cherrypy/tutorial/tut03_get_and_post.py +++ b/cherrypy/tutorial/tut03_get_and_post.py @@ -4,11 +4,14 @@ Tutorial - Passing variables This tutorial shows you how to pass GET/POST variables to methods. """ +import os.path + import cherrypy class WelcomePage: + @cherrypy.expose def index(self): # Ask for the user's name. return ''' @@ -17,8 +20,8 @@ class WelcomePage: <input type="text" name="name" /> <input type="submit" /> </form>''' - index.exposed = True + @cherrypy.expose 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 @@ -37,10 +40,8 @@ class WelcomePage: return 'Please enter your name <a href="./">here</a>.' else: return 'No, really, enter your name <a href="./">here</a>.' - greetUser.exposed = True -import os.path tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') if __name__ == '__main__': @@ -48,6 +49,3 @@ if __name__ == '__main__': # to objects, so we need to mount a request handler root. A request # to '/' will be mapped to HelloWorld().index(). cherrypy.quickstart(WelcomePage(), config=tutconf) -else: - # This branch is for the test suite; you can ignore it. - cherrypy.tree.mount(WelcomePage(), config=tutconf) diff --git a/cherrypy/tutorial/tut04_complex_site.py b/cherrypy/tutorial/tut04_complex_site.py index e0660024..3caa1775 100644 --- a/cherrypy/tutorial/tut04_complex_site.py +++ b/cherrypy/tutorial/tut04_complex_site.py @@ -5,11 +5,14 @@ This tutorial shows you how to create a site structure through multiple possibly nested request handler objects. """ +import os.path + import cherrypy class HomePage: + @cherrypy.expose def index(self): return ''' <p>Hi, this is the home page! Check out the other @@ -19,17 +22,16 @@ class HomePage: <li><a href="/joke/">A silly joke</a></li> <li><a href="/links/">Useful links</a></li> </ul>''' - index.exposed = True class JokePage: + @cherrypy.expose 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: @@ -40,6 +42,7 @@ class LinksPage: # methods! self.extra = ExtraLinksPage() + @cherrypy.expose 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 @@ -62,11 +65,11 @@ class LinksPage: <p>[<a href="../">Return</a>]</p> ''' - index.exposed = True class ExtraLinksPage: + @cherrypy.expose def index(self): # Note the relative link back to the Links page! return ''' @@ -78,7 +81,6 @@ class ExtraLinksPage: </ul> <p>[<a href="../">Return to links page</a>]</p>''' - index.exposed = True # Of course we can also mount request handler objects right here! @@ -92,7 +94,6 @@ root.links = LinksPage() # creating all contained request handler objects. -import os.path tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') if __name__ == '__main__': @@ -100,6 +101,3 @@ if __name__ == '__main__': # to objects, so we need to mount a request handler root. A request # to '/' will be mapped to HelloWorld().index(). cherrypy.quickstart(root, config=tutconf) -else: - # This branch is for the test suite; you can ignore it. - cherrypy.tree.mount(root, config=tutconf) diff --git a/cherrypy/tutorial/tut05_derived_objects.py b/cherrypy/tutorial/tut05_derived_objects.py index 9f4f2c32..f626e03f 100644 --- a/cherrypy/tutorial/tut05_derived_objects.py +++ b/cherrypy/tutorial/tut05_derived_objects.py @@ -7,6 +7,8 @@ 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. """ +import os.path + import cherrypy @@ -45,6 +47,7 @@ class HomePage(Page): # create a subpage self.another = AnotherPage() + @cherrypy.expose def index(self): # Note that we call the header and footer methods inherited # from the Page class! @@ -54,22 +57,20 @@ class HomePage(Page): <a href="./another/">another page</a>, too! </p> ''' + self.footer() - index.exposed = True class AnotherPage(Page): title = 'Another Page' + @cherrypy.expose def index(self): return self.header() + ''' <p> And this is the amazing second page! </p> ''' + self.footer() - index.exposed = True -import os.path tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') if __name__ == '__main__': @@ -77,6 +78,3 @@ if __name__ == '__main__': # to objects, so we need to mount a request handler root. A request # to '/' will be mapped to HelloWorld().index(). cherrypy.quickstart(HomePage(), config=tutconf) -else: - # This branch is for the test suite; you can ignore it. - cherrypy.tree.mount(HomePage(), config=tutconf) diff --git a/cherrypy/tutorial/tut06_default_method.py b/cherrypy/tutorial/tut06_default_method.py index f71c315a..0ce4cabe 100644 --- a/cherrypy/tutorial/tut06_default_method.py +++ b/cherrypy/tutorial/tut06_default_method.py @@ -16,11 +16,14 @@ like /users/<username>. Since the <username> bit will not be found (as there are no matching methods), it is handled by the default method. """ +import os.path + import cherrypy class UsersPage: + @cherrypy.expose 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 @@ -30,27 +33,25 @@ class UsersPage: <a href="./hendrik">Hendrik Mans</a><br/> <a href="./lorenzo">Lorenzo Lamas</a><br/> ''' - index.exposed = True + @cherrypy.expose 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" + out = 'Remi Delon, CherryPy lead developer' elif user == 'hendrik': - out = "Hendrik Mans, CherryPy co-developer & crazy German" + out = 'Hendrik Mans, CherryPy co-developer & crazy German' elif user == 'lorenzo': - out = "Lorenzo Lamas, famous actor and singer!" + out = 'Lorenzo Lamas, famous actor and singer!' else: - out = "Unknown user. :-(" + out = 'Unknown user. :-(' return '%s (<a href="./">back</a>)' % out - default.exposed = True -import os.path tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') if __name__ == '__main__': @@ -58,6 +59,3 @@ if __name__ == '__main__': # to objects, so we need to mount a request handler root. A request # to '/' will be mapped to HelloWorld().index(). cherrypy.quickstart(UsersPage(), config=tutconf) -else: - # This branch is for the test suite; you can ignore it. - cherrypy.tree.mount(UsersPage(), config=tutconf) diff --git a/cherrypy/tutorial/tut07_sessions.py b/cherrypy/tutorial/tut07_sessions.py index 62efa440..204322b5 100644 --- a/cherrypy/tutorial/tut07_sessions.py +++ b/cherrypy/tutorial/tut07_sessions.py @@ -8,6 +8,8 @@ any kind of object into that dictionary; otherwise, you are limited to objects that can be pickled. """ +import os.path + import cherrypy @@ -15,6 +17,7 @@ class HitCounter: _cp_config = {'tools.sessions.on': True} + @cherrypy.expose def index(self): # Increase the silly hit counter count = cherrypy.session.get('count', 0) + 1 @@ -27,10 +30,8 @@ class HitCounter: During your current session, you've viewed this page %s times! Your life is a patio of fun! ''' % count - index.exposed = True -import os.path tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') if __name__ == '__main__': @@ -38,6 +39,3 @@ if __name__ == '__main__': # to objects, so we need to mount a request handler root. A request # to '/' will be mapped to HelloWorld().index(). cherrypy.quickstart(HitCounter(), config=tutconf) -else: - # This branch is for the test suite; you can ignore it. - cherrypy.tree.mount(HitCounter(), config=tutconf) diff --git a/cherrypy/tutorial/tut08_generators_and_yield.py b/cherrypy/tutorial/tut08_generators_and_yield.py index 40566e32..18f42f93 100644 --- a/cherrypy/tutorial/tut08_generators_and_yield.py +++ b/cherrypy/tutorial/tut08_generators_and_yield.py @@ -7,33 +7,34 @@ in situations where using a template package like CherryPy or Cheetah would be overkill, and messy string concatenation too uncool. ;-) """ +import os.path + import cherrypy class GeneratorDemo: def header(self): - return "<html><body><h2>Generators rule!</h2>" + return '<html><body><h2>Generators rule!</h2>' def footer(self): - return "</body></html>" + return '</body></html>' + @cherrypy.expose 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>" + yield '<h3>List of users:</h3>' for user in users: - yield "%s<br/>" % user + yield '%s<br/>' % user yield self.footer() - index.exposed = True -import os.path tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') if __name__ == '__main__': @@ -41,6 +42,3 @@ if __name__ == '__main__': # to objects, so we need to mount a request handler root. A request # to '/' will be mapped to HelloWorld().index(). cherrypy.quickstart(GeneratorDemo(), config=tutconf) -else: - # This branch is for the test suite; you can ignore it. - cherrypy.tree.mount(GeneratorDemo(), config=tutconf) diff --git a/cherrypy/tutorial/tut09_files.py b/cherrypy/tutorial/tut09_files.py index 6a7b4a34..48585cbe 100644 --- a/cherrypy/tutorial/tut09_files.py +++ b/cherrypy/tutorial/tut09_files.py @@ -41,15 +41,18 @@ popular browsers. """ import os -localDir = os.path.dirname(__file__) -absDir = os.path.join(os.getcwd(), localDir) +import os.path import cherrypy from cherrypy.lib import static +localDir = os.path.dirname(__file__) +absDir = os.path.join(os.getcwd(), localDir) + class FileDemo(object): + @cherrypy.expose def index(self): return """ <html><body> @@ -62,8 +65,8 @@ class FileDemo(object): <a href='download'>This one</a> </body></html> """ - index.exposed = True + @cherrypy.expose def upload(self, myFile): out = """<html> <body> @@ -85,16 +88,14 @@ class FileDemo(object): size += len(data) return out % (size, myFile.filename, myFile.content_type) - upload.exposed = True + @cherrypy.expose def download(self): - path = os.path.join(absDir, "pdf_file.pdf") - return static.serve_file(path, "application/x-download", - "attachment", os.path.basename(path)) - download.exposed = True + path = os.path.join(absDir, 'pdf_file.pdf') + return static.serve_file(path, 'application/x-download', + 'attachment', os.path.basename(path)) -import os.path tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') if __name__ == '__main__': @@ -102,6 +103,3 @@ if __name__ == '__main__': # to objects, so we need to mount a request handler root. A request # to '/' will be mapped to HelloWorld().index(). cherrypy.quickstart(FileDemo(), config=tutconf) -else: - # This branch is for the test suite; you can ignore it. - cherrypy.tree.mount(FileDemo(), config=tutconf) diff --git a/cherrypy/tutorial/tut10_http_errors.py b/cherrypy/tutorial/tut10_http_errors.py index c0e9893b..18f02fd0 100644 --- a/cherrypy/tutorial/tut10_http_errors.py +++ b/cherrypy/tutorial/tut10_http_errors.py @@ -9,18 +9,21 @@ logged, displayed, and formatted. """ import os -localDir = os.path.dirname(__file__) -curpath = os.path.normpath(os.path.join(os.getcwd(), localDir)) +import os.path import cherrypy +localDir = os.path.dirname(__file__) +curpath = os.path.normpath(os.path.join(os.getcwd(), localDir)) + class HTTPErrorDemo(object): # Set a custom response for 403 errors. _cp_config = {'error_page.403': - os.path.join(curpath, "custom_error.html")} + os.path.join(curpath, 'custom_error.html')} + @cherrypy.expose def index(self): # display some links that will result in errors tracebacks = cherrypy.request.show_tracebacks @@ -49,8 +52,8 @@ class HTTPErrorDemo(object): when you raise an error.</a></p> </body></html> """ % trace - index.exposed = True + @cherrypy.expose def toggleTracebacks(self): # simple function to toggle tracebacks on and off tracebacks = cherrypy.request.show_tracebacks @@ -58,22 +61,20 @@ class HTTPErrorDemo(object): # redirect back to the index raise cherrypy.HTTPRedirect('/') - toggleTracebacks.exposed = True + @cherrypy.expose def error(self, code): # raise an error based on the get query raise cherrypy.HTTPError(status=code) - error.exposed = True + @cherrypy.expose def messageArg(self): message = ("If you construct an HTTPError with a 'message' " - "argument, it wil be placed on the error page " - "(underneath the status line by default).") + 'argument, it wil be placed on the error page ' + '(underneath the status line by default).') raise cherrypy.HTTPError(500, message=message) - messageArg.exposed = True -import os.path tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf') if __name__ == '__main__': @@ -81,6 +82,3 @@ if __name__ == '__main__': # to objects, so we need to mount a request handler root. A request # to '/' will be mapped to HelloWorld().index(). cherrypy.quickstart(HTTPErrorDemo(), config=tutconf) -else: - # This branch is for the test suite; you can ignore it. - cherrypy.tree.mount(HTTPErrorDemo(), config=tutconf) |
