Notebook Tour

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.

Interactive exploration

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]:
6

In [2]:
import math

In [3]:
math.atan?


Docstring:
atan(x)

Return the arc tangent (measured in radians) of x.
Type:      builtin_function_or_method

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]:
<matplotlib.text.Text at 0x11018a7b8>

Seamless access to the system shell:


In [8]:
!ls -al


total 66904
drwxr-xr-x  38 bgranger  staff      1292 Oct 22 14:50 .
drwxr-xr-x  30 bgranger  staff      1020 Jul 21 13:31 ..
-rw-r--r--@  1 bgranger  staff     10244 May 11  2016 .DS_Store
drwxr-xr-x  13 bgranger  staff       442 Jun 26 09:42 .git
-rw-r--r--   1 bgranger  staff       575 Apr 29  2016 .gitignore
drwxr-xr-x  19 bgranger  staff       646 Oct 23 13:00 .ipynb_checkpoints
-rw-r--r--@  1 bgranger  staff    828359 May  4  2016 Bash Examples.ipynb
-rw-r--r--   1 bgranger  staff   1978751 Oct 22 14:50 Computational Narratives.ipynb
-rw-r--r--   1 bgranger  staff    684332 May  4  2016 Deploying and Scaling.ipynb
-rw-r--r--   1 bgranger  staff     62950 May  4  2016 Index.ipynb
-rw-r--r--   1 bgranger  staff   2554167 Jun 26 09:47 Interaction.ipynb
-rw-r--r--   1 bgranger  staff     36633 May  4  2016 Jupyter and IPython.ipynb
-rw-r--r--   1 bgranger  staff    890612 May  4  2016 JupyterLab.ipynb
-rw-r--r--   1 bgranger  staff      1082 Apr 29  2016 LICENSE
-rw-r--r--   1 bgranger  staff   1332703 Jul 18  2016 Notebook Ecosystem.ipynb
-rw-r--r--   1 bgranger  staff     91450 Jan 17  2017 Notebook Tour.ipynb
-rw-r--r--   1 bgranger  staff    215885 May  4  2016 R Examples.ipynb
-rw-r--r--   1 bgranger  staff       559 Apr 29  2016 README.md
-rw-r--r--   1 bgranger  staff       832 Jun 29 14:23 Untitled.ipynb
-rw-r--r--   1 bgranger  staff   2511496 May  4  2016 Usage in Curriculum.ipynb
-rw-r--r--   1 bgranger  staff   5491180 Jan 17  2017 Usage in Journalism.ipynb
-rw-r--r--   1 bgranger  staff   6564789 Jan 17  2017 Usage in Products.ipynb
-rw-r--r--   1 bgranger  staff  10438560 Jan 17  2017 Usage in Publishing.ipynb
-rw-r--r--   1 bgranger  staff    470272 May  4  2016 What is Data Science.ipynb
drwxr-xr-x   3 bgranger  staff       102 Apr 29  2016 __pycache__
-rw-r--r--   1 bgranger  staff      1188 May  4  2016 courses.csv
drwxr-xr-x   4 bgranger  staff       136 Apr 29  2016 data
-rw-r--r--   1 bgranger  staff      1676 Apr 29  2016 frontmatter.py
drwxr-xr-x  89 bgranger  staff      3026 Feb  6  2017 images
-rw-r--r--   1 bgranger  staff       303 Apr 29  2016 images-pad.css
-rw-r--r--   1 bgranger  staff       288 Apr 29  2016 images.css
-rw-r--r--   1 bgranger  staff      1192 Apr 29  2016 ipythonproject.py
drwxr-xr-x  18 bgranger  staff       612 Apr 29  2016 ipythonteam
-rw-r--r--   1 bgranger  staff       606 Apr 29  2016 load_style.py
-rw-r--r--   1 bgranger  staff      1327 Apr 29  2016 lorenz.py
-rw-r--r--   1 bgranger  staff       972 Apr 29  2016 talk.css
-rw-r--r--   1 bgranger  staff      1090 Apr 29  2016 talktools.py
-rw-r--r--   1 bgranger  staff        13 Apr 29  2016 testing.md

Narrative text and equations

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.

Rich output

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:

  • JavaScript
  • HTML
  • LaTeX
  • PDF
  • PNG/JPEG
  • SVG

In IPython, the display function is like print for these rich representations:


In [6]:
from IPython.display import display

Images

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)


<IPython.core.display.Image object>

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]:

HTML

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)


Header 1 Header 2
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2

JavaScript

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');"))

LaTeX

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]:
$$\frac{3 \pi}{2} + \frac{e^{i x}}{x^{2} + y}$$

In [18]:
(1/cos(x)).series(x, 0, 16)


Out[18]:
$$1 + \frac{x^{2}}{2} + \frac{5 x^{4}}{24} + \frac{61 x^{6}}{720} + \frac{277 x^{8}}{8064} + \frac{50521 x^{10}}{3628800} + \frac{540553 x^{12}}{95800320} + \frac{199360981 x^{14}}{87178291200} + \mathcal{O}\left(x^{16}\right)$$