Jupyter Notebook Cheat Sheet
Jupyter Notebook reference covering magic commands, shell integration, cell types, and essential keyboard shortcuts for efficient interactive computing.
Common Magic Commands
Line and cell magics for the IPython kernel.
%matplotlib inline # render plots inline%timeit sum(range(1000)) # time a single line%%time # time a whole cell (must be first line)%who # list variables in the current namespace%load_ext autoreload%autoreload 2 # auto-reload modules on change!pip install requests # run a shell command
Cell Magics & Shell
Write files and run shell scripts from a cell.
%%writefile script.pyprint("hello from a generated file")%%bashecho "runs as a bash script"ls -la%run script.py # execute an external .py file%pwd # print working directory
Keyboard Shortcuts
Command mode shortcuts (press Esc first).
- Esc, A- insert a new cell above
- Esc, B- insert a new cell below
- Esc, D D- delete the selected cell
- Shift+Enter- run cell and select the next one
- Ctrl+Enter- run the cell in place
- Esc, M- convert cell to Markdown
- Esc, Y- convert cell to code
Cell & Kernel Concepts
Building blocks of a notebook document.
- Code cell- executes in the kernel and shows output below
- Markdown cell- renders formatted text, LaTeX, and images
- Raw cell- passed through unmodified during nbconvert (no execution)
- Kernel- the language runtime (ipykernel for Python) executing your code
- Checkpoint- autosaved notebook snapshot used for recovery
Interactive Widgets & Rich Display
Build interactive UIs with ipywidgets and customize how objects render via the display protocol.
import ipywidgets as widgetsfrom IPython.display import display, HTML@widgets.interact(x=(0, 10), label=["a", "b", "c"])def show(x=5, label="a"): print(x, label)# Manual widget wiringslider = widgets.FloatSlider(min=0, max=1, step=0.05, description="threshold")output = widgets.Output()def on_change(change): with output: output.clear_output() print(f"threshold = {change['new']}")slider.observe(on_change, names="value")display(slider, output)# Custom rich repr for your own classesclass Report: def _repr_html_(self): return "<b>Custom HTML rendering</b>"
Headless Execution: nbconvert & Papermill
Run notebooks as parameterized scripts from the CLI or a pipeline, without opening the UI.
# Execute a notebook end-to-end and overwrite it with outputsjupyter nbconvert --to notebook --execute --inplace analysis.ipynb# Export to a script or HTML reportjupyter nbconvert --to script analysis.ipynbjupyter nbconvert --to html --no-input analysis.ipynb # hide code cells# Papermill: inject parameters into a tagged cell and produce a new output notebookpip install papermillpapermill template.ipynb output.ipynb -p threshold 0.8 -p dataset "prod"
Debugging & Profiling Magics
Drop into the debugger on exception and profile slow cells without leaving the notebook.
%pdb on # auto-launch pdb whenever a cell raises%debug # post-mortem debug the last exception%prun some_function(df) # cProfile a single call, ranked by cumulative time%%prun -s cumulativeresult = expensive_pipeline(df)%load_ext memory_profiler%memit train(model, X, y) # peak memory used by a call%env MY_VAR=value # inspect/set environment variables for the kernel
Kernel Config & Notebook Metadata
Register alternate kernels and inspect the JSON structure behind a .ipynb file.
# Register a virtualenv/conda env as a selectable kernelpython -m ipykernel install --user --name myenv --display-name "Python (myenv)"jupyter kernelspec listjupyter kernelspec remove myenv# A .ipynb is JSON: cells[], each with cell_type, source, outputs, metadata# Cell tags (Edit > Cell Tags in UI) drive tools like papermill and nbconvert# templates, e.g. tagging a cell "parameters" or "remove-cell"
Ecosystem Tools & Extensions
Common tooling that extends notebooks beyond the base kernel loop.
- JupyterLab- the successor UI to classic Notebook, with a file browser, terminals, and extension manager built in
- nbdime- structural diff/merge for notebooks so git diffs show cell-level changes instead of raw JSON noise
- jupytext- pairs a .ipynb with a plain .py/.md file for cleaner version control and code review
- voila- turns a notebook into a standalone, code-hidden interactive web app
- nbval / pytest-notebook- runs notebooks as regression tests, asserting cell outputs match expectations
- JupyterHub- multi-user server that spawns a notebook instance per authenticated user
- ipyparallel- distributes cell execution across a cluster of IPython engines
Restart the kernel and run all cells top-to-bottom (Kernel > Restart & Run All) before trusting a notebook's results — out-of-order execution during interactive editing easily leaves you with state that won't reproduce from a fresh run.