Skip to content

Instantly share code, notes, and snippets.

@bofm
Last active April 11, 2017 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bofm/cb5f196d353de9656b9d to your computer and use it in GitHub Desktop.
Save bofm/cb5f196d353de9656b9d to your computer and use it in GitHub Desktop.
defines IPython Notebook line magic to render 2d-array as HTML table.
from IPython.core.magic import (register_line_magic, register_cell_magic,
register_line_cell_magic)
from six import string_types
class T(list):
def _repr_html_(self):
html = [u"<table>"]
for row in self:
html.append(u"<tr>")
if isinstance(row, string_types):
html.append(u"<td>{0}</td>".format(row))
else:
for col in row:
html.append(u"<td>{0}</td>".format(col))
html.append(u"</tr>")
html.append(u"</table>")
return u''.join(html)
@register_line_magic
def t(line):
"my line magic"
return eval('T(%s)' % line)
# Usage
%t [('a', 'b'), (1,2)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment