# 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:
pwd
u'/Users/minrk/dev/ip/mine/docs/examples/notebooks'
ls
message = 'The IPython notebook is great!'
# note: the echo command does not run on Windows, it's a unix command.
!echo $message
%pylab inline
x = linspace(0, 3*pi, 500)
plot(x, sin(x**2))
title('A simple chirp');
>>> the_world_is_flat = 1
>>> if the_world_is_flat:
... print "Be careful not to fall off!"
%run non_existent_file
x = 1
y = 4
z = y/(1-x)
magic
import time, sys
for i in range(8):
print i,
time.sleep(0.5)
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!!
from IPython.display import Image
Image(filename='../../source/_static/logo.png')
An image can also be displayed from raw data or a url
Image(url='http://python.org/images/python-logo.gif')
SVG images are also supported out of the box (since modern browsers do a good job of rendering them):
from IPython.display import SVG
SVG(filename='python-logo.svg')
#### 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:
# 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)
Embed
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.
SoftLinked
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):
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 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.
from IPython.display import HTML
video = open("animation.m4v", "rb").read()
video_encoded = video.encode("base64")
video_tag = '<video controls alt="test" src="data:video/x-m4v;base64,{0}">'.format(video_encoded)
HTML(data=video_tag)
## Local Files
The above examples embed images and video from the notebook filesystem in the output
areas of code cells. It is also possible to request these files directly in markdown cells
if they reside in the notebook directory via relative urls prefixed with `files/`:
files/[subdirectory/]ls
from IPython.display import FileLink
FileLink('00_notebook_tour.ipynb')
Alternatively, if we want to link to all of them, 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.
from IPython.display import FileLinks
FileLinks('.')
### External sites You can even embed an entire page from another site in an iframe; for example this is today's Wikipedia page for mobile users:
HTML('<iframe src=http://en.mobile.wikipedia.org/?useformat=mobile width=700 height=350></iframe>')
### Mathematics And we also support the display of mathematical expressions typeset in LaTeX, which is rendered in the browser thanks to the [MathJax library](http://mathjax.org). Note that this is *different* from the above examples. Above we were typing mathematical expressions in Markdown cells (along with normal text) and letting the browser render them; now we are displaying the output of a Python computation as a LaTeX expression wrapped by the `Math()` object so the browser renders it. The `Math` object will add the needed LaTeX delimiters (`$$`) if they are not provided:
from IPython.display import Math
Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')
With the `Latex` class, you have to include the delimiters yourself. This allows you to use other LaTeX modes such as `eqnarray`:
from IPython.display import Latex
Latex(r"""\begin{eqnarray}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{eqnarray}""")
Or you can enter latex directly with the `%%latex` cell magic:
%%latex
\begin{aligned}
\nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} & = 0
\end{aligned}
%pylab inline
%load http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/integral_demo.py
#!/usr/bin/env python
# implement the example graphs/integral from pyx
from pylab import *
from matplotlib.patches import Polygon
def func(x):
return (x-3)*(x-5)*(x-7)+85
ax = subplot(111)
a, b = 2, 9 # integral area
x = arange(0, 10, 0.01)
y = func(x)
plot(x, y, linewidth=1)
# make the shaded region
ix = arange(a, b, 0.01)
iy = func(ix)
verts = [(a,0)] + zip(ix,iy) + [(b,0)]
poly = Polygon(verts, facecolor='0.8', edgecolor='k')
ax.add_patch(poly)
text(0.5 * (a + b), 30,
r"$\int_a^b f(x)\mathrm{d}x$", horizontalalignment='center',
fontsize=20)
axis([0,10, 0, 180])
figtext(0.9, 0.05, 'x')
figtext(0.1, 0.9, 'y')
ax.set_xticks((a,b))
ax.set_xticklabels(('a','b'))
ax.set_yticks([])
show()