JupyterLab: Extensions, Magic Commands & Best Practices
Magic Commands
# Line magics (single %)
%timeit arr.sum() # benchmark a single expression
%time result = slow_function() # time a single execution
%run script.py # execute a .py file
%load script.py # load file contents into cell
%who # list all variables
%whos # list variables with type/value
%reset # clear all variables
%matplotlib inline # render plots inline
%matplotlib widget # interactive plots (ipympl)
%env MY_VAR=value # set environment variable
%pwd # print working directory
%ls # list files
%cd /path/to/dir # change directory
%history # show cell history
# Cell magics (%% — apply to entire cell)
%%timeit
result = [x**2 for x in range(10000)]
%%bash
echo "Hello from bash"
ls -la
%%writefile myfile.py
def hello():
print("Hello")
%%capture output
import subprocess
result = subprocess.run(['ls'], capture_output=True)
# IPython display utilities
from IPython.display import display, HTML, Image, Markdown, JSON
display(HTML('<b>Bold HTML</b>'))
display(Markdown('# Markdown heading'))
display(Image('plot.png'))Popular Extensions
# Extensions install via pip (JupyterLab 3+)
pip install jupyterlab-git # Git integration in sidebar
pip install jupyterlab-lsp # Language Server Protocol (autocomplete, hover)
pip install python-lsp-server # Python LSP backend for jupyterlab-lsp
pip install jupyterlab_code_formatter # Black/isort formatting
pip install black isort # formatters for above
pip install ipympl # interactive matplotlib (%matplotlib widget)
pip install jupyterlab-drawio # Diagram editor
pip install elyra # AI/ML pipeline builder
pip install nbdime # Notebook diffing/merging
# Check installed extensions
jupyter labextension listnbconvert — Export Notebooks
# Export to other formats
jupyter nbconvert notebook.ipynb --to html
jupyter nbconvert notebook.ipynb --to pdf
jupyter nbconvert notebook.ipynb --to script # .py file
jupyter nbconvert notebook.ipynb --to markdown
jupyter nbconvert notebook.ipynb --to slides # reveal.js
# Execute and export (re-runs all cells first)
jupyter nbconvert notebook.ipynb --to html --execute
jupyter nbconvert notebook.ipynb --execute --inplace # save results back
# Papermill: parameterized execution
pip install papermill
papermill notebook.ipynb output.ipynb -p alpha 0.1 -p n_epochs 10Best Practices
Restart kernel and run all cells (Kernel → Restart Kernel and Run All) before sharing — catches hidden state bugs.
Keep cells short and focused. Long cells with many side effects are hard to re-run selectively.
Use %store variable_name to persist variables across sessions (stored in IPython profile).
Name notebooks descriptively with dates: 2024-03-15-eda-user-churn.ipynb.
Use nbstripout (pre-commit hook) to strip output before git commits — keeps diffs clean.
For production code, refactor notebook logic into .py modules; import and call from notebook.
Use papermill for automated notebook execution with parameter injection (CI/CD reports).
JupyterHub: multi-user server for teams — each user gets an isolated server session.
Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free