JupyterLab Cheatsheet¶
Installation¶
| Platform | Command |
|---|---|
| Ubuntu/Debian (pip) | sudo apt-get update && sudo apt-get install python3 python3-pip pip3 install jupyterlab |
| Ubuntu/Debian (conda) | conda install -c conda-forge jupyterlab |
| macOS (Homebrew) | brew install python3 pip3 install jupyterlab |
| macOS (conda) | brew install --cask anaconda conda install -c conda-forge jupyterlab |
| Windows (pip) | pip install jupyterlab |
| Windows (Anaconda) | Install Anaconda, then: conda install -c conda-forge jupyterlab |
| Docker | docker pull jupyter/datascience-notebook docker run -p 8888:8888 jupyter/datascience-notebook |
| Upgrade | pip install --upgrade jupyterlab |
| Verify Installation | jupyter lab --version |
Basic Commands¶
| Command | Description |
|---|---|
jupyter lab |
Start JupyterLab server (opens browser automatically) |
jupyter lab --port=8889 |
Start JupyterLab on specific port |
jupyter lab --no-browser |
Start without opening browser |
jupyter lab --notebook-dir=/path/to/dir |
Start with specific working directory |
jupyter lab list |
List all running JupyterLab servers |
jupyter lab stop 8888 |
Stop JupyterLab server on port 8888 |
jupyter lab --version |
Display JupyterLab version |
jupyter --paths |
Show all Jupyter paths (config, data, runtime) |
jupyter lab --generate-config |
Generate default configuration file |
jupyter lab password |
Set password for JupyterLab server |
jupyter lab clean |
Clean up JupyterLab installation |
jupyter kernelspec list |
List all available kernels |
jupyter labextension list |
List all installed extensions |
jupyter trust notebook.ipynb |
Trust a notebook (allow JavaScript) |
jupyter lab --help |
Display help and all available options |
Kernel Management¶
| Command | Description |
|---|---|
jupyter kernelspec list |
List all installed kernels |
python -m ipykernel install --user --name myenv |
Install Python kernel from environment |
python -m ipykernel install --user --name myenv --display-name "Python (myenv)" |
Install kernel with custom display name |
jupyter kernelspec uninstall myenv |
Remove a kernel |
R -e "install.packages('IRkernel'); IRkernel::installspec()" |
Install R kernel |
julia -e 'using Pkg; Pkg.add("IJulia")' |
Install Julia kernel |
conda install -c conda-forge ipykernel |
Install kernel support in conda environment |
jupyter kernelspec remove kernel-name |
Remove kernel specification |
Notebook Conversion (nbconvert)¶
| Command | Description |
|---|---|
jupyter nbconvert --to html notebook.ipynb |
Convert notebook to HTML |
jupyter nbconvert --to pdf notebook.ipynb |
Convert notebook to PDF |
jupyter nbconvert --to python notebook.ipynb |
Convert notebook to Python script |
jupyter nbconvert --to markdown notebook.ipynb |
Convert notebook to Markdown |
jupyter nbconvert --to slides notebook.ipynb |
Convert notebook to reveal.js slides |
jupyter nbconvert --to notebook --execute notebook.ipynb |
Execute notebook and save output |
jupyter nbconvert --execute --to html notebook.ipynb |
Execute and convert to HTML |
jupyter nbconvert --clear-output notebook.ipynb |
Clear all cell outputs |
jupyter nbconvert --to html *.ipynb |
Batch convert all notebooks in directory |
jupyter nbconvert --to pdf --execute notebook.ipynb |
Execute notebook and export to PDF |
Extension Management¶
| Command | Description |
|---|---|
jupyter labextension list |
List all installed extensions |
jupyter labextension install @jupyterlab/toc |
Install table of contents extension |
jupyter labextension uninstall @jupyterlab/toc |
Uninstall an extension |
jupyter labextension update @jupyterlab/toc |
Update specific extension |
jupyter labextension disable @jupyterlab/toc |
Disable extension without uninstalling |
jupyter labextension enable @jupyterlab/toc |
Enable previously disabled extension |
jupyter lab build |
Rebuild JupyterLab (required after extension changes) |
jupyter labextension check-updates |
Check for extension updates |
jupyter labextension develop . --overwrite |
Link extension for development |
Advanced Server Configuration¶
| Command | Description |
|---|---|
jupyter lab --ip=0.0.0.0 |
Allow connections from any IP address |
jupyter lab --ip=0.0.0.0 --no-browser |
Start server for remote access |
jupyter lab --ServerApp.token='mytoken' |
Set custom authentication token |
jupyter lab --ServerApp.allow_origin='*' |
Allow requests from any origin (CORS) |
jupyter lab --ServerApp.allow_remote_access=True |
Enable remote access explicitly |
jupyter lab --ServerApp.iopub_data_rate_limit=1000000000 |
Increase data rate limit for large outputs |
jupyter lab --ServerApp.max_body_size=524288000 |
Increase max upload size (500MB) |
jupyter lab --debug |
Start in debug mode with verbose logging |
jupyter lab --dev-mode --watch |
Start in development mode with auto-rebuild |
jupyter lab --show-config |
Display current configuration |
jupyter lab --ServerApp.base_url='/jupyter' |
Set custom base URL (for reverse proxy) |
jupyter lab --ServerApp.allow_root=True |
Allow running as root user (not recommended) |
Workspace Management¶
| Command | Description |
|---|---|
jupyter lab workspaces export > workspace.json |
Export current workspace layout |
jupyter lab workspaces import workspace.json |
Import workspace layout |
jupyter lab workspaces list |
List all saved workspaces |
jupyter lab workspaces delete workspace-name |
Delete a workspace |
Configuration¶
Main Configuration File¶
Location: ~/.jupyter/jupyter_lab_config.py (Linux/macOS) or %USERPROFILE%\.jupyter\jupyter_lab_config.py (Windows)
Generate default configuration:
Common Configuration Options¶
# ~/.jupyter/jupyter_lab_config.py
c = get_config()
# Server settings
c.ServerApp.ip = '0.0.0.0' # Listen on all interfaces
c.ServerApp.port = 8888 # Default port
c.ServerApp.open_browser = False # Don't open browser automatically
c.ServerApp.token = 'your-secret-token' # Authentication token
c.ServerApp.allow_remote_access = True # Enable remote access
c.ServerApp.allow_origin = '*' # Allow CORS from any origin
c.ServerApp.allow_root = False # Prevent running as root
# Directory settings
c.ServerApp.root_dir = '/path/to/notebooks' # Default working directory
c.ServerApp.preferred_dir = '/path/to/start' # Preferred starting directory
# Resource limits
c.ServerApp.iopub_data_rate_limit = 10000000 # Data rate limit (bytes/sec)
c.ServerApp.iopub_msg_rate_limit = 1000 # Message rate limit
c.ServerApp.rate_limit_window = 3.0 # Rate limit window (seconds)
# Kernel management
c.MappingKernelManager.default_kernel_name = 'python3'
c.MappingKernelManager.cull_idle_timeout = 3600 # Kill idle kernels after 1 hour
c.MappingKernelManager.cull_interval = 300 # Check for idle kernels every 5 min
c.MappingKernelManager.cull_connected = False # Don't cull connected kernels
# File management
c.ContentsManager.hide_globs = [
'__pycache__', '*.pyc', '*.pyo', '.DS_Store', '*.so', '*.dylib'
]
c.FileContentsManager.delete_to_trash = True # Use trash instead of permanent delete
# Security
c.ServerApp.disable_check_xsrf = False # Enable XSRF protection
c.ServerApp.tornado_settings = {
'headers': {
'Content-Security-Policy': "frame-ancestors 'self'"
}
}
# Logging
c.ServerApp.log_level = 'INFO' # Logging level
User Settings Directory¶
Location: ~/.jupyter/lab/user-settings/ (Linux/macOS) or %APPDATA%\jupyter\lab\user-settings\ (Windows)
Example theme settings (@jupyterlab/apputils-extension/themes.jupyterlab-settings):
Password Protection¶
# Set password interactively
jupyter lab password
# Or generate hash programmatically
python -c "from jupyter_server.auth import passwd; print(passwd('your-password'))"
Then add to config:
Common Use Cases¶
Use Case 1: Remote Server Setup¶
# Generate config file
jupyter lab --generate-config
# Set password
jupyter lab password
# Edit config file (~/.jupyter/jupyter_lab_config.py)
# Set: c.ServerApp.ip = '0.0.0.0'
# c.ServerApp.open_browser = False
# c.ServerApp.allow_remote_access = True
# Start server
jupyter lab --no-browser --port=8888
# Access from remote: http://your-server-ip:8888
Use Case 2: Creating a Custom Kernel Environment¶
# Create conda environment
conda create -n myproject python=3.9 pandas numpy matplotlib
# Activate environment
conda activate myproject
# Install ipykernel
conda install ipykernel
# Register kernel with JupyterLab
python -m ipykernel install --user --name myproject --display-name "Python (myproject)"
# Start JupyterLab and select the new kernel
jupyter lab
Use Case 3: Batch Notebook Execution¶
# Execute single notebook
jupyter nbconvert --to notebook --execute analysis.ipynb --output results.ipynb
# Execute all notebooks in directory
for notebook in *.ipynb; do
jupyter nbconvert --to notebook --execute "$notebook" --output "executed_${notebook}"
done
# Execute with timeout and error handling
jupyter nbconvert --to notebook --execute notebook.ipynb \
--ExecutePreprocessor.timeout=600 \
--ExecutePreprocessor.allow_errors=True
Use Case 4: Docker Deployment with Persistent Storage¶
# Create directory for notebooks
mkdir -p ~/jupyter-notebooks
# Run JupyterLab in Docker with volume mount
docker run -d \
-p 8888:8888 \
-v ~/jupyter-notebooks:/home/jovyan/work \
-e JUPYTER_ENABLE_LAB=yes \
--name jupyter \
jupyter/datascience-notebook
# Get access token
docker logs jupyter 2>&1 | grep "token="
# Stop container
docker stop jupyter
# Restart container
docker start jupyter
Use Case 5: Converting Notebooks to Presentation¶
# Convert to reveal.js slides
jupyter nbconvert --to slides presentation.ipynb
# Serve slides with built-in server
jupyter nbconvert --to slides presentation.ipynb --post serve
# Convert with custom template
jupyter nbconvert --to slides --template lab presentation.ipynb
# Export as standalone HTML
jupyter nbconvert --to slides presentation.ipynb --output-dir=./slides/
Keyboard Shortcuts (Command Mode)¶
| Shortcut | Action |
|---|---|
Enter |
Enter edit mode |
Shift + Enter |
Run cell and select below |
Ctrl + Enter |
Run selected cells |
Alt + Enter |
Run cell and insert below |
A |
Insert cell above |
B |
Insert cell below |
DD |
Delete selected cell |
Z |
Undo cell deletion |
M |
Change cell to Markdown |
Y |
Change cell to Code |
Shift + M |
Merge selected cells |
Ctrl + S |
Save notebook |
Shift + L |
Toggle line numbers |
00 |
Restart kernel |
Best Practices¶
-
Use Virtual Environments: Always work within isolated environments (conda or venv) to manage dependencies and avoid conflicts between projects.
-
Regular Kernel Restarts: Restart kernels periodically during long sessions to clear memory and ensure reproducibility. Use "Restart Kernel and Run All Cells" to verify notebook execution order.
-
Version Control Integration: Store notebooks in Git but use
.gitignoreto exclude checkpoint files (.ipynb_checkpoints/). Consider usingnbstripoutto remove output before committing. -
Secure Remote Access: When exposing JupyterLab remotely, always use strong passwords or tokens, enable HTTPS with SSL certificates, and consider using SSH tunneling or VPN for additional security.
-
Resource Management: Configure kernel culling for idle kernels to free up memory, especially on shared servers. Set appropriate timeout values based on typical workflow duration.
-
Modular Code Organization: Keep notebooks focused on specific analyses. Extract reusable functions into separate
.pymodules and import them, rather than duplicating code across notebooks. -
Documentation Habits: Use Markdown cells liberally to document your thought process, methodology, and findings. Include cell execution order dependencies and expected runtime for long-running cells.
-
Extension Discipline: Only install necessary extensions to avoid bloat and potential conflicts. Regularly update extensions and rebuild JupyterLab after updates.
Troubleshooting¶
| Issue | Solution |
|---|---|
| "Port 8888 is already in use" | Use different port: jupyter lab --port=8889 or stop existing server: jupyter lab stop 8888 |
| Cannot connect to kernel | Restart kernel from Kernel menu, or restart JupyterLab server. Check jupyter kernelspec list for valid kernels. |
| "IOPub data rate exceeded" | Increase limit: jupyter lab --ServerApp.iopub_data_rate_limit=1000000000 or add to config file. |
| Extensions not loading | Rebuild JupyterLab: jupyter lab build. Check compatibility with jupyter labextension list. |
| Notebook won't save | Check file permissions, disk space, and that server has write access. Look for .ipynb_checkpoints directory issues. |
| ModuleNotFoundError in notebook | Ensure kernel is using correct environment. Reinstall ipykernel in target environment: python -m ipykernel install --user --name envname |
| High memory usage | Enable kernel culling in config, restart unused kernels, or use %reset magic command to clear variables. |
| Slow notebook loading | Clear output of large notebooks: jupyter nbconvert --clear-output notebook.ipynb. Reduce cell output size. |
| Token/password not working | Regenerate token: jupyter lab password. Check ~/.jupyter/jupyter_server_config.json for conflicts. |
| "403 Forbidden" errors | Disable XSRF check temporarily: jupyter lab --ServerApp.disable_check_xsrf=True (not recommended for production). |
| Extension installation fails | Update Node.js: conda install nodejs or use system package manager. Clear npm cache: npm cache clean --force. |
| Kernel dies immediately | Check kernel logs: jupyter --runtime-dir to find log location. May indicate missing dependencies or memory issues. |
Useful Magic Commands (In Notebooks)¶
| Magic Command | Description |
|---|---|
%lsmagic |
List all available magic commands |
%time statement |
Time execution of single statement |
%%time |
Time execution of entire cell |
%timeit statement |
Time repeated execution for average |
%matplotlib inline |
Display plots inline in notebook |
%load file.py |
Load external Python file into cell |
%run script.py |
Execute external Python script |
%pwd |
Print current working directory |
%cd /path |
Change working directory |
%env |
List environment variables |
%pip install package |
Install package in current kernel |
%conda install package |
Install package via conda |
%%bash |
Execute cell as bash script |
%debug |
Activate interactive debugger |
%reset |
Clear all variables from namespace |