Notebooks

  • Previously known as "IPython notebooks"

What's good about notebooks?

  • They can run any Python (or increasingly, other languages) that you want
  • They are "rich" documents (I.e., they include LaTeX, plots, sounds, animations, etc.)
  • They are easily shared (just put them on github or anywhere else on the internet)
  • They are easy to maintain (they are just JSON files, no fancy database or anything required)

How do you use them?

(Start jupyter notebook from wherever all your stuff is for a project)

Create a notebook, and start typing Python code:

In [ ]:
from astropy import coordinates

coordinates.SkyCoord.from_name('M31')

Hit shift-enter (or control-enter)

In [1]:
from astropy import coordinates

coordinates.SkyCoord.from_name('M82')
Out[1]:
<SkyCoord (ICRS): (ra, dec) in deg
    (148.9684583, 69.6797028)>

Some Tips

Pay attention to the color of the box around the cell: green means "edit" mode (typing will edit the code), blue means "Command Mode" (typing will move/insert/execute cells).

Try the "multi select" mode (hold down cmd or ctrl while clicking on different parts of text in a cell) - every keystroke goes to all of the cursors.

Use ? to get help on a function - it appears in a pop-up bar at the bottom of the notebook

Check out the %magic list: lots of useful productivity tools (from IPython)

To learn more, open a notebook and go to "Help"->"User interface tour" or "Help"->"Keyboard Shortcuts"

Some neat things you can do with notebooks

Make plots

In [2]:
%matplotlib inline

from matplotlib import pyplot as plt
import numpy as np
In [3]:
plt.scatter(*np.random.randn(2, 100), 
            s=np.pi * (15 * np.random.rand(100))**2 , 
            c=np.random.randn(100), alpha=0.5,
            cmap='viridis')
plt.xlabel('x')
plt.ylabel('y')
Out[3]:
<matplotlib.text.Text at 0x11221ce10>

Render LaTeX

$M(r) = - \frac{r \sigma^2}{G} 
          \left[ \frac{d \ln \nu}{d \ln r} + 
                 \frac{d \ln \sigma^2}{d \ln r} + 
                  2 \beta(r)
          \right]$

$M(r) = - \frac{r \sigma^2}{G} \left[ \frac{d \ln \nu}{d \ln r} + \frac{d \ln \sigma^2}{d \ln r} + 2 \beta(r) \right]$

Embed images

In [4]:
from IPython import display

urltempl = ("http://skyservice.pha.jhu.edu/dr12/ImgCutout/getjpeg.aspx?"
            "ra={coo.ra.deg}&dec={coo.dec.deg}&width={width}&height={height}&scale={scale}")
m101 = coordinates.SkyCoord.from_name('M101')
display.Image(url=urltempl.format(coo=m101, width=1024, height=768, scale=1.25))
Out[4]:

Test/time your code

In [5]:
%timeit np.sqrt(12)
The slowest run took 14.82 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 1.07 µs per loop
In [6]:
%timeit 12**0.5
10000000 loops, best of 3: 22.4 ns per loop

Show animations

In [8]:
from matplotlib import animation
# Details of this one's implementation are in a hidden slide
animation.FuncAnimation(lorenz_fig, func=lorenz_animate, 
                        init_func=lorenz_init, blit=True,
                        frames=500, interval=30)
Out[8]:

Make presentations

The one you're seeing here!