Chapter 6, example 3

Here we show how to use rich representations in the notebook.

We first define a standard Python class that admits a SVG representation method. A Disc instance can be rendered as SVG in the notebook.


In [1]:
class Disc(object):
    def __init__(self, size, color='red'):
        self.size = size
        self.color = color
        
    def _repr_svg_(self):
        return """<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
                  <circle cx="{0:d}" cy="{0:d}" r="{0:d}" fill="{1:s}" />
                  </svg>""".format(self.size, self.color)

In [2]:
Disc(80, 'purple')


Out[2]:

Other rich representation types are available in IPython.display.


In [3]:
import IPython.display as dsp

Here, we can type mathematical equations in LaTeX, and they are nicely rendered in the notebook.


In [4]:
dsp.Math(r"\sum_{n=1}^\infty \frac{1}{n^2} = \frac{\pi^2}{6}")


Out[4]:
$$\sum_{n=1}^\infty \frac{1}{n^2} = \frac{\pi^2}{6}$$