From the Firehose to the Web Browser
We’re already collecting the data we need, and Tornado is ready to respond to requests for updates. Basically, all we have left to do is build the web page and write the JavaScript.
Templates in Tornado
The template language in Tornado is based on the templates in Django. Both of these projects contain rich templating languages where standard HTML files get parsed based on logic written in a subset of Python. However, a big difference between the templating language that inspired Tornado and Tornado itself is the ability to include arbitrary Python code. In Django, you’re restricted to variables, filters, and standard flow control statements. To go beyond the most basic logic, you must create new template filters and run those. With Tornado, you’re allowed to use any valid Python expression, including functions and list comprehensions. As long as it’s a valid Python expression, it will work in these templates.
Tornado offers a straightforward mapping of most of Python’s
standard flow control statements. You can specify for
loops, if statements,
try/catch blocks, and many of the
basic control statements that come to mind. These control statements are
wrapped between {% and %}.
Developers can also specify arbitrary expressions; those are marked up
by wrapping the code with {{ and
}}. The expression is evaluated and the result is then placed directly into the HTML. The following is an example template that contains both flow control and a simple Python ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access