{ "cells": [ { "cell_type": "markdown", "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": "markdown", "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import display" ] }, { "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import display_pretty, display_html, display_jpeg, display_png, display_json, display_latex, display_svg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Images" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To work with images (JPEG, PNG) use the `Image` class." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import Image" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "i = Image(url='img/python-logo.gif')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Returning an `Image` object from an expression will automatically display it:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "i" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or you can pass it to `display`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "display(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An image can also be displayed from raw data or a url" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Image(url='http://python.org/images/python-logo.gif')" ] }, { "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import SVG\n", "SVG(filename='img/python-logo.svg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Links to local files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we want to create a link to one of them, we can call use the `FileLink` object." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import FileLink, FileLinks\n", "FileLink('Part 1 - Running Code.ipynb')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, if we want to link to all of the files in a directory, we can use the `FileLinks` object, passing `'.'` to indicate that we want links generated for the current working directory. Note that if there were other directories under the current directory, `FileLinks` would work in a recursive manner creating links to files in all sub-directories as well." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "FileLinks('.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Embedded vs Non-embedded Images" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, image data is embedded in the Notebook document so that the images can be viewed offline. However it is also possible to tell the `Image` class to only store a *link* to the image. Let's see how this works using a webcam at Berkeley." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import Image\n", "\n", "# by default Image data are embedded\n", "Embed = Image( 'https://upload.wikimedia.org/wikipedia/commons/1/1e/Stonehenge.jpg')\n", "\n", "# if kwarg `url` is given, the embedding is assumed to be false\n", "SoftLinked = Image(url='https://upload.wikimedia.org/wikipedia/commons/1/1e/Stonehenge.jpg')\n", "\n", "# In each case, embed can be specified explicitly with the `embed` kwarg\n", "# ForceEmbed = Image(url='http://scienceview.berkeley.edu/view/images/newview.jpg', embed=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is the embedded version. Note that this image was pulled from the webcam when this code cell was originally run and stored in the Notebook. Unless we rerun this cell, this is not todays image." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Embed" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is today's image from same webcam at Berkeley, (refreshed every minutes, if you reload the notebook), visible only with an active internet connection, that should be different from the previous one. Notebooks saved with this kind of image will be lighter and always reflect the current version of the source, but the image won't display offline." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "SoftLinked" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course, if you re-run this Notebook, the two images will be the same again." ] }, { "cell_type": "markdown", "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import YouTubeVideo\n", "# a talk about IPython at Sage Days at U. Washington, Seattle.\n", "# Video credit: William Stein.\n", "YouTubeVideo('1j_HxD4iLn8')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the nascent video capabilities of modern browsers, you may also be able to display local\n", "videos. At the moment this doesn't work very well in all browsers, so it may or may not work for you;\n", "we will continue testing this and looking for ways to make it more robust. \n", "\n", "The following cell loads a local file called `animation.m4v`, encodes the raw video as base64 for http\n", "transport, and uses the HTML5 video tag to load it. On Chrome 15 it works correctly, displaying a control\n", "bar at the bottom with a play/pause button and a location slider." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import HTML\n", "from base64 import b64encode\n", "video = open(\"img/animation.m4v\", \"rb\").read()\n", "video_encoded = b64encode(video)\n", "video_tag = '