skip to content

jupyter β€” Interactive Notebooks

Run interactive Python notebooks with Jupyter. Covers JupyterLab setup, cell types, keyboard shortcuts, magic commands, nbconvert export, and common pitfalls.

5 min read 15 snippets yesterday quick read

jupyter β€” Interactive Notebooks#

What it is#

Jupyter notebooks (.ipynb files) let you mix executable Python cells, markdown, and rich outputs (plots, tables, HTML) in a single document. There are two interfaces:

  • JupyterLab β€” modern IDE-like interface (recommended)
  • Jupyter Notebook β€” classic single-document interface

Both run a local web server and open in your browser. Notebooks are widely used for data exploration, prototyping, and technical writing.

Install#

# JupyterLab (recommended)
pip install jupyterlab

# Classic Notebook interface
pip install notebook

# Both
pip install jupyterlab notebook

Launch#

jupyter lab           # opens JupyterLab at http://localhost:8888
jupyter notebook      # opens classic Notebook interface
jupyter lab --no-browser --port 8889   # headless / custom port

Output:

[I 2026-04-25 14:30:01.234 ServerApp] JupyterLab 4.2.0 is running at:
[I 2026-04-25 14:30:01.234 ServerApp] http://localhost:8888/lab?token=abc123...
[I 2026-04-25 14:30:01.234 ServerApp]  or http://127.0.0.1:8888/lab?token=abc123...
[I 2026-04-25 14:30:01.234 ServerApp] Use Control-C to stop this server...

Your browser opens automatically. If it doesn’t, copy the URL from the terminal output.

When / why to use it#

  • Exploratory data analysis where you want to see output inline (plots, DataFrames).
  • Prototyping an algorithm step by step before putting it in a .py module.
  • Writing a technical document or report that mixes prose and code.
  • Teaching or presenting code β€” cells can be run one at a time.

Common pitfalls#

[!WARNING] Execution order is not document order β€” cells can be run in any order. A notebook that runs top-to-bottom passes CI; one where you ran cells out of order may have hidden state. Always use Kernel β†’ Restart & Run All before sharing to verify the notebook runs cleanly from scratch.

[!WARNING] Large notebooks in version control β€” .ipynb files contain cell outputs (images, dataframes) in JSON, making diffs huge. Use nbstripout to strip outputs before committing: pip install nbstripout && nbstripout --install.

[!TIP] Variables set in one cell persist for the rest of the session. If you delete or change a cell, the old value stays in memory until you restart the kernel.

Keyboard shortcuts#

These work in command mode (press Esc to enter, highlighted cell border turns blue):

KeyAction
EnterSwitch to edit mode
Shift+EnterRun cell, move to next
Ctrl+EnterRun cell, stay
Alt+EnterRun cell, insert below
AInsert cell above
BInsert cell below
DDDelete cell
ZUndo cell deletion
MConvert cell to Markdown
YConvert cell to Code
LToggle line numbers
IIInterrupt kernel
00Restart kernel
Shift+MMerge selected cells

In edit mode (green border): standard text editor shortcuts apply, plus Tab for autocomplete and Shift+Tab for docstring pop-up.

Magic commands#

Magic commands start with % (line magic) or %% (cell magic). They run before your Python code is evaluated.

# Time a single expression
%time sum(range(1_000_000))

Output:

CPU times: user 23.4 ms, sys: 0 ns, total: 23.4 ms
Wall time: 23.4 ms
# Benchmark over many runs
%%timeit
total = 0
for i in range(1_000_000):
    total += i

Output:

28.5 ms Β± 312 Β΅s per loop (mean Β± std. dev. of 7 runs, 10 loops each)
%matplotlib inline    # render matplotlib plots inline in the notebook
%who                  # list all variables in scope
%whos                 # list variables with type and value
%env PATH             # show/set environment variable
%run script.py        # run a .py file and import its globals
%load utils.py        # load a file's contents into the current cell
%pwd                  # print working directory
%ls                   # list directory (like shell ls)

Richer example β€” inline plotting#

import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

x = np.linspace(0, 4 * np.pi, 500)
fig, ax = plt.subplots(figsize=(9, 3))
ax.plot(x, np.sin(x), label="sin(x)")
ax.plot(x, np.exp(-x / 5) * np.sin(x), label="damped sin(x)", linestyle="--")
ax.set_title("Waveforms")
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()

Output:

[inline figure rendered in the notebook cell output β€” a waveform plot with two curves]

Export / convert notebooks#

# To HTML (share as a static page)
jupyter nbconvert --to html analysis.ipynb

# To Python script (strip all outputs and markdown, keep code cells)
jupyter nbconvert --to script analysis.ipynb

# To PDF (requires LaTeX)
jupyter nbconvert --to pdf analysis.ipynb

# To Markdown
jupyter nbconvert --to markdown analysis.ipynb

Output:

[NbConvertApp] Converting notebook analysis.ipynb to html
[NbConvertApp] Writing 312345 bytes to analysis.html

List and manage kernels#

jupyter kernelspec list             # list installed kernels
jupyter kernelspec remove myenv     # remove a kernel
pip install ipykernel               # install ipykernel to register a venv
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"

Output of kernelspec list:

Available kernels:
  python3    /usr/local/share/jupyter/kernels/python3
  myenv      /home/user/.local/share/jupyter/kernels/myenv

[!TIP] Always install ipykernel inside the virtual environment you want to use, then register it with python -m ipykernel install --user --name <name>. This makes the venv selectable from the JupyterLab kernel picker.

Strip outputs before committing#

pip install nbstripout
nbstripout --install                # installs a git filter for this repo
# From now on: git diff and git add see clean notebooks (no outputs)