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')





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]:
Header 1 Header 2
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2

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]:
playerID yearID gameNum gameID teamID lgID GP startingPos
0 aaronha01 1955 0 NLS195507120 ML1 NL 1 None
1 aaronha01 1956 0 ALS195607100 ML1 NL 1 None

In [6]:
df._repr_html_()  # pandas 已经添加了`_repr_html_`方法, 所以能显示


Out[6]:
u'<div>\n<table border="1" class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      <th>playerID</th>\n      <th>yearID</th>\n      <th>gameNum</th>\n      <th>gameID</th>\n      <th>teamID</th>\n      <th>lgID</th>\n      <th>GP</th>\n      <th>startingPos</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>aaronha01</td>\n      <td>1955</td>\n      <td>0</td>\n      <td>NLS195507120</td>\n      <td>ML1</td>\n      <td>NL</td>\n      <td>1</td>\n      <td>None</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>aaronha01</td>\n      <td>1956</td>\n      <td>0</td>\n      <td>ALS195607100</td>\n      <td>ML1</td>\n      <td>NL</td>\n      <td>1</td>\n      <td>None</td>\n    </tr>\n  </tbody>\n</table>\n</div>'

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)


a1
c4
b2