import os.path
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render('index.html')
class PoemPageHandler(tornado.web.RequestHandler):
def post(self):
noun1 = self.get_argument('noun1')
noun2 = self.get_argument('noun2')
verb = self.get_argument('verb')
noun3 = self.get_argument('noun3')
self.render('poem.html', roads=noun1, wood=noun2, made=verb,difference=noun3)
if __name__ == '__main__':
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[(r'/', IndexHandler), (r'/poem', PoemPageHandler)],
template_path=os.path.join(os.path.dirname(__file__), "templates")
)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
This sample code is from the book 'Introduction to Tornado'. This example code in a single file is quite easy to understand but it's not really practical because it's creating multiple classes in a single file.
What I want to do is - I'd like to create individual module(?) files that handles specific pages and its actions. For example, I'd like to rewrite the codes in three different files.
Since I'm new to this Python Tornado Framework, I'm not quite sure what the best way to rewrite & refactor the whole codes. Would someone please show me a better way of separating py files to maintain classes easily?
Thanks !!
Check this repo. It contains an example of tornado project.
It takes an idea from django. It splitted into apps and apps are splittend into models, handlers, forms, etc.
acl_webapp # <-- the project
+-- base # <-- app (contains base forms, models, handlers)
+-- accounts # <-- app
+-- logs
+-- news # <-- app
| +-- __init__.py
| +-- forms.py # <-- contains forms
| +-- handlers.py # <-- contains handlers
| +-- models.py # <-- contains models
+-- pages # <-- app
+-- static
+-- templates
+-- utils
+-- __init__.py
+-- app.py # <-- tornado Application entry point
+-- settings.py # <-- project settings
+-- urls.py # <-- project urls