No enemies of the People will be guillotined during this talk.
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.
source: Blue's Clues Wikia
So what is it?
So something big is going on here. You don't want to be a counter-revolutionary, do you?
%%ruby
say = "if that's your thing"
puts say.upcase
IF THAT'S YOUR THING
%%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
%%script perl
my $files = `ls *.ipynb`;
print $files;
demos.ipynb make_presentation.ipynb Measuring g with an inclined plane.ipynb Music.ipynb presentation.ipynb
files = !ls *.ipynb
files
['demos.ipynb', 'make_presentation.ipynb', 'Measuring g with an inclined plane.ipynb', 'Music.ipynb', 'presentation.ipynb']
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
!rm -f *.ipynb.2
!ls *_*.ipynb*
make_presentation.ipynb
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
“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
Probablistic Programming & Bayesian Methods for Hackers by Cameron Davidson-Pilon
Let's make a coin.
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
Coin('heads')
That was boring. Let's display or
from IPython.display import Image
coin.flip()
if coin.state == 'heads':
i = Image('images/CENT_OBV.jpg')
else:
i = Image('images/CENT_REV.jpg')
i
That was not very object-oriented.
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
Coin()
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!
Coin()
from IPython.display import YouTubeVideo
# a talk about IPython at Sage Days at U. Washington, Seattle.
# Video credit: William Stein.
YouTubeVideo('1j_HxD4iLn8')
Inline TeX in Markdown: $\sqrt{3x-1}+(1+x)^2$
from IPython.display import Math
Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')
>>> def cheer(word):
... for letter in word:
... print('%s!' % letter.upper())
...
>>> cheer('ohio')
O! H! I! O!
%load http://catherinedevlin.pythoneers.com/posertalk.py
import random
word = 'python'
result = ''.join(l.upper()
for l in word
if random.choice((True, False))
else l)
result
ipython nbconvert --format reveal presentation.ipynb
cd nbconvert_build
python -m SimpleHTTPServer 8000 &
Requires IPython 1.0
Every notebook is a JSON file with ".ipynb" extension
!head presentation.ipynb
{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ {
Share a link to a workbook
or a link to a bundle
... and your collaborators don't even need Python...
The IPython kernel executes code; the client(s) is/are separate
%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.
IPython Notebook in IDEs
Calls for a magnet to release the ball and a photo gate!
%quickref
, %lsmagic