The IPython Notebook Revolution

Catherine Devlin

PyOhio 2013

No enemies of the People will be guillotined during this talk.

IPython: An interactive Python environment

Who cares?

IPython 0.0.1 was introduced in 2001, before I had even heard of Python. For years, I considered myself a Python programmer while barely knowing a thing about IPython. I knew it had tab-completion; that was nice. And a bunch of "magic" commands that I didn't have time to research.

IPython Notebook

So what is it?

The Revolution triumphant

  • IPython Notebook introduced at EuroSciPy 2011
  • Increasing buzz at PyCon 2012, 2013
  • July 2013: 1.0.dev released!

So something big is going on here. You don't want to be a counter-revolutionary, do you?

Science!

Sir Not-Appearing-In-This-Film:

  • Graphing
  • pandas
  • Parallel computing
  • Cython

IPython Notebook revolutionizes

Your personal workflow

Your social workflow

Itself

Revolutionize Your Personal Workflow

Amp up your introspection

Break out of your shell

Run not-Python


In [6]:
%%ruby
say = "if that's your thing"
puts say.upcase


IF THAT'S YOUR THING

Run bash scripts


In [7]:
%%bash
for ext in py ipy ipynb; do
  ls *.$ext
done


XKCDify.py
d3js.ipynb
demos.ipynb
demo_wak.ipynb
make_presentation.ipynb
presentation.ipynb
ls: cannot access *.ipy: No such file or directory

Don't run bash scripts

End backtick envy


In [2]:
%%script perl
my $files = `ls *.ipynb`;
print $files;


demos.ipynb
make_presentation.ipynb
Measuring g with an inclined plane.ipynb
Music.ipynb
presentation.ipynb

In [3]:
files = !ls *.ipynb
files


Out[3]:
['demos.ipynb',
 'make_presentation.ipynb',
 'Measuring g with an inclined plane.ipynb',
 'Music.ipynb',
 'presentation.ipynb']

Hybrid scripts


In [4]:
for ext in ('html', 'ipy', 'ipynb'):
    !ls *.{ext}


presentation.reveal.html
ls: cannot access *.ipy: No such file or directory
demos.ipynb				  Music.ipynb
make_presentation.ipynb			  presentation.ipynb
Measuring g with an inclined plane.ipynb

In [5]:
!rm -f *.ipynb.2
!ls *_*.ipynb*


make_presentation.ipynb

In [6]:
notebooks = !ls *_*.ipynb
for nb_name in notebooks:
    name_stem = nb_name.split('.ipynb')[0]
    !cp {name_stem}.ipynb {name_stem.upper()}.ipynb.2
!ls *_*.ipynb*


make_presentation.ipynb  MAKE_PRESENTATION.ipynb.2

Fits the way you experiment

Matches experimental workflow

Keep your experiments

Revolutionize Your Shared Workflow

“Man is by nature a social animal; an individual who is unsocial naturally and not accidentally is either beneath our notice or more than human. Society is something that precedes the individual. Anyone who either cannot lead the common life or is so self-sufficient as not to need to, and therefore does not partake of society, is either a beast or a god. ”

― Aristotle, Politics

Rich output

Expressive documentation

Compare to Sphinx and Dexy

Easiest balance of clarity and ease

Executable, always-updated book

Expressive results

Let's make a coin.


In [15]:
import random

class Coin(object):
    def __init__(self, state=None):
        if state in ('heads', 'tails'):
            self.state = state
        else:
            self.flip()
    def flip(self):
        self.state = random.choice(('heads', 'tails'))
    def __repr__(self):
        return "Coin('%s')" % self.state
        
               
coin = Coin()
coin


Out[15]:
Coin('heads')

That was boring. Let's display or


In [16]:
from IPython.display import Image

coin.flip()
if coin.state == 'heads':
    i = Image('images/CENT_OBV.jpg')
else:
    i = Image('images/CENT_REV.jpg')

In [17]:
i


Out[17]:

That was not very object-oriented.


In [18]:
class Coin(object):
    def __init__(self, state=None):
        if state in ('heads', 'tails'):
            self.state = state
        else:
            self.flip()
    def flip(self):
        self.state = random.choice(('heads', 'tails'))
    def __repr__(self):
        return "Coin('%s')" % self.state
    def _repr_html_(self):
        return '''a <span style="color:gold">Coin</span> 
                  showing <span style="font-size:40px;font-weight:bold;"
                  >%s</span>''' % self.state

In [19]:
Coin()


Out[19]:
a Coin showing heads

In [20]:
class Coin(object):
    def __init__(self, state=None):
        if state in ('heads', 'tails'):
            self.state = state
        else:
            self.flip()
    def flip(self):
        self.state = random.choice(('heads', 'tails'))
    def __repr__(self):
        return "Coin('%s')" % self.state
    coin_faces = {'heads': 'CENT_OBV.jpg', 'tails': 'CENT_REV.jpg'}
    def _repr_html_(self):
        # return '<img src="files/images/%s" />' % self.coin_faces[self.state]
        return '<img src="files/images/%s" />' % self.coin_faces[self.state]   
        # The notebook and the .reveal presentation work differently here!

In [21]:
Coin()


Out[21]:

Anything goes


In [22]:
from IPython.display import YouTubeVideo
# a talk about IPython at Sage Days at U. Washington, Seattle.
# Video credit: William Stein.
YouTubeVideo('1j_HxD4iLn8')


Out[22]:

Math / Latex

Inline TeX in Markdown: $\sqrt{3x-1}+(1+x)^2$


In [69]:
from IPython.display import Math
Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')


Out[69]:
$$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx$$

d3js (see demo)

Social coding

Use pasted code


In [34]:
>>> def cheer(word):
...     for letter in word:
...         print('%s!' % letter.upper())
... 
>>> cheer('ohio')


O!
H!
I!
O!

Load directly from a URL


In [35]:
%load http://catherinedevlin.pythoneers.com/posertalk.py

In [ ]:
import random

word = 'python'
result = ''.join(l.upper()
                 for l in word
                 if random.choice((True, False))
                 else l)
result

Give a presentation

ipython nbconvert --format reveal presentation.ipynb
cd nbconvert_build
python -m SimpleHTTPServer 8000 &

Requires IPython 1.0

Blog

Example

More examples in the Gallery

nbviewer

Every notebook is a JSON file with ".ipynb" extension


In [23]:
!head presentation.ipynb


{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
  • nbviewer.ipython.org/url/.ipynb-url serves it publicly!
  • or nbviewer.ipython.org/*gist-number,
  • downloadable

wakari by Continuum Analytics

Share a link to a workbook

or a link to a bundle

... and your collaborators don't even need Python...

The Ongoing Revolution

(the awesome future)

IPython is an architecture

The IPython kernel executes code; the client(s) is/are separate


In [1]:
%connect_info


{
  "stdin_port": 33954, 
  "ip": "127.0.0.1", 
  "control_port": 49134, 
  "hb_port": 35868, 
  "key": "b9e29524-a1a6-43d3-a1ac-afce9f54c8af", 
  "shell_port": 37299, 
  "transport": "tcp", 
  "iopub_port": 42341
}

Paste the above JSON into a file, and connect with:
    $> ipython <app> --existing <file>
or, if you are local, you can connect with just:
    $> ipython <app> --existing kernel-5083b221-ec94-49f1-83b1-48f9e8fa6d91.json 
or even just:
    $> ipython <app> --existing 
if this is the most recent IPython session you have started.

Easygoing architecture -> endless client potential

Extending and hacking

(more demos)

Keyboard demo by Benjamin RK

Gravity measurement by Benjamin RK

Calls for a magnet to release the ball and a photo gate!

Go learn this


In [ ]: