Skip to Main Content

Research Data Management

Information on best practices and standards for data and code management.

JUPYTER NOTEBOOKS

The Jupyter Notebook is an interactive computing environment that enables users to author notebook documents that include code, interactive widgets, plots, narrative text, equations, images and even video! The Jupyter name comes from 3 programming languages: Julia, Python, and R. It is a popular tool for literate programming. Donald Knuth first defined literate programming as a script, notebook, or computational document that contains an explanation of the program logic in a natural language (e.g. English or Mandarin), interspersed with snippets of macros and source code, which can be compiled and rerun. You can think of it as an executable paper!

The Jupyter Notebook combines three components (from the docs):

  • The notebook web application: An interactive web application for writing and running code interactively and authoring notebook documents.

  • Kernels: Separate processes started by the notebook web application that runs users’ code in a given language (e.g. python, R, Julia, Go, and more -- get the full list of kernels from the wiki) and returns output back to the notebook web application. The kernel also handles things like computations for interactive widgets, tab completion and introspection.

  • Notebook documents: Self-contained documents that contain a representation of all content visible in the notebook web application, including inputs and outputs of the computations, narrative text, equations, images, and rich media representations of objects. Each notebook document has its own kernel. You can export your notebook as many other formats, even LaTex and PDF!

The main components of the interface, from top to bottom:

  • The notebook name: you can change by clicking on it. This is also the name of the .ipynb file.
  • The menu bar gives you access to several actions pertaining to the notebook (like saving it!) and the kernel (like restarting it!)
  • To the right of the menu bar is the Kernel name. You can change the kernel language of your notebook from the Kernel menu (to another programming language).
  • The toolbar contains icons for common actions. In particular, the dropdown menu showing Code lets you change the type of a cell.
  • Below all that is the actual Notebook. It consists of a linear list of cells, which you can change the type of (markdown or code). You should only run your notebook from top to bottom – ONLY. If you make a lot of edits, go to Kernel > Restart & Run All before considering your work finished.

Annotated Jupyter Notebook interface

Jupyter notebooks can be comprised mainly of two types of cells (though more can be added with plugins).

  1. Markdown Cells (for narratives): when run, a markdown cell will display markdown or HTML that you write (that means all sort of rich content, including images). Essential markdown summary: https://daringfireball.net/projects/markdown/syntax
  2. Code Cells (for data cleaning, analysis, visualization, etc.): executable code in a variety of languages, dictated by the kernel (default is Python, but more can be added).

Some key jupyter notebook shortcuts to keep in mind while you work:

  • Use shift + enter to run an active cell

  • Use esc in highlighted cell to toggle command options:

    • esc + L  shows line numbers

    • esc + M formats cell as Markdown cell

    • esc + a inserts a cell above current cell

    • esc + b inserts a cell below current cell

  • Check all current variables: run %whos from a code cell

INSTALLING JUPYTER NOTEBOOKS

You can install Jupyter notebooks and some key kernels on your computer in a few ways:

Our recommended method is to download using Anaconda (make sure you select version 3.*), which gives you Jupyter, python 3, and a lot of key python libraries for research: https://www.anaconda.com/download/. After you've finished downloading + installing with Anaconda, you should see an application "Jupyter notebooks" in your list of applications.

If you're comfortable with the terminal you can also install Jupyter Notebooks with pip:

python3 -m pip install --upgrade pip
python3 -m pip install jupyter
jupyter notebooks # launches the notebook interface

CC

Creative Commons License
Original work in this LibGuide is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.