A brief tour of the IPython notebook ==================================== This document will give you a brief tour of the capabilities of the IPython notebook. You can view its contents by scrolling around, or execute each cell by typing ``Shift-Enter``. After you conclude this brief high-level tour, you should read the accompanying notebook titled ``01_notebook_introduction``, which takes a more step-by-step approach to the features of the system. The rest of the notebooks in this directory illustrate various other aspects and capabilities of the IPython notebook; some of them may require additional libraries to be executed. **NOTE:** This notebook *must* be run from its own directory, so you must ``cd`` to this directory and then start the notebook, but do *not* use the ``--notebook-dir`` option to run it from another location. The first thing you need to know is that you are still controlling the same old IPython you're used to, so things like shell aliases and magic commands still work: In[1]: .. code:: python pwd Out[1]: .. parsed-literal:: u'/Users/minrk/dev/ip/mine/docs/examples/notebooks' In[2]: .. code:: python ls .. parsed-literal:: 00_notebook_tour.ipynb callbacks.ipynb python-logo.svg 01_notebook_introduction.ipynb cython_extension.ipynb rmagic_extension.ipynb Animations_and_Progress.ipynb display_protocol.ipynb sympy.ipynb Capturing Output.ipynb formatting.ipynb sympy_quantum_computing.ipynb Script Magics.ipynb octavemagic_extension.ipynb trapezoid_rule.ipynb animation.m4v progbar.ipynb In[3]: .. code:: python message = 'The IPython notebook is great!' # note: the echo command does not run on Windows, it's a unix command. !echo $message .. parsed-literal:: The IPython notebook is great! Plots with matplotlib --------------------- IPython adds an 'inline' matplotlib backend, which embeds any matplotlib figures into the notebook. In[4]: .. code:: python %pylab inline .. parsed-literal:: Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline]. For more information, type 'help(pylab)'. In[5]: .. code:: python x = linspace(0, 3*pi, 500) plot(x, sin(x**2)) title('A simple chirp'); .. image:: tests/ipynbref/00_notebook_tour_orig_files/00_notebook_tour_orig_fig_00.png You can paste blocks of input with prompt markers, such as those from `the official Python tutorial `_ In[6]: .. code:: python >>> the_world_is_flat = 1 >>> if the_world_is_flat: ... print "Be careful not to fall off!" .. parsed-literal:: Be careful not to fall off! Errors are shown in informative ways: In[7]: .. code:: python %run non_existent_file .. parsed-literal:: ERROR: File `u'non_existent_file.py'` not found. In[8]: .. code:: python x = 1 y = 4 z = y/(1-x) :: --------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) in () 1 x = 1 2 y = 4 ----> 3 z = y/(1-x) ZeroDivisionError: integer division or modulo by zero When IPython needs to display additional information (such as providing details on an object via ``x?`` it will automatically invoke a pager at the bottom of the screen: In[18]: .. code:: python magic Non-blocking output of kernel ----------------------------- If you execute the next cell, you will see the output arriving as it is generated, not all at the end. In[19]: .. code:: python import time, sys for i in range(8): print i, time.sleep(0.5) .. parsed-literal:: 0 .. parsed-literal:: 1 .. parsed-literal:: 2 .. parsed-literal:: 3 .. parsed-literal:: 4 .. parsed-literal:: 5 .. parsed-literal:: 6 .. parsed-literal:: 7 Clean crash and restart ----------------------- We call the low-level system libc.time routine with the wrong argument via ctypes to segfault the Python interpreter: In[*]: .. code:: python import sys from ctypes import CDLL # This will crash a Linux or Mac system; equivalent calls can be made on Windows dll = 'dylib' if sys.platform == 'darwin' else '.so.6' libc = CDLL("libc.%s" % dll) libc.time(-1) # BOOM!! Markdown cells can contain formatted text and code -------------------------------------------------- You can *italicize*, **boldface** - build - lists and embed code meant for illustration instead of execution in Python: :: def f(x): """a docstring""" return x**2 or other languages: :: if (i=0; i An image can also be displayed from raw data or a url In[2]: .. code:: python Image(url='http://python.org/images/python-logo.gif') Out[2]: .. parsed-literal:: SVG images are also supported out of the box (since modern browsers do a good job of rendering them): In[3]: .. code:: python from IPython.display import SVG SVG(filename='python-logo.svg') Out[3]: .. parsed-literal:: Embedded vs Non-embedded Images ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ As of IPython 0.13, images are embedded by default for compatibility with QtConsole, and the ability to still be displayed offline. Let's look at the differences: In[4]: .. code:: python # by default Image data are embedded Embed = Image( 'http://scienceview.berkeley.edu/view/images/newview.jpg') # if kwarg `url` is given, the embedding is assumed to be false SoftLinked = Image(url='http://scienceview.berkeley.edu/view/images/newview.jpg') # In each case, embed can be specified explicitly with the `embed` kwarg # ForceEmbed = Image(url='http://scienceview.berkeley.edu/view/images/newview.jpg', embed=True) Today's image from a webcam at Berkeley, (at the time I created this notebook). This should also work in the Qtconsole. Drawback is that the saved notebook will be larger, but the image will still be present offline. In[5]: .. code:: python Embed Out[5]: .. parsed-literal:: Today's image from same webcam at Berkeley, (refreshed every minutes, if you reload the notebook), visible only with an active internet connexion, that should be different from the previous one. This will not work on Qtconsole. Notebook 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. In[6]: .. code:: python SoftLinked Out[6]: .. parsed-literal:: Of course, if you re-run the all notebook, the two images will be the same again. Video ~~~~~ And 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[7]: .. code:: python from IPython.display import YouTubeVideo # a talk about IPython at Sage Days at U. Washington, Seattle. # Video credit: William Stein. YouTubeVideo('1j_HxD4iLn8') Out[7]: .. parsed-literal:: 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 loads a local file called ``animation.m4v``, encodes the raw video as base64 for http transport, and uses the HTML5 video tag to load it. On Chrome 15 it works correctly, displaying a control bar at the bottom with a play/pause button and a location slider. In[8]: .. code:: python from IPython.display import HTML video = open("animation.m4v", "rb").read() video_encoded = video.encode("base64") video_tag = '