Sign In
In [1]:
%load_ext load_style
%load_style talk.css

Overview of the IPython notebook

In [2]:
from IPython.display import display, HTML

Python ships with a basic interactive interpreter ... not much fun here

IPython is an enhanced interactive interpreter, developped for scientific development, adding e.g. history, tab-completion, interactive help, and lots and lots of goodies:

Since 2005, the IPython development team has introduced the IPython notebook. This is a web application, running in the browser, that is connected to a Python kernel running in the background.

In [3]:
HTML('<iframe src= width=900 height=350></iframe>')

It behaves as an interactive notebook, in which you can weave Python code and outputs, figures generated from Python / matplotlib, images (either local and remote), websites, videos and richly formatted comments using markdown, which is a superset of HTML with a very simple syntax (see here for more)

It is structured into executable cells, into which by default you can run arbitrary python code

It has a sophisticated tab completion and help system

The power of IPython comes in part because of its numerous extensions and magic functions

And finally you can export a notebook in different formats, including HTML and Latex (and PDF).

Recently the IPython notebook as been featured in the toolbox section of Nature

In [4]:
HTML('<iframe src= width=1000 height=350></iframe>')

Using the IPython notebook

To start an IPython notebook session open a command prompt, navigate to a desired working directory then issue the following command:

$ ipython notebook

A new window open in your browser where you can open an existing notebook or start a new one. Notebooks are organized with cells. You may have a code cell for inputing commands followed by its result cell which contains the output of the code. You may also have a text cell, such as this one you're reading right now. Cell type can be changed in the above dropdown menu.

There is the menubar above for navigating a notebook but you will find the following shortcuts helpful:

  • Enter : Enter the edit mode of a cell
  • Esc : Leave the edit mode and get into the control mode (see icons on the right of the menu bar)
  • Shift + Enter : Execute cell and advance to next cell
  • Alt + Enter : Execute cell and insert an empty cell below
  • Ctrl + Enter : Execute cell in place (do not advance to the next cell)
  • Press esc (command mode) then h to display keyboard shortcuts

At times you might run code that gets stuck in an infinite loop or simply you want to clear all your workspace variables and start over. To do that you can do:

Kernel -> Interrupt

Kernel -> Restart

In [6]:
### This is a code cell, containing python code, it is executed by <shift>-<enter> or <alt>-<enter>

def func(x):
    return x**3

[func(x) for x in xrange(10)]
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]

Getting help

IPython has a sophisticated help and tab completion system, which allows introspection: i.e. If you want details regarding the properties and functionality of any Python objects currently loaded into IPython, you can use the ? to reveal any details that are available:

In [7]:
some_dict = {}
In [10]:
from numpy import random 
In [11]:
import numpy
In [ ]:
help(random) # produces a long output below instead of in a dedicated frame. <CTRL>-<M> <D>

Tab completion

Because IPython allows for introspection, it is able to afford the user the ability to tab-complete commands or objects that have been partially typed. This is done by pressing the <tab> key at any point during the process of typing a command:

In [14]:
listA = [1, 2., 'sentence', 1, (1,2), {'answer': 42}] # here I construct a LIST containing different items
In [16]:
In [17]:

including markdown comments

A short introduction to markdown syntax

Using markdown cells, you can insert formatted comments, observations ... directly in between executable code and outputs ...

To toggle from code cell (the default) to markdown cell, you can use the toolbar, or <ctrl>-<m> + <m> (<command>-<m> + <m> on Macs)

You can italicize, boldface

  • build

  • lists

  1. enumerate

  2. stuff

and embed syntax highlighted code meant for illustration instead of execution in Python:

def f(x):
    """a docstring"""
    return x**2

or other languages:

/* Fibonacci Series c language */
int main()
   int n, first = 0, second = 1, next, c;
   printf("Enter the number of terms\n");
   printf("First %d terms of Fibonacci series are :-\n",n);
   for ( c = 0 ; c < n ; c++ )
      if ( c <= 1 )
         next = c;
         next = first + second;
         first = second;
         second = next;
   return 0;

You can insert images seemlessly into a Markdown cell, but if it is local instead of on the web, it needs to reside in an images folder in the same directory as your notebook.

The syntax is then

![optional tag](files/images/image.png)



Thanks to MathJax, you can include mathematical expressions in a markdown cell using Latex syntax both inline and displayed:

For example $e^{i\pi} + 1 = 0$ becomes \(e^{i\pi} + 1 = 0\)


$$e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i$$


\[e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i\]

Mathjax is a JavaScript library, and by default IPython uses the online version, from the Mathjax Content Distribution Network (CDN), if you want to be able to use it offline, you need to install MathJax locally by running that (once) in a code cell:

from IPython.external.mathjax import install_mathjax

The IPython notebook rich display system

In [18]:
from IPython.display import Image, YouTubeVideo
In [19]:
### that will display a frame with specified width and height ... 

HTML('<iframe src="" height=300 width=600>'
In [20]:
### This inserts an image in the output cell

Notebook Image
In [21]:
### This embeds a video from YouTube
### You can embed Vimeo videos by calling from IPython.display import VimeoVideo

# Fernando Pérez at PyConCA, here I start at 30 minutes 
YouTubeVideo('F4rFuIb1Ie4', start = 30*60)  
In [22]:
### You can also embed a LOCAL video in the notebook, but have to encode it: Not sure it will work 
### on Windows ...

import io
import base64

filename = './data/Monty_Python.mp4'

video =, 'r+b').read()
encoded = base64.b64encode(video)
HTML(data='''<video alt="A witch!" controls>
           <source src="data:video/mp4;base64,{0}" type="video/mp4" />

Inline plots with matplotlib

In [23]:
import numpy as np
import matplotlib.pyplot as plt
In [24]:
# this is a magic IPython command (%) allowing matplotlib plots to be displayed inline in the notebook 
%matplotlib inline
In [25]:
t = np.linspace(0, 2*np.pi, 100)
[<matplotlib.lines.Line2D at 0x10b403690>]
Notebook Image
In [28]:
# you can also choose to display figure OUTSIDE the notebook
# the available backends are [inline|qt|osx|gtx] ... For Windows try qt
%matplotlib osx 
In [29]:
t = np.linspace(0, 2*np.pi, 100)
[<matplotlib.lines.Line2D at 0x10d61a290>]

from version 1.4 of Matplotlib, a special interactive backend for the IPython notebook is available

You need to restart the kernel and then run the following cell for it to work

In [ ]:
import matplotlib
In [30]:
%matplotlib inline

Some useful magic commands in IPython

In [31]:
# for a list of available magic command 

  "cell": {
    "prun": "ExecutionMagics",
    "file": "Other",
    "!": "OSMagics",
    "capture": "ExecutionMagics",
    "timeit": "ExecutionMagics",
    "script": "ScriptMagics",
    "pypy": "Other",
    "system": "OSMagics",
    "perl": "Other",
    "HTML": "Other",
    "bash": "Other",
    "python": "Other",
    "SVG": "Other",
    "javascript": "DisplayMagics",
    "writefile": "OSMagics",
    "ruby": "Other",
    "python3": "Other",
    "python2": "Other",
    "latex": "DisplayMagics",
    "sx": "OSMagics",
    "svg": "DisplayMagics",
    "html": "DisplayMagics",
    "sh": "Other",
    "time": "ExecutionMagics",
    "debug": "ExecutionMagics"
  "line": {
    "psource": "NamespaceMagics",
    "load_style": "Other",
    "logstart": "LoggingMagics",
    "popd": "OSMagics",
    "loadpy": "CodeMagics",
    "install_ext": "ExtensionMagics",
    "colors": "BasicMagics",
    "who_ls": "NamespaceMagics",
    "lf": "Other",
    "install_profiles": "DeprecatedMagics",
    "ll": "Other",
    "pprint": "BasicMagics",
    "lk": "Other",
    "ls": "Other",
    "save": "CodeMagics",
    "tb": "ExecutionMagics",
    "lx": "Other",
    "pylab": "PylabMagics",
    "killbgscripts": "ScriptMagics",
    "quickref": "BasicMagics",
    "magic": "BasicMagics",
    "dhist": "OSMagics",
    "edit": "KernelMagics",
    "logstop": "LoggingMagics",
    "gui": "BasicMagics",
    "alias_magic": "BasicMagics",
    "debug": "ExecutionMagics",
    "page": "BasicMagics",
    "logstate": "LoggingMagics",
    "ed": "Other",
    "pushd": "OSMagics",
    "timeit": "ExecutionMagics",
    "rehashx": "OSMagics",
    "hist": "Other",
    "qtconsole": "KernelMagics",
    "rm": "Other",
    "dirs": "OSMagics",
    "run": "ExecutionMagics",
    "reset_selective": "NamespaceMagics",
    "rep": "Other",
    "pinfo2": "NamespaceMagics",
    "matplotlib": "PylabMagics",
    "unload_ext": "ExtensionMagics",
    "doctest_mode": "KernelMagics",
    "logoff": "LoggingMagics",
    "reload_ext": "ExtensionMagics",
    "pdb": "ExecutionMagics",
    "load": "CodeMagics",
    "lsmagic": "BasicMagics",
    "autosave": "KernelMagics",
    "cd": "OSMagics",
    "pastebin": "CodeMagics",
    "prun": "ExecutionMagics",
    "cp": "Other",
    "autocall": "AutoMagics",
    "bookmark": "OSMagics",
    "connect_info": "KernelMagics",
    "mkdir": "Other",
    "system": "OSMagics",
    "whos": "NamespaceMagics",
    "rmdir": "Other",
    "automagic": "AutoMagics",
    "store": "StoreMagics",
    "more": "KernelMagics",
    "pdef": "NamespaceMagics",
    "precision": "BasicMagics",
    "pinfo": "NamespaceMagics",
    "pwd": "OSMagics",
    "psearch": "NamespaceMagics",
    "reset": "NamespaceMagics",
    "recall": "HistoryMagics",
    "xdel": "NamespaceMagics",
    "xmode": "BasicMagics",
    "cat": "Other",
    "mv": "Other",
    "rerun": "HistoryMagics",
    "logon": "LoggingMagics",
    "history": "HistoryMagics",
    "pycat": "OSMagics",
    "unalias": "OSMagics",
    "install_default_config": "DeprecatedMagics",
    "env": "OSMagics",
    "load_ext": "ExtensionMagics",
    "config": "ConfigMagics",
    "profile": "BasicMagics",
    "pfile": "NamespaceMagics",
    "less": "KernelMagics",
    "who": "NamespaceMagics",
    "notebook": "BasicMagics",
    "man": "KernelMagics",
    "sx": "OSMagics",
    "macro": "ExecutionMagics",
    "clear": "KernelMagics",
    "alias": "OSMagics",
    "time": "ExecutionMagics",
    "sc": "OSMagics",
    "ldir": "Other",
    "pdoc": "NamespaceMagics"

For example


list all variables (incl. classes, functions, etc) in the namespace

In [32]:
Variable Type Data/Info ------------------------------------ HTML type <class 'IPython.core.display.HTML'> Image type <class 'IPython.core.display.Image'> YouTubeVideo type <class 'IPython.lib.display.YouTubeVideo'> base64 module <module 'base64' from '/U<...>ib/python2.7/base64.pyc'> display function <function display at 0x1031971b8> encoded str AAAAGGZ0eXBtcDQyAAAAAGlzb<...>bIFcAVnAAAAAHQZtAVwBWcA== filename str ./data/Monty_Python.mp4 func function <function func at 0x104a06e60> io module <module 'io' from '/Users<...>da/lib/python2.7/io.pyc'> listA list n=6 np module <module 'numpy' from '/Us<...>ages/numpy/__init__.pyc'> numpy module <module 'numpy' from '/Us<...>ages/numpy/__init__.pyc'> plt module <module 'matplotlib.pyplo<...>s/matplotlib/pyplot.pyc'> random module <module 'numpy.random' fr<...>mpy/random/__init__.pyc'> some_dict dict n=0 t ndarray 100: 100 elems, type `float64`, 800 bytes video str ftypmp42isommp42<...>VpA� WVpA�@WVp x int 9

Writing the content of a cell to file

In [34]:
import numpy as np

print('Hello world')

a = np.arange(10) ### will be available in the namespace 

Running some external python script (local or online)

In [36]:
Hello world
In [37]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Loading the content of some local python script

you can load actually anything, e.g. a Markdown file

In [38]:
In [ ]:
import numpy as np

print('Hello world')

a = np.arange(10) ### will be available in the namespace 
works as well for scripts available online
In [39]:
### loading an example from the matplotlib gallery

In [42]:
Demo of the `streamplot` function.

A streamplot, or streamline plot, is used to display 2D vector fields. This
example shows a few features of the stream plot function:

    * Varying the color along a streamline.
    * Varying the density of streamlines.
    * Varying the line width along a stream line.
import numpy as np
import matplotlib.pyplot as plt

Y, X = np.mgrid[-3:3:100j, -3:3:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U*U + V*V)

plt.streamplot(X, Y, U, V, color=U, linewidth=2,

f, (ax1, ax2) = plt.subplots(ncols=2)
ax1.streamplot(X, Y, U, V, density=[0.5, 1])

lw = 5*speed/speed.max()
ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)


Interacting with the OS: the IPython notebook as an enhanced shell

In [43]:
!rm  ### ! escapes to the OS (!del on windows)
In [45]:
!ls *.py
In [46]:
notebooks = !ls *.ipynb ### notebooks is a python list ... not sure how to use wildcards on Windows
In [50]:

And if you need to actually quickly put together and run a bash script (linux / Mac), you don't need to escape the IPython notebook thanks to the


cell magic ...

In [51]:

# Substitutes underscores for blanks in all the filenames in a directory.

ONE=1                     # For getting singular/plural right (see below).
number=0                  # Keeps track of how many files actually renamed.
FOUND=0                   # Successful return value.

for filename in *         # Traverses all files in directory.
     echo "$filename" | grep -q " "         # Checks whether filename
     if [ $? -eq $FOUND ]                   # contains space(s).
       fname=$filename                      # Strips off path.
       n=`echo $fname | sed -e "s/ /_/g"`   # Substitutes underscore for blank.
       mv "$fname" "$n"                     # Do the actual renaming.
       let "number += 1"

if [ "$number" -eq "$ONE" ]                 # For correct grammar.
 echo "$number file renamed."
 echo "$number files renamed."

exit 0
0 files renamed.

Exporting your notebook in other formats

A notebook (extension .ipynb) is actually just a JSON file, using built-in converters you can convert a notebook into a variety of formats for sharing, illustration, publishing, etc.

In [52]:
#!ipython nbconvert --help
!ipython  nbconvert --help
This application is used to convert notebook files (*.ipynb) to various other formats. WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES. Options ------- Arguments that take values are actually convenience aliases to full Configurables, whose aliases are listed on the help line. For more information on full configurables, see '--help-all'. --debug set log level to logging.DEBUG (maximize logging output) --init Initialize profile with default config files. This is equivalent to running `ipython profile create <profile>` prior to startup. --quiet set log level to logging.CRITICAL (minimize logging output) --stdout Write notebook output to stdout instead of files. --profile=<Unicode> (BaseIPythonApplication.profile) Default: u'default' The IPython profile to use. --reveal-prefix=<Unicode> (RevealHelpPreprocessor.url_prefix) Default: 'reveal.js' The URL prefix for reveal.js. This can be a a relative URL for a local copy of reveal.js, or point to a CDN. For speaker notes to work, a local reveal.js prefix must be used. --ipython-dir=<Unicode> (BaseIPythonApplication.ipython_dir) Default: u'' The name of the IPython directory. This directory is used for logging configuration (through profiles), history storage, etc. The default is usually $HOME/.ipython. This options can also be specified through the environment variable IPYTHONDIR. --writer=<DottedObjectName> (NbConvertApp.writer_class) Default: 'FilesWriter' Writer class used to write the results of the conversion --log-level=<Enum> (Application.log_level) Default: 30 Choices: (0, 10, 20, 30, 40, 50, 'DEBUG', 'INFO', 'WARN', 'ERROR', 'CRITICAL') Set the log level by value or name. --to=<CaselessStrEnum> (NbConvertApp.export_format) Default: 'html' Choices: ['custom', 'html', 'latex', 'markdown', 'python', 'rst', 'slides'] The export format to be used. --template=<Unicode> (TemplateExporter.template_file) Default: u'default' Name of the template file to use --output=<Unicode> (NbConvertApp.output_base) Default: '' overwrite base name use for output files. can only be use when converting one notebook at a time. --post=<DottedOrNone> (NbConvertApp.postprocessor_class) Default: u'' PostProcessor class used to write the results of the conversion --config=<Unicode> (BaseIPythonApplication.extra_config_file) Default: u'' Path to an extra config file to load. If specified, load this config file in addition to any other IPython config. --profile-dir=<Unicode> (ProfileDir.location) Default: u'' Set the profile location directly. This overrides the logic used by the `profile` option. To see all available configurables, use `--help-all` Examples -------- The simplest way to use nbconvert is > ipython nbconvert mynotebook.ipynb which will convert mynotebook.ipynb to the default format (probably HTML). You can specify the export format with `--to`. Options include ['custom', 'html', 'latex', 'markdown', 'python', 'rst', 'slides'] > ipython nbconvert --to latex mynotebook.ipynb Both HTML and LaTeX support multiple output templates. LaTeX includes 'base', 'article' and 'report'. HTML includes 'basic' and 'full'. You can specify the flavor of the format used. > ipython nbconvert --to html --template basic mynotebook.ipynb You can also pipe the output to stdout, rather than a file > ipython nbconvert mynotebook.ipynb --stdout A post-processor can be used to compile a PDF > ipython nbconvert mynotebook.ipynb --to latex --post PDF You can get (and serve) a Reveal.js-powered slideshow > ipython nbconvert myslides.ipynb --to slides --post serve Multiple notebooks can be given at the command line in a couple of different ways: > ipython nbconvert notebook*.ipynb > ipython nbconvert notebook1.ipynb notebook2.ipynb or you can specify the notebooks list in a config file, containing:: c.NbConvertApp.notebooks = ["my_notebook.ipynb"] > ipython nbconvert --config
In [53]:
name = 'IPython_notebook'
In [56]:
In [57]:
!ipython nbconvert IPython_notebook.ipynb --to latex --post PDF
[NbConvertApp] Using existing profile dir: u'/Users/nicolasf/.ipython/profile_default'
[NbConvertApp] Converting notebook IPython_notebook.ipynb to latex [NbConvertApp] Support files will be in IPython_notebook_files/
[NbConvertApp] Loaded template article.tplx
[NbConvertApp] Writing 50326 bytes to IPython_notebook.tex [NbConvertApp] Building PDF [NbConvertApp] Running pdflatex 3 times: [u'pdflatex', u'IPython_notebook.tex'] Traceback (most recent call last): File "/Users/nicolasf/anaconda/bin/ipython", line 6, in <module> sys.exit(start_ipython()) File "/Users/nicolasf/anaconda/lib/python2.7/site-packages/IPython/", line 120, in start_ipython return launch_new_instance(argv=argv, **kwargs) File "/Users/nicolasf/anaconda/lib/python2.7/site-packages/IPython/config/", line 564, in launch_instance app.start() File "/Users/nicolasf/anaconda/lib/python2.7/site-packages/IPython/terminal/", line 367, in start return self.subapp.start() File "/Users/nicolasf/anaconda/lib/python2.7/site-packages/IPython/nbconvert/", line 268, in start self.convert_notebooks() File "/Users/nicolasf/anaconda/lib/python2.7/site-packages/IPython/nbconvert/", line 311, in convert_notebooks self.postprocessor(write_resultes) File "/Users/nicolasf/anaconda/lib/python2.7/site-packages/IPython/nbconvert/postprocessors/", line 28, in __call__ self.postprocess(input) File "/Users/nicolasf/anaconda/lib/python2.7/site-packages/IPython/nbconvert/postprocessors/", line 140, in postprocess cont = self.run_latex(filename) File "/Users/nicolasf/anaconda/lib/python2.7/site-packages/IPython/nbconvert/postprocessors/", line 104, in run_latex self.latex_count, log_error) File "/Users/nicolasf/anaconda/lib/python2.7/site-packages/IPython/nbconvert/postprocessors/", line 84, in run_command p = subprocess.Popen(command, stdout=stdout, stdin=null) File "/Users/nicolasf/anaconda/lib/python2.7/", line 710, in __init__ errread, errwrite) File "/Users/nicolasf/anaconda/lib/python2.7/", line 1327, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory If you suspect this is an IPython bug, please report it at: or send an email to the mailing list at You can print a more detailed traceback right now with "%tb", or use "%debug" to interactively debug it. Extra-detailed tracebacks for bug-reporting purposes can be enabled via: c.Application.verbose_crash=True
In [58]:
!ipython nbconvert {name}.ipynb --to html
[NbConvertApp] Using existing profile dir: u'/Users/nicolasf/.ipython/profile_default'
[NbConvertApp] Converting notebook IPython_notebook.ipynb to html [NbConvertApp] Support files will be in IPython_notebook_files/
[NbConvertApp] Loaded template full.tpl
[NbConvertApp] Writing 12800808 bytes to IPython_notebook.html
In [59]:
display(HTML("<a href='{name}.html' target='_blank'> {name}.html </a>".format(name=name)))

Sharing your IPython notebook

  1. host your IPython notebook on the public internet (e.g. github repo, github gist)
  2. go to and copy the URL
  3. Et voila !

Interacting with your code with the IPython notebook widgets

IPython Parallel

In [ ]:
In [ ]:
In [ ]:

IPython on a HPC

Use IPython notebook interactively on a HPC system: