daniel__
daniel__

Reputation: 11845

AssertionError in tornado

I edited this question to show a small example. This demo code is from Introduction to tornado book, and give me this error:

Traceback (most recent call last):
  File "demo.py", line 25, in <module>
    ui_modules={'Hello', HelloModule}
  File "/usr/local/lib/python2.7/dist-packages/tornado-3.1.1-py2.7.egg/tornado/web.py", line 1422, in __init__
    self._load_ui_modules(settings.get("ui_modules", {}))
  File "/usr/local/lib/python2.7/dist-packages/tornado-3.1.1-py2.7.egg/tornado/web.py", line 1545, in _load_ui_modules
    assert isinstance(modules, dict)
AssertionError

demo.py

import tornado.web
import tornado.httpserver
import tornado.ioloop
import tornado.options 
import os.path
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)


class HelloHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('hello.html')


class HelloModule(tornado.web.UIModule):
    def render(self):
        return '<h1>Hello, world!</h1>'

if __name__ == '__main__':
    tornado.options.parse_command_line()

    app = tornado.web.Application(
        handlers=[(r'/', HelloHandler)],
        template_path=os.path.join(os.path.dirname(__file__), 'templates'),
        ui_modules={'Hello', HelloModule}
    )
    server = tornado.httpserver.HTTPServer(app)
    server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

hello.html

<html>
<head><title>UI Module Example</title></head>
<body>
{% module Hello() %}
</body>
</html>

Upvotes: 0

Views: 1662

Answers (2)

stingMantis
stingMantis

Reputation: 340

There is actually an errata for the book located Here

Upvotes: 1

Thomas Orozco
Thomas Orozco

Reputation: 55197

ui_modules={'Hello', HelloModule} is not a dict, it's a set.

It should be: ui_modules={'Hello': HelloModule} (notice the comma was replaced with a colon).

Upvotes: 5

Related Questions