Friday, June 28, 2013

Easy HTML output in IPython Notebook

If any object has a _repr_html_ method, the IPython Notebook will use it to render HTML output. It's really easy to make a simple class that permits general dynamic HTML-rich output with Markdown. Markdown is a superset of HTML, so HTML in your output string will work, too.
import markdown
class MD(str):
    def _repr_html_(self):
        return markdown.markdown(self)
Four little lines, and you can do this!