Getting the Tornado Facebook demo working

Recently, I downloaded Tornado:

Tornado is an open source version of the scalable, non-blocking web server and tools that power FriendFeed.

Included is a facebook demo that displays your feed though it didn’t work “out-of-the-box” raising the error:

File "modules/post.html", line 26, in _execute
KeyError: 'message'

modules/post.html is a snippet of html used to render each feed item. The snippet looks like this:

<div class="post">
  <div class="picture">
    {% set author_url="" + escape(post["from"]["id"]) %}
    <a href="{{ author_url }}"><img src="//{{ escape(post["from"]["id"]) }}/picture?type=square"/></a>
  <div class="body">
    <a href="{{ author_url }}" class="actor">{{ escape(post["from"]["name"]) }}&lt;/a>
    {% if post["message"] %}
      <span class="message">{{ escape(post["message"]) }}</span>
    {% end %}
    <div class="meta">
      <a href="{{ escape(post["actions"][0]["link"]) }}" class="permalink">{{ locale.format_date(datetime.datetime.strptime(post["created_time"], "%Y-%m-%dT%H:%M:%S+0000")) }}</a>

The curly braces are python code snippets and the line reading if post[“message”] is causing the problem. In python referencing a non-existent dictionary key raises a KeyError exception thus changing the code to use:

{% if “message” in post %}

To debug this problem I simply removed the code referencing post[“message”] and replaced it with str(post) which simply dumped the JSON contents onto the page. From there I could see that not all entries had a message key thus the error.

One more note. Looking at the documentation Tornado uses a library call epoll which at first glance sounds familiar to I/O completion ports on Windows which is something I’m familiar with.