IPython's Rich Display System

In Python, objects can declare their textual representation using the __repr__ method. IPython expands on this idea and allows objects to declare other, richer representations including:

A single object can declare some or all of these representations; all are handled by IPython's display system. This Notebook shows how you can use this display system to incorporate a broad range of content into your Notebooks.

Basic display imports

The display function is a general purpose tool for displaying different representations of objects. Think of it as print for these rich representations.

In []:
from IPython.display import display

A few points:

If you want to display a particular representation, there are specific functions for that:

In []:
from IPython.display import (
    display_pretty, display_html, display_jpeg,
    display_png, display_json, display_latex, display_svg


To work with images (JPEG, PNG) use the Image class.

In []:
from IPython.display import Image
In []:
i = Image(filename='figs/logo.png')

Returning an Image object from an expression will automatically display it:

In []:

Or you can pass it to display:

In []:

An image can also be displayed from raw data or a url

In []:

When you display an image from a URL, the image data will not be embedded in the Notebook file. This means you will have to re-run that cell to see the image again. You can override this behavior by setting embed=True:

In []:
Image(url='http://www.python.org/community/logos/python-logo-master-v3-TM.png', embed=True)

SVG images are also supported out of the box (since modern browsers do a good job of rendering them):

In []:
from IPython.display import SVG


Find an image online and use IPython's Image class to embed it in a Notebook using its URL. Then try downloading the image into your Notebook directory and embedding it by filename.


More exotic objects can also be displayed, as long as their representation supports the IPython display protocol. For example, videos hosted externally on YouTube are easy to load (and writing a similar wrapper for other hosted content is trivial):

In []:
from IPython.display import YouTubeVideo


Python objects can declare HTML representations that will be displayed in the Notebook. If you have some HTML you want to display, simply use the HTML class.

In []:
from IPython.display import HTML
In []:
s = """<table>
<th>Header 1</th>
<th>Header 2</th>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
In []:
h = HTML(s); h


In []:
from IPython.display import Javascript
In []:
In []:

circle {
  fill: rgb(31, 119, 180);
  fill-opacity: .25;
  stroke: rgb(31, 119, 180);
  stroke-width: 1px;

.leaf circle {
  fill: #ff7f0e;
  fill-opacity: 1;

text {
  font: 10px sans-serif;

In []:

// This unhides the output area

// element is the jQuery element we will append to
var e = element.get(0);
var diameter = 600,
    format = d3.format(",d");

var pack = d3.layout.pack()
    .size([diameter - 4, diameter - 4])
    .value(function(d) { return d.size; });

var svg = d3.select(e).append("svg")
    .attr("width", diameter)
    .attr("height", diameter)
    .attr("transform", "translate(2,2)");

d3.json("files/flare.json", function(error, root) {
  var node = svg.datum(root).selectAll(".node")
      .attr("class", function(d) { return d.children ? "node" : "leaf node"; })
      .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

      .text(function(d) { return d.name + (d.children ? "" : ": " + format(d.size)); });

      .attr("r", function(d) { return d.r; });

  node.filter(function(d) { return !d.children; }).append("text")
      .attr("dy", ".3em")
      .style("text-anchor", "middle")
      .text(function(d) { return d.name.substring(0, d.r / 3); });

d3.select(self.frameElement).style("height", diameter + "px");


Pandas makes use of this capability to allow DataFrames to be represented as HTML tables.

In []:
import pandas

By default, DataFrames will be represented as text; to enable HTML representations we need to set a print option:

In []:

Here is a small amount of stock data for APPL:

In []:
%%file data.csv
Date,Open,High,Low,Close,Volume,Adj Close

Read this as into a DataFrame:

In []:
df = pandas.read_csv('data.csv')

And view the HTML representation:

In []:


In []:
%load_ext sympy.interactive.ipythonprinting
# If you are using older versions of SymPy use:
# %load_ext sympyprinting
In []:
from __future__ import division
import sympy as sym
from sympy import *
x, y, z = symbols("x y z")
k, m, n = symbols("k m n", integer=True)
f, g, h = map(Function, 'fgh')
In []:
Rational(3,2)*pi + exp(I*x) / (x**2 + y)
In []:
a = 1/x + (x*sin(x) - 1)/x
In []:
(1/cos(x)).series(x, 0, 6)

External sites

You can even embed an entire page from another site in an iframe; for example this is today's Wikipedia page for mobile users:

In []:
HTML('<iframe src=http://en.mobile.wikipedia.org/?useformat=mobile width=700 height=350></iframe>')


Go to SoundCloud and search for a sound clip you want to embed in your Notebook. To find the HTML code to embed the sound, click on the "Share" link and copy the "Widget" HTML. Then paste it into HTML(""" """) in a Notebook cell.

In []:
%load soln/soundcloud.py


And we also support the display of mathematical expressions typeset in LaTeX, which is rendered in the browser thanks to the MathJax library.

In []:
from IPython.display import Math
Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')

With the Latex class, you have to include the delimiters yourself. This allows you to use other LaTeX modes such as eqnarray:

In []:
from IPython.display import Latex
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0 

Orignal Source: IPython: Interactive Computing in Python's Git repository