IPython Notebook Introduction

An EAST Knowledge presentation on 2014-06-14 by Charles Stanhope Personal Card

What is it?

From the home page:

The IPython Notebook is a web-based interactive computational environment where you can combine code execution, text, mathematics, plots and rich media into a single document.

I would add that IPython Notebook enables you to:

  • Rapidly prototype and explore algorithmic ideas
  • Easily share results
  • Replicate work
  • Freely collaborate
  • Leverage a large body of existing libraries

Interface Basics

  • Uses the web browser
  • A scrolling document interface
  • Multiple cells per document
  • Each cell is one of a few types:
    • Markdown - for expository content
    • Code - for actual computing
    • Headings - for providing linkable sections
    • Raw - for just raw text
  • Code cells produce output that appears inline with the document

Quick Code Cell Examples

In [1]:
# Enter our data
# I get two tip payments per month.
tips_for_the_year = [30.0, 40.0, # Jan.
                     25.0, 36.0, # Feb.
                     50.0, 23.0, # Mar.
                     33.0, 54.0, # April
                     41.0, 32.0, # May
                     36.0, 66.0, # June
                     70.0, 40.0, # July
                     33.0, 52.0, # August
                     54.0, 33.0, # Sept.
                     44.0, 23.0, # Oct.
                     53.0, 44.0, # Nov.
                     80.0, 98.0  # Dec.
print tips_for_the_year
[30.0, 40.0, 25.0, 36.0, 50.0, 23.0, 33.0, 54.0, 41.0, 32.0, 36.0, 66.0, 70.0, 40.0, 33.0, 52.0, 54.0, 33.0, 44.0, 23.0, 53.0, 44.0, 80.0, 98.0]
In [2]:
# Do some calculations
# What is my total tips?
total_tips = sum(tips_for_the_year)
print "Total: $%.2f" % total_tips

# What is my average tip?
average_monthly = total_tips / len(tips_for_the_year)
print "Average: $%.2f" % average_monthly

# What was my best tip?
print "Best: $%.2f" % max(tips_for_the_year)
Total: $1090.00
Average: $45.42
Best: $98.00
In [3]:
# Plot some graphs using our data
import matplotlib.pyplot as plt
%matplotlib inline

# Let's gather our tips into a single list of monthly tips.
# I can continue to use the tips that I entered above as long
# as I have executed the cell previous to this one.
per_month = []
for x in range(0, len(tips_for_the_year)/2):
    per_month.append(tips_for_the_year[x] + tips_for_the_year[x + 1])

# Altnerate:
# per_month = [sum(n) for n in zip(tips_for_year[0::2], tips_for_the_year[1::2])]

plt.plot(range(1, len(per_month)+1), per_month)
plt.title("2013 Tip Performance")
plt.ylabel("Tips ($)")
<matplotlib.text.Text at 0x37dc690>

Theory of Operation


  • Two Process Model
    • The language "kernel" (Python) runs in one process
    • The interface (Notebook) runs in another process
    • You can use other interfaces with IPython e.g. console or qtconsole.
  • Communication occurs over a messaging passing interface
  • The two process do not have to run on the same machine
    • (In fact, I am running this demo with two machines.)

IPython Two Process Model

More Thorough Example

How to Get Help

  • Use the Menu bar above
  • The interactive help of IPython
    • ?
    • %quickref
    • object?
  • help() function for Python
  • The internet (but you already knew that)
In [4]:
# IPython help
In [5]:
# IPython quick reference
In [6]:
# The object? IPython provides
In [7]:
# Call help() to get Python documentation for an object.
# Python often (not always) has extensive documentation for its modules
Help on _Helper in module site object:

class _Helper(__builtin__.object)
 |  Define the builtin 'help'.
 |  This is a wrapper around pydoc.help (with a twist).
 |  Methods defined here:
 |  __call__(self, *args, **kwds)
 |  __repr__(self)
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  __weakref__
 |      list of weak references to the object (if defined)

In [8]:
# Or you can call help() by itself to get an interactive help.
Welcome to Python 2.7!  This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/2.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

help> quit

You are now leaving help and returning to the Python interpreter.
If you want to ask for help on a particular object directly from the
interpreter, you can type "help(object)".  Executing "help('string')"
has the same effect as typing a particular string at the help> prompt.

Other Nice Features

  • HTML Export
    • Version 2.0: available directly from the browser interface!
    • But you still have to have pandoc installed
  • The "magic" functions which provide easy access to various system level functions
  • Support for other programming languages
  • Supports rich inline output for:
    • Graphics
    • HTML
  • Interactive widgets
  • Latex support for great looking math!
    • Unfortunately I don't have any examples. :-/
  • Support for multiprocessing


What drew me to use IPython Notebook

(Story time about the issues I've run into for development.)

Why I like IPython Notebook:

  • Getting answers fast
    • No compile link cycle
    • Work incrementally and quickly
    • Runtime is preserved while you work
    • Allowing you to build as you go
  • Documenting work is easy
    • HTML export! Readers only need a browser.
    • (So awesome...)
  • Replicating work or collaboration
    • Freely licensed so no purchasing issues
    • IPython works cross-platform
  • A mind boggling amount of libraries and modules that are available to be used
    • And this is counting only Python. Other languages can be used too!
  • Actively being developed


Thank You!

You people are awesome. Let's try a live demo!

  • SSID: EAST Knowledge
  • Password: xxxxxxxxxxxxxxxx

Once connected to EAST Knowledge WiFi, try:

or if that fails, try:

in your browser.

Copyright Notice

To the extent possible under law, Charles Stanhope has waived all copyright and related or neighboring rights to IPython Notebook Introduction. This work is published from: United States.

In [8]: