The Jupyter Notebook is a web-based application that enables users to create documents that combine live code wth narrative next, equations, images, visualizations and HTML/JavaScript widgets.
This notebook gives an overview of the Jupyter Notebook and the standard IPython Kernel for running Python code.
First and foremost, Jupyter is an interactive environment for writing and running code. We provide features to make this as pleasant as possible.
In [1]:
2+4
Out[1]:
In [2]:
import math
In [3]:
math.atan?
Inline plotting:
In [4]:
%matplotlib inline
from matplotlib import pyplot as plt
from matplotlib import style
import numpy as np
import pandas as pd
In [5]:
data = {
'x': np.random.rand(100),
'y': np.random.rand(100),
'color': np.random.rand(100),
'size': 100.0*np.random.rand(100)
}
df = pd.DataFrame(data)
In [6]:
style.use('seaborn-whitegrid')
In [7]:
plt.scatter('x', 'y', c='color', s='size', data=df, cmap=plt.cm.Blues)
plt.xlabel('x')
plt.ylabel('y')
plt.title("The data that we collected")
Out[7]:
Seamless access to the system shell:
In [8]:
!ls -al
In addition to code cells, the Notebook offers Markdown cells, which enable the user to create narrative text with embedded LaTeX equations. Here is a cell that includes Maxwell's equations:
\begin{aligned} \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \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}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ \nabla \cdot \vec{\mathbf{B}} & = 0 \end{aligned}Markdown cells enable users to create complex narratives that tell stories using code and data.
We are calling this literate computing as it is similar to Knuth's literate programming, but involves live code and data.
Programming langauges, including Python, allow the writing of textual output to stdout and stderr. Jupyter and IPython extend this idea and allows objects to declare rich output representations:
In IPython, the display function is like print for these rich representations:
In [6]:
from IPython.display import display
The Image object has a JPEG/PNG representation that is rendered by the Notebook:
In [7]:
from IPython.display import Image
In [8]:
i = Image("images/jupyter_logo.png", width="50%")
If you print the object, you see its textual representation:
In [9]:
print(i)
However, if you pass the object to display, the actual image is shown:
In [10]:
display(i)
Or, show the image by returning the object as the final result of an expression:
In [11]:
i
Out[11]:
The HTML object has an HTML representation:
In [12]:
from IPython.display import HTML
In [13]:
s = """<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>"""
In [14]:
h = HTML(s)
In [15]:
display(h)
The Javascript object has a "representation" that runs JavaScript code in the context of the Notebook.
In [ ]:
from IPython.display import Javascript
In [ ]:
display(Javascript("alert('hi');"))
This display architecture also understands objects that have a LaTeX representation. This is best illustrated by SymPy, which is a symbolic mathematics package for Python.
In [16]:
from __future__ import division
from sympy import *
x, y, z = symbols("x y z")
init_printing(use_latex='mathjax')
When a symbolic expression is passed to display or returned from an expression, the LaTeX representation is computed and displayed in the Notebook:
In [17]:
Rational(3,2)*pi + exp(I*x) / (x**2 + y)
Out[17]:
In [18]:
(1/cos(x)).series(x, 0, 16)
Out[18]: