#!/usr/bin/env python # coding: utf-8 # # ![Py4Eng](img/logo.png) # # # Jupyter Notebook: Rich display # ## Yoav Ram # # Introduction to the notebook # # We start with a short presentation by [Fernando Perez](http://fperez.org/), founder of the IPython project. # We use the notebook's rich display system to show the presentation - we'll how understand to do that soon. # In[1]: from IPython.display import HTML HTML('') # # Rich display system # # The notebook has a rich display system that uses the fact that the notebook runs in a browser - an multipotent rendering program capable of running JavaScript and specializing in interactive, graphical user interfaces. # # The notebook allows objects to declare rich representations using different formats: # # - HTML # - JSON # - PNG # - JPEG # - SVG # - LaTeX # - YouTubeVideo # # A single object can declare some or all of these representations; all are handled by the notebook display system. # # This section follows [Wojciech Jaśkowski's course notebook](http://www.cs.put.poznan.pl/wjaskowski/pub/teaching/kck/lectures/notebooks/ipython-notebook.html). # # ## Basic display imports # # The display function is a general purpose tool for displaying different representations of objects. Think of it as print for these rich representations. # # In[2]: from IPython.display import display # A few points: # # - Calling display on an object will send all possible representations to the Notebook. # - These representations are stored in the Notebook document. # - In general the Notebook will use the richest available representation. # # If you want to display a particular representation, there are specific functions for that: # In[3]: from IPython.display import display_pretty, display_html, display_jpeg, display_png, display_json, display_latex, display_svg # ## Images # # To work with images (JPEG, PNG) use the Image class. # # In[4]: from IPython.display import Image # Returning an Image object from an expression will automatically display it: # In[5]: i = Image(r'img\logo.png') i # # # Or you can pass it to display: # # In[6]: display(i) # # # SVG images are also supported out of the box (since modern browsers do a good job of rendering them). Note that images can be loaded with a URL instead of a filename. # # In[12]: from IPython.display import SVG SVG('https://www.python.org/static/community_logos/python-logo-generic.svg') # # ## Links to local files # # If we want to create a link to one of them, we can call use the FileLink object. # # In[7]: from IPython.display import FileLink, FileLinks FileLink('if-while.ipynb') # # # 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. # # In[8]: FileLinks('.') # ## Embedded vs Non-embedded Images # # 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. # # By default Image data are embedded; 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. # # In[4]: berkeley_url = 'http://static.lawrencehallofscience.org/scienceview/scienceview.berkeley.edu/html/view/view_assets/images/newview.jpg' embed = Image(berkeley_url) embed # If kwarg `url` is given, the embedding is assumed to be false. # # 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 (unless you just re-run the previous cell). # # Notebooks saved with this kind of image will be take less space and always reflect the current version of the source, but the image won't display offline. # # In[8]: soft_linked = Image(url=berkeley_url) soft_linked # Of course, if you re-run this Notebook, the two images will be the same again. # # # In each case, embed can be specified explicitly with the `embed` kwarg: # In[9]: force_embed = Image(url=berkeley_url, embed=True) force_embed # ## Video # # 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): # # In[27]: from IPython.display import YouTubeVideo # a talk about IPython at Sage Days at U. Washington, Seattle. # Video credit: William Stein. YouTubeVideo('1j_HxD4iLn8') # # # Using the nascent video capabilities of modern browsers, you may also be able to display local videos. At the moment this doesn't work very well in all browsers, so it may or may not work for you; we will continue testing this and looking for ways to make it more robust. # # The following cell plays a local file called wildlife.mp4 using the HTML5 video tag. This might not work on older browsers (tested with [Firefox](https://www.mozilla.org/en-US/firefox) 43). # In[30]: HTML(""" """) # # ## HTML # # 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. # # In[31]: s = """
Header 1 | Header 2 |
---|---|
row 1, cell 1 | row 1, cell 2 |
row 2, cell 1 | row 2, cell 2 |