Jupyter Notebooks

Code
Python
Published

March 19, 2021

Modified

November 21, 2023

Project Jupyter 1 …non-profit, 100% open-source software, modified BSD license …evolved to support interactive data science …scientific computing across all programming languages …developed on GitHub 2. Jupyter is a large umbrella project for tools and standards for interactive computing with computational notebooks.

Notebooks

Computational notebooks …interactive environment for prototyping and explaining code …shareable document that combines computer code and narrative text that accompanies the code. Combines computational inputs of an interactive session …rich output generated by running code …includes HTML, images, video, and plots embedded in the notebook …complete and self-contained record of a computation. Notebook interfaces:

  • Jupyter Notebook 3 …simplified, lightweight notebook authoring experience.
  • JupyterLab 4 …feature-rich …tabbed multi-notebook editing …system console
  • JupyterLab desktop GUI 5 …cross-platform Linux, Mac OS, Windows

Try Jupyter 6 in a web-browser before installation …local installation recommend to be done with Python pip:

# ...JupyterLab
pip install jupyterlab
jupyter lab

# ...Jupyter Notebook
pip install notebook
jupyter notebook

Cells

Notebooks consist of a linear sequence of cells …containing text or executable code …web interface represents a be-modal editor. Enter to go into edit mode for a cell. Esc to leave insert mode back to command mode. Command mode keys:

Key Description
h Show command keys
a/b Create new cell above/bellow
dd Delete current cell
s Save notebook

Cells can have different type …command key m to change the cell type to markdown …typical Markdown notation …including embedded code for illustration …GitHub style syntax high-lighting.

Code

Default cell type is code …executed on the associated kernel:

Key Description
Shift-Enter Run current cell (play button)
Alt-Enter Run current cell and inserts a new one below
Ctrl-Enter Run the current cell and enters command mode

stdout and stderr …displayed as text in the output area …clear the output of a cell by changing its type to raw …back to code using the command keys r, y.

Commands

“Magic commands” …single magic commands are prefixed with % (percent) …multi-line epxressions start with %% (double percent) …prefix the magic command with ? (question mark) to see the help text

%lsmagic       # ...list a all magic functions with:
!date +%s      # ...date command on the shell interpreter

Kernels

Kernels 7 are separate processes started by the Jupyter server to code in given programming language and environment. A full list of kernels 8 is available on GitHub. By default IPython 9 is used a reference kernel ipykernel for a code cell.

Install a kernel for Bash:

pip install bash_kernel
python3 -m bash_kernel.install

List installed kernels…

>>> jupyter kernelspec list   
Available kernels:
  bash       /home/vpenso/.local/share/jupyter/kernels/bash
  python3    /home/vpenso/.local/share/jupyter/kernels/python3

Another popular framework to build Jupyter kernels is Xeus 10 implemented in C++. Several Jupyter kernels are built upon xeus, such as xeus-cling, a kernel for the C++ programming language, and xeus-python, an alternative Python kernel for Jupyter.

Extensions

JupyterLab extensions are npm packages (the standard package format in Javascript development) Search for extensions on Github or npm.

# ...install with Python PIP
pip install jupyterlab-git jupyterlab-spellchecker

# ...or NPM
jupyter labextension install @jupyterlab/toc
jupyter labextension install jupyterlab_vim

# ...show installed extensions
jupyter labextension list          

Noteworthy extensions include: