Jupyter Reference

Jupyter notebooks are a way to program interactively, incorporating text, links, math, and graphics straight into the notebook. The gold standard for a notebook is that anyone could download your notebook off the internet and run it to reproduce an analysis you report in a publication.

Jupyter notebooks are built from "cells." There are two basic types of cells in jupyter. Markdown cells (like this one) that can hold text and code cells (like the next one) that hold code.


In [ ]:
print("This is a code cell")
print(5*5)

Interacting with Cells

  • To edit a code cell, just click on it.
  • To run a code cell, click on it and type Shift+Enter
  • To edit a markdown cell, double click on the cell.
  • To make the markdown cell look pretty, click on it and type Shift+Enter.
  • You can change the cell type with the dropdown menu above that selects Code or Markdown.
  • You can create a new cell by clicking Insert->Cell on the menu.

Code cells

Each code cell contains python code. It can be run indepedently of others. To run it, click on the cell and hit Shift+Enter. To run all of the cells in order, go to the menu and select Cell->Run All.

When you run a cell, all other cells "know" you ran the code. Try running the three code cells that follow this one.

  • run the x = 5 cell.
  • run the print(x) cell (it should spit out 5).
  • run the x = 7 cell.
  • re-run the print(x) cell (it should now spit out 7).

What matters is what cell ran last, not where the cell is up and down on the page. Because x = 7 was the last code ran, print(x) will now return 7 wherever that command is in the notebook.


In [ ]:
x = 5

In [ ]:
print(x)

In [ ]:
x = 7

Sometimes bad things happen

  • If your code is frozen, you can go to Kernel->Interrupt.
  • To restart the whole session (clearing all previous runs) go to the menu and select Kernel->Restart. You'll have to rerun all cells at this point.

Graphics

Lots of code needs to dump out a graph. This can be achieved by putting the "magic" directive %matplotlib inline in a code cell (this usually goes at the very top of the notebook). After that is called, all graphs will be captured by the notebook. Run the following to cells to see how it works.


In [ ]:
%matplotlib inline

In [ ]:
from matplotlib import pyplot as plt
plt.plot([1,2,3],[4,5,6])

Markdown cells

MARKDOWN CELLS ARE YOUR LAB NOTEBOOK

This is the place you should take notes describing what you're doing and why. Markdown let you annotate your science with pretty formatting. You write text normally, but then add a few simple markdown flags to make the cell pretty.

Formatting is not hard

  • This is bold text.
  • This is italic text.
  • This is math $x = 2$ using LaTeX.
  • This is code: x = 2.
  • This is a link.

Lists aren't hard:

  • this
  • is
  • a
  • list

Neither are numbered lists:

  1. this
  2. is
  3. a
  4. numbered
  5. list

You can put in a big equation:

$$x = \frac{sin(y)}{\sqrt{e^{-z}}}$$

Or multiple lines of code:

# This is a multi-line code block. It won't run, but it will look nice

x = 2
print(x*x)

# See?

You can make tables

Tables Are Cool
a $x=7$ 1600
b print(x) 12

There are several levels of headers

Biggest

Big

Still big

Bold, not big

Italic, not big

You can even use html to make things really interesting.

Like adding color.


In [ ]: