Rich Output

In the notebook format you are not limited to console output. In this example we'll generate more interesting things in Python for inclusion in the notebook.

Values

The simplest form of output is the evaluation value of the cell. This is very similar to the regular Python interactive read-eval-print-loop. Built-in data types have familiar representations. None is not shown.


In [1]:
2


Out[1]:
2

In [2]:
"a string"


Out[2]:
'a string'

In [3]:
None

In [4]:
{ 'y': 3, 'x': 5}


Out[4]:
{'x': 5, 'y': 3}

Print

You can also print strings to the output. Notice that the Out[] label is shown for the evaluation value, not any printed output.


In [5]:
print('Hello, world')


Hello, world

In [6]:
print("Hey, what's my value?")
42


Hey, what's my value?
Out[6]:
42

Value representation

If you do not specify a way to represent custom values, they appear using the default Python representation which shows the class and a memory location.


In [7]:
class Thing:
    def __init__(self, x):
        self.x = x

Thing(3)


Out[7]:
<__main__.Thing instance at 0x27cfa28>

The __repr__ function can be defined for your class to provide a string representation.


In [8]:
class Thing:
    def __init__(self, x):
        self.x = x
    def __repr__(self):
        return 'Thing({})'.format(self.x)

Thing(3)


Out[8]:
Thing(3)

HTML

Code cells can produce HTML output directly using the IPython.display module. Notice that HTML display objects must be used, print cannot produce HTML in the output area.


In [9]:
from IPython.display import HTML
HTML('<span style="color: #000; background-color: #f00;">bold</span>')


Out[9]:
bold

In [10]:
print('<h1>I wanna be big</h1>')


<h1>I wanna be big</h1>

HTML representation

Just like with strings, custom Python objects can define a method _repr_html_ that lets them be shown using HTML in the output.


In [11]:
class Thing:
    def __init__(self, x):
        self.x = x
    
    def _repr_html_(self):
        ''' Fancy shadow effect '''
        style = 'text-shadow: 1px 2px 0 #888;'
        return 'Thing(<span style="{}">{}</span>)'.format(style, self.x)

Thing(3)


Out[11]:
Thing(3)

Images

Use the Image class from IPython.display to work with PNG or JPEG images.


In [12]:
from IPython.display import Image

pineapple = Image('../../images/Pineapple-256.png')
pineapple


Out[12]:

The default working directory is the directory containing your notebook file.

You can also load images from the internet.

(Photo below by Aaron Burden).


In [13]:
flower = Image(url='http://i.imgur.com/RmIUQ7R.jpg', embed=True)
flower


Out[13]:

Embed

For media such as images included by URL, the default behavior is for the saved notebook to only include a reference to the online data. When the notebook is reopened the image will be fetched following the URL. To save a copy of the media in the notebook you can add the embed=True option.

Audio

You can also turn arrays of numbers into audio data. An Audio object contains audio data and metadata about mono/stereo, sample format and playback framerate. In this example we use numpy to do phase modulation.


In [14]:
from IPython.display import Audio
import numpy as np

framerate = 8000
seconds = 3.0
frames = framerate * seconds
x = np.linspace(0, seconds, frames)
freq = 440.0 * 4.0
fmult = 1.5
mod = np.linspace(2.0, -2.0, frames)
freq2 = freq * fmult
y = np.sin(x * freq + mod * np.sin(x * freq2));

sound = Audio(data=y, rate=framerate)
sound


Out[14]: