{ "metadata": { "name": "Rich Display System" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "IPython's Rich Display System" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:\n", "\n", "* HTML\n", "* JSON\n", "* PNG\n", "* JPEG\n", "* SVG\n", "* LaTeX\n", "\n", "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." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Basic display imports" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `display` function is a general purpose tool for displaying different representations of objects. Think of it as `print` for these rich representations." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import display" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A few points:\n", "\n", "* Calling `display` on an object will send **all** possible representations to the Notebook.\n", "* These representations are stored in the Notebook document.\n", "* In general the Notebook will use the richest available representation.\n", "\n", "If you want to display a particular representation, there are specific functions for that:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import (\n", " display_pretty, display_html, display_jpeg,\n", " display_png, display_json, display_latex, display_svg\n", ")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Images" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To work with images (JPEG, PNG) use the `Image` class." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import Image" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "i = Image(filename='figs/logo.png')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Returning an `Image` object from an expression will automatically display it:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "i" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or you can pass it to `display`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "display(i)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An image can also be displayed from raw data or a url" ] }, { "cell_type": "code", "collapsed": false, "input": [ "Image(url='http://www.python.org/community/logos/python-logo-master-v3-TM.png')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "Image(url='http://www.python.org/community/logos/python-logo-master-v3-TM.png', embed=True)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "SVG images are also supported out of the box (since modern browsers do a good job of rendering them):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import SVG\n", "SVG(filename='figs/python-logo.svg')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Video" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import YouTubeVideo\n", "YouTubeVideo('sjfsUzECqK0')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "HTML" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import HTML" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "s = \"\"\"\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Header 1Header 2
row 1, cell 1row 1, cell 2
row 2, cell 1row 2, cell 2
\"\"\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "h = HTML(s); h" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "JavaScript" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import Javascript" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "Javascript(\"\"\"$.getScript('http://d3js.org/d3.v3.min.js')\"\"\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "HTML(\"\"\"\n", "\n", "\"\"\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "Javascript(\"\"\"\n", "\n", "// This unhides the output area\n", "container.show();\n", "\n", "// element is the jQuery element we will append to\n", "var e = element.get(0);\n", " \n", "var diameter = 600,\n", " format = d3.format(\",d\");\n", "\n", "var pack = d3.layout.pack()\n", " .size([diameter - 4, diameter - 4])\n", " .value(function(d) { return d.size; });\n", "\n", "var svg = d3.select(e).append(\"svg\")\n", " .attr(\"width\", diameter)\n", " .attr(\"height\", diameter)\n", " .append(\"g\")\n", " .attr(\"transform\", \"translate(2,2)\");\n", "\n", "d3.json(\"files/flare.json\", function(error, root) {\n", " var node = svg.datum(root).selectAll(\".node\")\n", " .data(pack.nodes)\n", " .enter().append(\"g\")\n", " .attr(\"class\", function(d) { return d.children ? \"node\" : \"leaf node\"; })\n", " .attr(\"transform\", function(d) { return \"translate(\" + d.x + \",\" + d.y + \")\"; });\n", "\n", " node.append(\"title\")\n", " .text(function(d) { return d.name + (d.children ? \"\" : \": \" + format(d.size)); });\n", "\n", " node.append(\"circle\")\n", " .attr(\"r\", function(d) { return d.r; });\n", "\n", " node.filter(function(d) { return !d.children; }).append(\"text\")\n", " .attr(\"dy\", \".3em\")\n", " .style(\"text-anchor\", \"middle\")\n", " .text(function(d) { return d.name.substring(0, d.r / 3); });\n", "});\n", "\n", "d3.select(self.frameElement).style(\"height\", diameter + \"px\");\n", "\"\"\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Pandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pandas makes use of this capability to allow `DataFrames` to be represented as HTML tables." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import pandas" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, `DataFrames` will be represented as text; to enable HTML representations we need to set a print option:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "pandas.set_option('display.notebook_repr_html',True)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is a small amount of stock data for APPL:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%file data.csv\n", "Date,Open,High,Low,Close,Volume,Adj Close\n", "2012-06-01,569.16,590.00,548.50,584.00,14077000,581.50\n", "2012-05-01,584.90,596.76,522.18,577.73,18827900,575.26\n", "2012-04-02,601.83,644.00,555.00,583.98,28759100,581.48\n", "2012-03-01,548.17,621.45,516.22,599.55,26486000,596.99\n", "2012-02-01,458.41,547.61,453.98,542.44,22001000,540.12\n", "2012-01-03,409.40,458.24,409.00,456.48,12949100,454.53" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read this as into a `DataFrame`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "df = pandas.read_csv('data.csv')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And view the HTML representation:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "df" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "SymPy" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load_ext sympy.interactive.ipythonprinting\n", "# If you are using older versions of SymPy use:\n", "# %load_ext sympyprinting" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from __future__ import division\n", "import sympy as sym\n", "from sympy import *\n", "x, y, z = symbols(\"x y z\")\n", "k, m, n = symbols(\"k m n\", integer=True)\n", "f, g, h = map(Function, 'fgh')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "Rational(3,2)*pi + exp(I*x) / (x**2 + y)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1/x + (x*sin(x) - 1)/x\n", "a" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "(1/cos(x)).series(x, 0, 6)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "External sites" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can even embed an entire page from another site in an iframe; for example this is today's Wikipedia\n", "page for mobile users:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "HTML('')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Go to [SoundCloud](http://soundcloud.com) 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." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load soln/soundcloud.py" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "LaTeX" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And we also support the display of mathematical expressions typeset in LaTeX, which is rendered\n", "in the browser thanks to the [MathJax library](http://mathjax.org)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import Math\n", "Math(r'F(k) = \\int_{-\\infty}^{\\infty} f(x) e^{2\\pi i k} dx')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With the `Latex` class, you have to include the delimiters yourself. This allows you to use other LaTeX modes such as `eqnarray`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import Latex\n", "Latex(r\"\"\"\\begin{eqnarray}\n", "\\nabla \\times \\vec{\\mathbf{B}} -\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{E}}}{\\partial t} & = \\frac{4\\pi}{c}\\vec{\\mathbf{j}} \\\\\n", "\\nabla \\cdot \\vec{\\mathbf{E}} & = 4 \\pi \\rho \\\\\n", "\\nabla \\times \\vec{\\mathbf{E}}\\, +\\, \\frac1c\\, \\frac{\\partial\\vec{\\mathbf{B}}}{\\partial t} & = \\vec{\\mathbf{0}} \\\\\n", "\\nabla \\cdot \\vec{\\mathbf{B}} & = 0 \n", "\\end{eqnarray}\"\"\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }