Skip to main content

jupytext: cell_metadata_filter: all,-hidden,-heading_collapsed formats: md:myst notebook_metadata_filter: all,-language_info,-toc,-jupytext.text_representation.jupytext_version,-jupytext.text_representation.format_version text_representation: extension: .md format_name: myst kernelspec: display_name: Python 3 language: python name: python3

Licence CC BY-NC-ND - Thierry Parmentelat

+++

Embedding pythontutor.com illustrations in Jupyter

this notebook is stored in text (markdown) format using jupytext, make sure to have this tool installed if you want to open it locally

+++

Installation

pip3 install ipythontutor

+++

Screeshot

As github won't render iframes embedded in a ipynb, here's a screenshot

+++

Basic Usage

# required to load the extension
%load_ext ipythontutor

In its simplest form, the code in the cell is handed over (as python3) to the excellent http://pythontutor.com/

%%ipythontutor
original = reference = [1, 2]
original[0] = 'boom'
print(reference)

Setting sizes

%%ipythontutor height=400 width=850
def fact(n):
    return 1 if n <= 1 else n * fact(n-1)
original = [fact(3), fact(4)]
reference = original[:]
original[0] = 'boom'
print(reference)

Scaling

If your page is smaller than what pythontutor can reasonably work with, you can specify a scaling ratio.

%%ipythontutor width=600 height=200 ratio=0.7
import copy
original = [1, [2, 3]]
reference = copy.deepcopy(original)
original[1][0] = 'boom'
print(reference)

Add a button to open pythontutor in a new tab

%%ipythontutor linkButton=true
def fact(n):
    return 1 if n <= 1 else n * fact(n-1)
original = [fact(3), fact(4)]
reference = original[:]
original[0] = 'boom'
print(reference)

Other settings

The following list shows the settings that can be tweaked on the magic line. See also this page about embedding pythontutor for more details on these settings:

from ipythontutor import Magics
for var, default in Magics.defaults.items():
    print(f"{var:>20} - defaults to - {default:<}")

Note on proto

The default is to use https to reach pythontutor, as this is expected to work in most cases. If that's not working for you, instead of setting proto=http on each magic cell, you can change this globally - like any other default btw - this way:

from ipythontutor import Magics
Magics.defaults['proto'] = 'http'

Using python2

%%ipythontutor py=2
print "Hey"
original = [1, 2]
copy = original[:]
original[0] = 'boom'

See also

Check out another pure approach, that does not rely on pythontutor, at the nbtutor project.

Release files for ipythontutor 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ipythontutor 0.4.0
File Size Uploaded
ipythontutor-0.4.0.tar.gz 5.3 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for ipythontutor 0.4.0
File Interpreter ABI Platform
ipythontutor-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 5.3 MB

Release files / ipythontutor-0.4.0.tar.gz

Download URL ipythontutor-0.4.0.tar.gz
Size 5.3 MB
Tags Source
SHA-256 checksum
How to use checksums
e83dad2298b2ead02378e8cf63da37d6384b7535a41fd94175c961077a6aaf9b
BLAKE2b-256 checksum
How to use checksums
47f06c75bc5ff4605c1355b9465273a6ec3e7b52b009c204309162f38f6dd170
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / ipythontutor-0.4.0-py3-none-any.whl

Download URL ipythontutor-0.4.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b5c80acc34d3ba267caa5035040415fbcf3b0b9ca8f7abd8f952fa65b1c9de45
BLAKE2b-256 checksum
How to use checksums
d4e9caac9de8200d4cb73287df06d5cafea5c742a33204969428b3e908854dae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

1 release file

0.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page