Last active
April 11, 2017 11:49
-
-
Save bofm/cb5f196d353de9656b9d to your computer and use it in GitHub Desktop.
defines IPython Notebook line magic to render 2d-array as HTML table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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