In [3]:
from IPython.display import Image, FileLink, FileLinks, HTML

In [27]:
Image(filename='../profile_double11/static/custom/dongxi.jpg')


<class 'IPython.core.display.Image'>
Out[27]:

In [6]:
FileLink('codemirror-python.ipynb')


<class 'IPython.lib.display.FileLink'>

In [7]:
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)


<class 'IPython.core.display.HTML'>
Out[7]:
Header 1 Header 2
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2

In [ ]:
l = [1, 2, 3]
l
# 显示的是__repr__方法的返回值

In [28]:
import sqlite3
con = sqlite3.connect("baseball-archive-2012.sqlite")
df = pd.read_sql("select * from allstarfull limit 2", con)

In [29]:
df


<class 'pandas.core.frame.DataFrame'>
Out[29]:
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

2 rows × 8 columns


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


Out[30]:
u'<div style="max-height:1000px;max-width:1500px;overflow:auto;">\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<p>2 rows \xd7 8 columns</p>\n</div>'

In [9]:
s = '0     1n1     3\n2     5\n3   NaN\n4     6\n5     8\ndtype: float64'

In [10]:
s


Out[10]:
'0     1\n1     3\n2     5\n3   NaN\n4     6\n5     8\ndtype: float64'

In [11]:
class A(object):
    def __repr__(self):
        return '0     1\n1     3\n2     5\n3   NaN\n4     6\n5     8\ndtype: float64'

In [12]:
a = A()
a


<class '__main__.A'>
Out[12]:
0     1
1     3
2     5
3   NaN
4     6
5     8
dtype: float64

In [ ]:
# http://ipython.org/ipython-doc/dev/config/integrating.htmls
# _repr_html_: return raw HTML as a string
# _repr_json_: return raw JSON as a string
# _repr_jpeg_: return raw JPEG data
# _repr_png_: return raw PNG data
# _repr_svg_: return raw SVG data as a string
# _repr_latex_: return LaTeX commands in a string surrounded by "$".

from IPython.display import (
    display_html, display_jpeg, display_png,
    display_javascript, display_svg, display_latex
)

In [ ]:
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)

In [ ]:
t = DictTable(a=1, b=2, c=3)

In [ ]:
display_html(t)

In [ ]:
t