{ "metadata": { "name": "9932_06_03" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Chapter 6, example 3\n", "====================\n", "\n", "Here we show how to use rich representations in the notebook." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We first define a standard Python class that admits a SVG representation method. A Disc instance can be rendered as SVG in the notebook." ] }, { "cell_type": "code", "collapsed": false, "input": [ "class Disc(object):\n", " def __init__(self, size, color='red'):\n", " self.size = size\n", " self.color = color\n", " \n", " def _repr_svg_(self):\n", " return \"\"\"\n", " \n", " \"\"\".format(self.size, self.color)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "Disc(80, 'purple')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 2, "svg": [ "\n", " \n", " " ], "text": [ "<__main__.Disc at 0x582c790>" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Other rich representation types are available in `IPython.display`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import IPython.display as dsp" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, we can type mathematical equations in LaTeX, and they are nicely rendered in the notebook." ] }, { "cell_type": "code", "collapsed": false, "input": [ "dsp.Math(r\"\\sum_{n=1}^\\infty \\frac{1}{n^2} = \\frac{\\pi^2}{6}\")" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\sum_{n=1}^\\infty \\frac{1}{n^2} = \\frac{\\pi^2}{6}$$" ], "output_type": "pyout", "prompt_number": 4, "text": [ "" ] } ], "prompt_number": 4 } ], "metadata": {} } ] }