jupyter-python-tutor
Python Tutor–style step-by-step code visualization directly inside JupyterLab 4.x and Notebook 7 cells.
Quick Start
pip install jupyter-python-tutor
In a Jupyter cell:
%load_ext jupyter_python_tutor
Then use the %%pytutor cell magic:
%%pytutor
x = 3
y = x + 4
print(y)
An interactive trace appears below the cell — drag the slider to navigate through execution, see variable values change at each line, with SVG arrows from frame variables to heap objects (like Python Tutor).
Features
- Interactive slider navigation — drag the slider (or use ◀ ▶ buttons / ← → arrow keys) to scrub through execution steps
- SVG pointer arrows — arrows from frame variables to heap objects, like OPT_Mentor / Python Tutor
- Single-step view — only the current step is shown (not all steps stacked)
- Call stack visualization — global frame + local frames with line numbers
- Heap objects — lists, dicts, class instances, functions, and other objects shown as boxes with arrows pointing to them
- Resizable columns — drag the vertical dividers between code / variables / objects to resize; double-click a divider to collapse/expand
- Auto-height — iframe height follows the tallest column content (no fixed height, no internal scroll)
- AST-based variable filtering — only variables used by the traced code are shown (no IPython internals, no contamination from other cells)
- stdout capture —
print()output is displayed per step - functools.wraps-safe — functions show their true name (
__code__.co_name), not the wrapper's__name__ - Trusted notebook support — renders fully (with JS, CSS, arrows) in trusted notebooks;
jupyter trust notebook.ipynbafter execution - Works with Notebook 7 — compatible with the new Jupyter Notebook 7 (JupyterLab-based)
- Pure Python — no compiled extensions, works in CPython 3.8+
Options
%%pytutor --max-steps 1000 # Limit trace steps (default: 5000)
%%pytutor --raw # Output trace as JSON instead of HTML
%%pytutor --height 400 # Set minimum widget height (default: 500)
%%pytutor --input 2026 # Pre-collected input for input() calls
Examples
Loops
%%pytutor
total = 0
for i in range(5):
total += i
print(f"Sum: {total}")
Functions with recursion
%%pytutor
def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)
result = factorial(4)
print(result)
Classes and inheritance
%%pytutor
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} says hello"
class Dog(Animal):
def speak(self):
return f"{self.name} barks"
d = Dog("Rex")
print(d.speak())
How It Works
-
Tracing: The
%%pytutormagic compiles the cell code and executes it undersys.settrace(). At each line/call/return event, a snapshot of all local and global variables is captured. AST analysis (ast.parse) extracts the set of names the traced code touches, so only code-relevant variables are shown (filtering out IPython internals and variables from other cells). -
Rendering: The trace steps are rendered as a complete HTML page inside an
<iframe srcdoc="...">. This bypasses JupyterLab 4's HTML sanitizer (which strips<script>,<style>,<button>,<input>, etc. from untrusted output). In trusted notebooks, the iframe renders with full CSS + JavaScript — enabling slider navigation, SVG arrows, draggable dividers, and auto-height. -
Arrows: SVG paths are computed at render time from the bounding boxes of pointer boxes (in frame variables) to heap objects. Arrows redraw on slider navigation, column resize, and window resize.
-
JupyterLab 4 compatibility: JupyterLab 4's HTML sanitizer (DOMPurify) strips JavaScript and external URLs from untrusted notebooks. The iframe
srcdocapproach bypasses this entirely in trusted notebooks. Runjupyter trust your_notebook.ipynbafter executing cells with%%pytutorto ensure the visualization renders correctly.
Limitations
- Trusted notebooks required for full rendering (arrows, slider, dividers). Untrusted notebooks will show a stripped-down version.
sys.settrace()adds overhead — very long loops will be slow; use--max-stepsto limit- Threads are not traced (only the main thread)
License
MIT
Metadata
Release files for jupyter-python-tutor 0.3.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| jupyter_python_tutor-0.3.5.tar.gz | 38.7 kB | Without provenance | Details for jupyter_python_tutor-0.3.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| jupyter_python_tutor-0.3.5-py3-none-any.whl | Python 3 | none | any | Details for jupyter_python_tutor-0.3.5-py3-none-any.whl |
Total release size: 67.6 kB
Release files / jupyter_python_tutor-0.3.5.tar.gz
| Download URL | jupyter_python_tutor-0.3.5.tar.gz |
|---|---|
| Size | 38.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dbb8df7077fc201f0aa8468a83a14b0b3608b3e1b79a91a24d9b28c8ef209abe
|
|
BLAKE2b-256 checksum How to use checksums |
a076b90e49dc62c9dbb91101411f656c6cf506d457361ec70a50b6a39b241604
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.5
|
Release files / jupyter_python_tutor-0.3.5-py3-none-any.whl
| Download URL | jupyter_python_tutor-0.3.5-py3-none-any.whl |
|---|---|
| Size | 28.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
47844ebe2fa266ba6f79bda72698dfac2c5499f22b59be9b1b1551ac826e96a2
|
|
BLAKE2b-256 checksum How to use checksums |
8b85082defba8af80ff4383c0d39d27b9da9472bd34bde3e272301330b850efd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.5
|
Security for jupyter-python-tutor 0.3.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.