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]:
pwd
Out[1]:
In [2]:
ls
In [3]:
message = 'The IPython notebook is great!'
# note: the echo command does not run on Windows, it's a unix command.
!echo $message
Plots with matplotlib: do not execute the next below if you do not have matplotlib installed or didn't call the %matplolib magic, as the code will not work.
In [4]:
%matplotlib inline
In [5]:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 3*np.pi, 500)
plt.plot(x, np.sin(x**2))
plt.title('A simple chirp');
You can paste blocks of input with prompt markers, such as those from the official Python tutorial
In [6]:
>>> the_world_is_flat = 1
>>> if the_world_is_flat:
... print "Be careful not to fall off!"
Errors are shown in informative ways:
In [7]:
%run non_existent_file
In [8]:
x = 1
y = 4
z = y/(1-x)
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 [9]:
magic
In [10]:
import time, sys
for i in range(8):
print i,
time.sleep(0.5)
In [ ]:
from ctypes import CDLL
# This will crash a linux system; equivalent calls can be made on Windows or Mac
libc = CDLL("libc.so.6")
libc.time(-1) # BOOM!!
Courtesy of MathJax, you can include mathematical expressions both inline: $e^{i\pi} + 1 = 0$ and displayed:
$$e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i$$
In [11]:
from IPython.core.display import Image
Image(filename='logo.png')
Out[11]:
An image can also be displayed from raw data or a url
In [12]:
Image('http://python.org/static/img/python-logo-large.png')
Out[12]:
SVG images are also supported out of the box (since modern browsers do a good job of rendering them):
In [13]:
from IPython.core.display import SVG
SVG(filename='python-logo.svg')
Out[13]:
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 [1]:
from IPython.lib.display import YouTubeVideo
# a talk about IPython at Sage Days at U. Washington, Seattle.
# Video credit: William Stein.
YouTubeVideo('1j_HxD4iLn8')
Out[1]:
In [15]:
from IPython.core.display import HTML
HTML('<iframe src=http://en.mobile.wikipedia.org/?useformat=mobile width=700 height=350>')
Out[15]:
And we also support the display of mathematical expressions typeset in LaTeX, which is rendered in the browser thanks to the MathJax library.
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:
In [16]:
from IPython.core.display import Math
Math(r'$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$')
Out[16]:
.py in the dashboard%loadpy with any local or remote url: the Matplotlib Gallery!In this notebook we've kept the output saved so you can see the result, but you should run the next cell yourself (with an active internet connection).
In [17]:
%loadpy http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/integral_demo.py
In [18]:
#!/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)] + list(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()