Jupyter Notebook

This notebook was adapted from https://github.com/oesteban/biss2016 and is originally based on https://github.com/jvns/pandas-cookbook.

Jupyter Notebook started as a web application, based on IPython that can run Python code directly in the webbrowser. Now, Jupyter Notebook can handle over 40 programming languages and is the interactive, open source web application to run any scientific code.

You might also want to try a new Jupyter environment JupyterLab.

How to run a cell

First, we need to explain how to run cells. Try to run the cell below!

In [ ]:
import pandas as pd

print("Hi! This is a cell. Click on it and press the ▶ button above to run it")
Hi! This is a cell. Click on it and press the ▶ button above to run it

You can also run a cell with Ctrl+Enter or Shift+Enter. Experiment a bit with that.

Tab Completion

One of the most useful things about Jupyter Notebook is its tab completion.

Try this: click just after read_csv( in the cell below and press Shift+Tab 4 times, slowly. Note that if you're using JupyterLab you don't have an additional help box option.

In [ ]:
# Use TAB completion for function info

After the first time, you should see this:

After the second time:

After the fourth time, a big help box should pop up at the bottom of the screen, with the full documentation for the read_csv function:

I find this amazingly useful. I think of this as "the more confused I am, the more times I should press Shift+Tab".

Okay, let's try tab completion for function names!

In [ ]:
# Use TAB completion to see possible function names

You should see this:

Get Help

There's an additional way on how you can reach the help box shown above after the fourth Shift+Tab press. Instead, you can also use obj? or obj?? to get help or more help for an object.

In [ ]:

Writing code

Writing code in the notebook is pretty normal.

In [ ]:
def print_10_nums():
    for i in range(10):
In [ ]:

If you messed something up and want to revert to an older version of a code in a cell, use Ctrl+Z or to go than back Ctrl+Y.

For a full list of all keyboard shortcuts, click on the small keyboard icon in the notebook header or click on Help > Keyboard Shortcuts.

Saving a Notebook

Jupyter Notebooks autosave, so you don't have to worry about losing code too much. At the top of the page you can usually see the current save status:

  • Last Checkpoint: 2 minutes ago (unsaved changes)
  • Last Checkpoint: a few seconds ago (autosaved)

If you want to save a notebook on purpose, either click on File > Save and Checkpoint or press Ctrl+S.

Magic functions

IPython has all kinds of magic functions. Magic functions are prefixed by % or %%, and typically take their arguments without parentheses, quotes or even commas for convenience. Line magics take a single % and cell magics are prefixed with two %%.

Some useful magic functions are:

Magic Name Effect
%env Get, set, or list environment variables
%pdb Control the automatic calling of the pdb interactive debugger
%pylab Load numpy and matplotlib to work interactively
%%debug Activates debugging mode in cell
%%html Render the cell as a block of HTML
%%latex Render the cell as a block of latex
%%sh %%sh script magic
%%time Time execution of a Python statement or expression

You can run %magic to get a list of magic functions or %quickref for a reference sheet.

Example 1

Let's see how long a specific command takes with %time or %%time:

In [ ]:
%time result = sum([x for x in range(10**6)])
CPU times: user 64.2 ms, sys: 11.9 ms, total: 76.1 ms
Wall time: 72.8 ms

Example 2

Let's use %%latex to render a block of latex

In [ ]:
$$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} \mathrm{d} x$$
$$F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} \mathrm{d} x$$

Home | github | Nipype