Python Turtle graphics for Jupyter notebooks
For a quick demo, open the lab notebook.
To use on a local Jupyter Notebook,
just download the jupyturtle.py file to the same folder where your notebook is saved,
and import it.
(I need to figure out how to deploy on Binder because Jupyturtle is not on conda forge yet
)
jupyturtle offers only a small subset of the features of Python's standard turtle
module.
But it has all the features required to solve the exercises in the book
Think Python, Third Edition, by
Allen Downey.
The idea and some of the code for this module came from Tolga Atam's ColabTurtle, which I discovered while reviewing a pre-print version of Downey's Think Python, Third Edition (O'Reilly, 2024).
Atam's best idea was to use SVG for drawing,
which makes the code simple and lightweight,
requiring only the Python standard library and the
ipython module that is always available in Jupyter.
This is a rewrite from scratch, using classes to model the turtle
and the drawing.
My goal was to make jupyturtle easier to understand and extend by
encapsulating the state and avoiding code duplication.
I used metaprogramming techniques to build the procedural API
with global functions like fd() to move the turtle.
The techniques are easier to understand in the didactic project
abacus.
jupyturtle2
is a fork that uses
ipycanvas
to draw pixels on an HTML canvas, instead of generating SVG,
so it handles complex drawings better.
But jupyturtle2 has two main drawbacks:
-
ipycanvasrequires the binary dependenciesnumpyandpillow, which may be harder to install in some environments. -
the generated drawings are not saved with the notebook like most other output cells; this is also a limitation of
ipycanvas