Jupyter Notebook

  • The Jupyter Notebook is an interactive environment for writing and running code.
  • The notebook is capable of running code in a wide range of languages.
  • However, each notebook is associated with a single kernel. This notebook is associated with the IPython kernel, therefore it runs Python code.

Notebook Cells

  • An Notebook is a series of "cells".
  • The active cell has an outline around it.
  • Navigate to different cells with the arrow keys.
  • A cell can be either be code, markdown, or "raw".
  • Create cells from the Notebook menu or using keyboard shortcuts.
    • h: Bring up keyboard shortcuts screen.

Modes

Edit mode Command mode
How to enter Enter,
or double-click
Esc or Ctrl-m
Cell border Green Blue
Action Type code or text Notebook-level actions

Some shortcuts

  • Ctrl-Enter: Run Cell
  • Shift-Enter: Run cell, select cell below.
  • Alt-Enter: Run Cell, insert below.
  • a : Insert cell (above)
  • b : Insert cell (below)
  • dd : Delete cell (the same as VIM)
  • Ctrl-s: Save Notebook
  • i: Interrupt kernel (press twice).
  • 0: Restart kernel (press twice).

Features

1. Nice Errors


In [ ]:
#1 + foos

2. Inline Documentation


In [ ]:
open?

3. Tab Completion (predictive and helpful!)

Type o then tab and you should see a drop-down menu of choices.


In [ ]:
#o     # Always press the tab! Tab is the same as Unix. It works a little like predictive text messaging if you are unfamilar with it.

4. Run Shell Commands with "!"


In [ ]:
!ls  # ls is a Unix command for list. Here we are listing all files within out current Jupyternotebook directory.

5. Remote Operations

  • You can run code both locally and remotely.
  • For example, you can connect to HPC via SSH protocol and run your code there.

6. Embed Images or Videos


In [ ]:
from IPython.core.display import Image 
Image(url='http://python.org/images/python-logo.gif')

In [ ]:
Image(url='https://jupyter.org/assets/main-logo.svg',width=200)

In [ ]:
from IPython.lib.display import YouTubeVideo
YouTubeVideo("7qym7b-qvkE")

7. Load Remote Code - This is Very Cool and helpful!

You can pull up remote code using loadpy

loadpy http://matplotlib.org/mpl_examples/mplot3d/2dcollections3d_demo.py

In [ ]:
#loadpy http://matplotlib.org/mpl_examples/mplot3d/2dcollections3d_demo.py

You can also notice that probably the best feature of the notebook is inline figures.

Sharing Notebooks

  • The file itself is simply a JSON file which can be shared the same as any other file.
  • It is possible to save a notebook in various formats via the File->Download As menu.

    • HTML
    • Markdown
    • Python script
    • PDF
    • etc.
  • NBviewer - view (but not execute) Jupyter Notebooks

References