I am trying to learn how to use the python web framework Tornado. I am already familiar with flask but so far I have difficulty even getting a simple app to start. My directory structure is as follows:
My code in app.py is simply:
define("port", default=5000, help="run on the given port", type=int)
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHandler),
]
settings = dict(
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
debug=True,
)
super(Application, self).__init__(handlers, **settings)
class MainHandler(tornado.web.RedirectHandler):
def get(self):
self.render("Testing.html")
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.current().start()
However when I run app.py I get the error:
ERROR:tornado.application:Uncaught exception
Traceback (most recent call last):
File "/vt/release/dist/3p/anaconda/lib/python2.7/site-packages/tornado/http1connection.py", line 238, in _read_message
delegate.finish()
File "/vt/release/dist/3p/anaconda/lib/python2.7/site-packages/tornado/httpserver.py", line 289, in finish
self.delegate.finish()
File "/vt/release/dist/3p/anaconda/lib/python2.7/site-packages/tornado/web.py", line 2047, in finish
self.execute()
File "/vt/release/dist/3p/anaconda/lib/python2.7/site-packages/tornado/web.py", line 2067, in execute
**self.handler_kwargs)
File "/vt/release/dist/3p/anaconda/lib/python2.7/site-packages/tornado/web.py", line 187, in __init__
self.initialize(**kwargs)
TypeError: initialize() takes at least 2 arguments (1 given)
Why is this? I should mention it does not give me the error until I try to connect.
Your MainHandler should inherit from RequestHandler, not RedirectHandler.
(Details: the RedirectHandler requires two arguments, "self" and "target_path". You'd specify the target path in your handlers list in Application.__init__. Since you don't have that second argument in the handlers list and you're incorrectly inheriting from RedirectHandler, Tornado gets an exception.)