An extension for IPython to embed a pythontutor iframe that can illustrate the code in the current cell.
Project description
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 exepcted 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ipythontutor-0.2.0.tar.gz
.
File metadata
- Download URL: ipythontutor-0.2.0.tar.gz
- Upload date:
- Size: 105.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32a1c6a6095b5ad0b6e993b6a3ce2a468f22f23bed106bb6c3799b85d3a7b7f9 |
|
MD5 | d81e2c41148bf8c250779de2b4df426a |
|
BLAKE2b-256 | aebde6908a8cdea67611aeaea3e01b48a47c3b33ae6e6799dd88fab2959c01b1 |
File details
Details for the file ipythontutor-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: ipythontutor-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 527792d2a248dc9c514e8b7e5cfec0314a745fb4841bb0537b4f853f1d8c4981 |
|
MD5 | 5b2a28c461b5951d9c46e15829ab7460 |
|
BLAKE2b-256 | 45161ea0243040a6f96a2b33d16395ca92b894d67ec36491c8100750d25c0d8d |