Show objects with display

IPython can display objects such as HTML, JSON, PNG, JPEG, SVG and Latex


To display images (JPEG, PNG) in IPython and notebooks, you can use the Image class:

from IPython.display import Image
<IPython.core.display.Image object>
from IPython.display import SVG

Non-embedded images

  • By default, image data is embedded:

    Image ('img_url')
  • However, if the url is given as kwarg, this is interpreted as a soft link:

    Image (url='img_url')
  • embed can also be specified explicitly:

    Image (url='img_url', embed = True)


Python objects can declare HTML representations to be displayed in a notebook:

from IPython.display import HTML
  • foo
  • bar


With notebooks, objects can also declare a JavaScript representation. This enables for example data visualisations with Javascript libraries like d3.js.

from IPython.display import Javascript

welcome = Javascript(
    'alert("Dies ist ein Beispiel für eine durch IPython angezeigte Javascript-Warnung.")'

For more extensive Javascript you can also use the %%javascript syntax.


IPython.display also has built-in support for displaying mathematical expressions set in LaTeX and rendered in the browser with MathJax:

from IPython.display import Math

Math(r"F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx")
$\displaystyle F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$

With the Latex class you have to specify the limits yourself. In this way, however, you can also use other LaTeX modes, such as eqnarray:

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}} \\
\begin{eqnarray} \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ \end{eqnarray}


IPython also enables interactive work with sounds. With the display.Audio class you can create an audio control that is embedded in the notebook. The interface is analogous to that of the Image class. All audio formats supported by the browser can be used.

from IPython.display import Audio

In the following we will output the sine function of a NumPy array as an audio signal. The Audio class normalises and codes the data and embeds the resulting audio in the notebook.

import numpy as np

f = 500.0
rate = 8000
L = 3
times = np.linspace(0, L, rate * L)
signal = np.sin(f * times)

Audio(data=signal, rate=rate)

Display notebooks

import os
import sys
import types

import nbformat

from IPython.display import HTML, display
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import PythonLexer

formatter = HtmlFormatter()
lexer = PythonLexer()

# publish the CSS for pygments highlighting
<style type='text/css'>
        % formatter.get_style_defs()
def show_notebook(fname):
    """display a short summary of the cells of a notebook"""
    nb =, as_version=4)
    html = []
    for cell in nb.cells:
        html.append("<h4>%s cell</h4>" % cell.cell_type)
        if cell.cell_type == "code":
            html.append(highlight(cell.source, lexer, formatter))
            html.append("<pre>%s</pre>" % cell.source)


markdown cell

# `foo.ipynb`

code cell

def bar():
    return "bar"

code cell

def dirlist():
    listing = !ls
    return listing

code cell

def whatsmyname():
    return __name__