from IPython.display import display from IPython.display import ( display_html, display_jpeg, display_png, display_javascript, display_svg, display_latex ) import io import pandas %%writefile data.csv Date,Open,High,Low,Close,Volume,Adj Close 2012-06-01,569.16,590.00,548.50,584.00,14077000,581.50 2012-05-01,584.90,596.76,522.18,577.73,18827900,575.26 2012-04-02,601.83,644.00,555.00,583.98,28759100,581.48 2012-03-01,548.17,621.45,516.22,599.55,26486000,596.99 2012-02-01,458.41,547.61,453.98,542.44,22001000,540.12 2012-01-03,409.40,458.24,409.00,456.48,12949100,454.53 df = pandas.read_csv("data.csv") pandas.set_option('display.notebook_repr_html', False) df pandas.set_option('display.notebook_repr_html', True) df lines = df._repr_html_().splitlines() print "\n".join(lines[:20]) %load soln/mycircle.py c = MyCircle() display_html(c) display_svg(c) display_latex(c) display_javascript(c) from sympy import Rational, pi, exp, I, symbols x, y, z = symbols("x y z") r = Rational(3,2)*pi + exp(I*x) / (x**2 + y) r from sympy.interactive.printing import init_printing init_printing() r ip = get_ipython() for mime, formatter in ip.display_formatter.formatters.items(): print '%24s : %s' % (mime, formatter.__class__.__name__) png_f = ip.display_formatter.formatters['image/png'] png_f.for_type? %matplotlib inline import matplotlib.pyplot as plt class AnotherCircle(object): def __init__(self, radius=1, center=(0,0), color='r'): self.radius = radius self.center = center self.color = color def __repr__(self): return "<%s Circle with r=%s at %s>" % ( self.color, self.radius, self.center, ) c = AnotherCircle() c from IPython.core.pylabtools import print_figure def png_circle(circle): """Render AnotherCircle to png data using matplotlib""" fig, ax = plt.subplots() patch = plt.Circle(circle.center, radius=circle.radius, fc=circle.color, ) ax.add_patch(patch) plt.axis('scaled') data = print_figure(fig, 'png') # We MUST close the figure, otherwise IPython's display machinery # will pick it up and send it as output, resulting in a double display plt.close(fig) return data c = AnotherCircle() print repr(png_circle(c)[:10]) png_f.for_type(AnotherCircle, png_circle) c2 = AnotherCircle(radius=2, center=(1,0), color='g') c2 display_png(c2) # for demonstration purpose, I do the same with a circle that has no _repr_javascript method class MyNoJSCircle(MyCircle): def _repr_javascript_(self): return cNoJS = MyNoJSCircle() cNoJS for i in range(3): display(cNoJS) print "I should see a nice html circle in web-app, but" print "nothing if the format I'm viewing the notebook in" print "does not support html" display_html(cNoJS) print "Whatever the format I will see a representation" print "of my circle" display(cNoJS) print "Same if I return the object" cNoJS print "But not if I print it" print cNoJS