Intro to Jupyter Notebooks


Vicky Steeves and Nicholas Wolf


Jupyter Notebooks Overview

Web Application
  • In-browser editing for code with auto-syntax highlighting, indentation, tab completion/introspection
  • In-browser code execution, with results attached to the code that generated them
  • Easily include math markdown using LaTeX
  • Display results of computation in rich media (LaTeX, HTML, SVG, etc.)

Jupyter Notebooks Overview

Notebook
  • A complete computation record of a session, interleaving executable code with text, maths, and rich representations of objects
  • Can export to LaTeX, PDF, slideshows, etc. or available from a public URL that renders it as a static webpage

Examples

Installation



Download this file and save it in a directory where you can find it.

Jupyter Notebook Cheatsheet

Launch Notebook in a Directory Other than Home:
jupyter notebook --notebook-dir=PATH-TO-FOLDER

Code Cells:
  • Notebook will evaluate final statement in cell
  • Use shift + enter to run an active cell
  • Use esc in highlighted cell to toggle command options:
    • esc + L = show line numbers
    • esc + M = format cell as Markdown cell
    • esc + a = insert cell above current cell
    • esc + b = insert cell below current cell
  • Check all current variables: run %whos

Jupyter Notebook Cheatsheet

Tidy Display of Pandas Dataframes and Inline Images:

from IPython.display import Image as IM
from IPython.display import display

(Other types: file links, audio files, code, and more. See here)

Creating and Displaying Interactive Plots:

import matplotlib.pyplot as plt
%matplotlib inline

Jupyter Notebook Cheatsheet

Using IPython Display:

display(dataframe)
display(image)

Make a Basic Scatter Plot:

x = list of values
y = list of values
plt.scatter(x, y) plt.xlabel('X Axis') plt.xlabel('Y Axis')

Jupyter Notebook Cheatsheet

Markdown Cells:

Moving a Notebook to the Web

Using NBViewer: https://nbviewer.jupyter.org/:

  1. Put notebook (.ipynb) file on the web (e.g. Github, Gitlab...somewhere so that URL is http://NAME-OF-NOTEBOOK.ipynb
  2. Enter the URL into NBViewer.
  3. Click Go! (check out example here)

Moving a Notebook to the Web

Using NBConvert: https://github.com/jupyter/nbconvert:

  1. In directory of your notebook, enter:
    jupyter nbconvert --output-dir='./docs' --to html *.ipynb
  2. This gives you a directory called “docs” which has HTML files. If you put these files on GitHub or GitLab, you can render them as webpages.
    Example: https://vickysteeves.gitlab.io/Stories-Code-Data-Py/

More on Jupyter Notebooks

Excellent Tutorial by Justin Bois at Caltech:
http://bebi103.caltech.edu.s3-website-us-east-1.amazonaws.com/2015/tutorials/t0b_intro_to_jupyter_notebooks.html#Best-practices-for-code-cells

Jupyter.org Documentation for Jupyter Notebooks:
https://jupyter-notebook.readthedocs.io/en/stable/

Questions?


Email us: vicky.steeves@nyu.edu & nicholas.wolf@nyu.edu

Learn more about RDM: guides.nyu.edu/data_management

Get this presentation: guides.nyu.edu/data_management/resources

Make an appointment: guides.nyu.edu/appointment