From: Greg Ward Date: Sun, 21 Jun 2015 14:04:21 +0000 (-0400) Subject: Consistently format the three "Hello, world" examples (docs and README) X-Git-Tag: v4.3.0b1~89^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5039d3d28b5897a8e614d8dbb277f68a8be75318;p=thirdparty%2Ftornado.git Consistently format the three "Hello, world" examples (docs and README) - encourage use of make_app() (makes testing easier) - use fully-qualified module paths (painful in real-world code, but makes examples clearer) - don't define a main() function that is never called: just use a boring old "__name__ == __main__" block, so the code can be copy + pasted + executed immediately - just use a tuple when defining routes, not url(...): this is supposed to be a simple, minimalist example --- diff --git a/README.rst b/README.rst index 446e42bdf..cbe53f76b 100644 --- a/README.rst +++ b/README.rst @@ -41,13 +41,15 @@ Here is a simple "Hello, world" example web app for Tornado: def get(self): self.write("Hello, world") - application = tornado.web.Application([ - (r"/", MainHandler), - ]) + def make_app(): + return tornado.web.Application([ + (r"/", MainHandler), + ]) if __name__ == "__main__": - application.listen(8888) - tornado.ioloop.IOLoop.instance().start() + app = make_app() + app.listen(8888) + tornado.ioloop.IOLoop.current().start() This example does not use any of Tornado's asynchronous features; for that see this `simple chat room diff --git a/docs/guide/structure.rst b/docs/guide/structure.rst index 8527f4af6..d8ad341f0 100644 --- a/docs/guide/structure.rst +++ b/docs/guide/structure.rst @@ -16,22 +16,22 @@ A minimal "hello world" example looks something like this: .. testcode:: - from tornado.ioloop import IOLoop - from tornado.web import RequestHandler, Application, url + import tornado.ioloop + import tornado.web - class HelloHandler(RequestHandler): + class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") def make_app(): - return Application([ - url(r"/", HelloHandler), - ]) + return tornado.web.Application([ + (r"/", MainHandler), + ]) - def main(): + if __name__ == "__main__": app = make_app() app.listen(8888) - IOLoop.current().start() + tornado.ioloop.IOLoop.current().start() .. testoutput:: :hide: diff --git a/docs/index.rst b/docs/index.rst index aae707710..52e653223 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -40,12 +40,14 @@ Here is a simple "Hello, world" example web app for Tornado:: def get(self): self.write("Hello, world") - application = tornado.web.Application([ - (r"/", MainHandler), - ]) + def make_app(): + return tornado.web.Application([ + (r"/", MainHandler), + ]) if __name__ == "__main__": - application.listen(8888) + app = make_app() + app.listen(8888) tornado.ioloop.IOLoop.current().start() This example does not use any of Tornado's asynchronous features; for