{ "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 = \"\"\"
| Header 1 | \n", "Header 2 | \n", "
|---|---|
| row 1, cell 1 | \n", "row 1, cell 2 | \n", "
| row 2, cell 1 | \n", "row 2, cell 2 | \n", "