Skip to main content

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 captureprint() 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.ipynb after 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

  1. Tracing: The %%pytutor magic compiles the cell code and executes it under sys.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).

  2. 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.

  3. 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.

  4. JupyterLab 4 compatibility: JupyterLab 4's HTML sanitizer (DOMPurify) strips JavaScript and external URLs from untrusted notebooks. The iframe srcdoc approach bypasses this entirely in trusted notebooks. Run jupyter trust your_notebook.ipynb after executing cells with %%pytutor to 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-steps to limit
  • Threads are not traced (only the main thread)

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jupyter_python_tutor-0.3.0.tar.gz (31.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jupyter_python_tutor-0.3.0-py3-none-any.whl (25.4 kB view details)

Uploaded Python 3

File details

Details for the file jupyter_python_tutor-0.3.0.tar.gz.

File metadata

  • Download URL: jupyter_python_tutor-0.3.0.tar.gz
  • Upload date:
  • Size: 31.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for jupyter_python_tutor-0.3.0.tar.gz
Algorithm Hash digest
SHA256 0b461be65a9a7b31c5a2864cc9d61ac085ce8e4d44634b0ac6c20057f5ee84f9
MD5 f3011972f6b1a072b624b6a53ec06122
BLAKE2b-256 71bae45f5e6f177181a2e4137de9c4fa252ef89a943291e827278e37019a0dba

See more details on using hashes here.

File details

Details for the file jupyter_python_tutor-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for jupyter_python_tutor-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1e832d70c968bfc43322c327235f5d98d9dc4543d02dcc1d9ebaf72d52ea6cc0
MD5 5ea8f1e2a6a41dcdfb756f7fc5bcfaec
BLAKE2b-256 68cd740f5e059dfe8809b7619c6b3fd205b5928bcc828eaedcb43e6d8e58780d

See more details on using hashes here.

Release history Release notifications | RSS feed

0.3.3

2 files

0.3.1

2 files

This release

0.3.0 This release

2 files

0.2.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page