IPython Notebook provides hook methods like _repr_html_
which can be defined by Python objects to allow richer representations. Here's an example:
In [1]:
class ListTable(list):
""" Overridden list class which takes a 2-dimensional list of
the form [[1,2,3],[4,5,6]], and renders an HTML Table in
IPython Notebook. """
def _repr_html_(self):
html = ["<table>"]
for row in self:
html.append("<tr>")
for col in row:
html.append("<td>{0}</td>".format(col))
html.append("</tr>")
html.append("</table>")
return ''.join(html)
In [23]:
import random
table = ListTable()
table.append(['x', 'y', 'x-y', '(x-y)^2'])
for i in xrange(7):
x = random.uniform(0, 10)
y = random.uniform(0, 10)
table.append([x, y, x-y, (x-y)**2])
table
Out[23]:
The 6 rich representions currently supported by IPython Notebook are:
- HTML
- JSON
- PNG
- JPEG
- SVG
- LaTeX
For more information, check out this IPython Notebook (by the IPython team).