In [1]:
from IPython.display import display_html, Image, FileLink, FileLinks, HTML
In [2]:
Image(filename='/tmp/dongxi.jpg', width='100px')
Out[2]:
In [3]:
FileLink('rich-display.ipynb')
Out[3]:
In [4]:
s = """<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>"""
HTML(s)
Out[4]:
In [5]:
import sqlite3
import pandas as pd
con = sqlite3.connect("/tmp/baseball.sqlite")
df = pd.read_sql("select * from allstarfull limit 2", con)
df
Out[5]:
In [6]:
df._repr_html_() # pandas 已经添加了`_repr_html_`方法, 所以能显示
Out[6]:
In [7]:
class DictTable(dict):
def _repr_html_(self):
html = ['<table>']
for key, value in self.iteritems():
html.append("<tr><td>{0}</td><td>{1}</td></tr>".format(key, value))
html.append("</table>")
return ''.join(html)
t = DictTable(a=1, b=2, c=4)
display_html(t)