What is IPython?¶
Level 0: Interactive Python¶
In [ ]:
def foo():
"""docstring"""
return 'hi'
foo()
In [ ]:
def bar():
print 'hi'
In [ ]:
from __future__ import print_function
import time
for i in range(10):
time.sleep(0.25)
print(i, end=' ')
In [ ]:
import numpy as np
In [ ]:
import ctypes
libc = ctypes.CDLL('libc.dylib')
libc.time(-1)
In [ ]:
print("I'm okay!")
Level 1: A bit more than Python¶
In [ ]:
import numpy as np
In [ ]:
np.
In [ ]:
np.linspace?
Shell interaction¶
In [ ]:
!date
In [ ]:
ls
In [ ]:
files = !ls
In [ ]:
for f in files:
print(repr(f))
Extending Python with %magics¶
In [ ]:
biglist = range(1000000)
bigset = set(biglist)
item = biglist[0]
%timeit item in biglist
%timeit item in bigset
In [ ]:
def inner(x):
return 1/x
def outer(y):
return inner(1-y)
z = 2
outer(-z + 3)
In [ ]:
%debug
In [ ]:
%lsmagic
Level 2: A bit more than the Terminal¶
Rich Display¶
In [ ]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import jn
In [ ]:
x = np.linspace(0,10)
for n in range(5):
plt.plot(x, jn(n,x))
In [ ]:
%%latex
\begin{eqnarray}
\nabla \times \vec{\mathbf{B}} &
- \frac{1}{c} \frac{\partial\vec{\mathbf{E}}}{\partial t}
& = & \frac{4 \pi}{c} \vec{\mathbf{j}} \\
\nabla \cdot \vec{\mathbf{E}} && = & 4 \pi \rho \\
\nabla \times \vec{\mathbf{E}} &
+ \frac{1}{c} \frac{\partial\vec{\mathbf{B}}}{\partial t}
& = & \vec{\mathbf{0}} \\
\nabla \cdot \vec{\mathbf{B}} && = & 0
\end{eqnarray}
In [ ]:
import sympy
sympy.init_printing(use_latex='mathjax')
x = sympy.symbols('x')
expr = x**10-1
expr
In [ ]:
sympy.factor(expr)
In [ ]:
from IPython.display import display
from IPython.html.widgets import interact
@interact
def factorit(n=10):
display(sympy.factor(x**n-1))
In [ ]:
from IPython.display import Image, YouTubeVideo
Image("figs/darts.png")
In [ ]:
YouTubeVideo('BROWqjuTM0g ')
Level 3: Documents¶
In the Notebook: not just code¶
We also have
- markdown and
- $\LaTeX$
var foo = $(".class");
What is a notebook?¶
In [ ]:
name = "Background"
!head -n 100 {name}.ipynb | pygmentize -l json
In [ ]:
!ipython nbconvert {name}.ipynb --to html
from IPython.display import display, HTML
display(HTML("<a href='{name}.html' target='_blank'>{name}.html</a>".format(name=name)))
But what is IPython, really?¶
Message type: execute_request
content = {
# Source code to be executed by the kernel, one or more lines.
'code' : str,
# A boolean flag which, if True, signals the kernel to execute
# this code as quietly as possible.
'silent' : bool,
# A boolean flag which, if True, signals the kernel to populate history
# The default is True if silent is False. If silent is True, store_history
# is forced to be False.
'store_history' : bool,
...
}
Message type: execute_reply
content = {
# One of: 'ok' OR 'error'
'status' : str,
# The global kernel counter that increases by one with each request that
# stores history. This will typically be used by clients to display
# prompt numbers to the user. If the request did not store history, this will
# be the current value of the counter in the kernel.
'execution_count' : int,
}
Message type: display_data
content = {
# The data dict contains key/value pairs, where the kids are MIME
# types and the values are the raw data of the representation in that
# format.
'data' : dict,
# Any metadata that describes the data
'metadata' : dict,
}
In [ ]:
%%javascript
IPython.notebook.kernel.execute("a=1");
In [ ]:
print(a)
The protocol is publicly documented, and language agnostic.
So what is IPython, really?¶
- Tools for interactive computing
- documents
- interfaces
- terminal
- qtconsole
- notebook
- introspection
- debugging
- A message protocol for remote execution
- language-agnostic
- rich remote repl
How does this become IPython.parallel?¶
Let's see a quick demo, and then find out in more detail.