If you’re using the Python turtle module to teach students, or you just like
using the turtle module yourself, this library can save the images from a turtle
script as SVG files. Experiment with your turtle code using the regular turtle
or the Live Coding in Python plugin for PyCharm, then pass an SvgTurtle
to
the same code, and save it as an SVG file. If you want to produce other file
formats, use svglib to convert the SVG to PDF, PNG, GIF, JPG, TIFF, and PCT,
among others.
Installing
Install it with pip install svg_turtle
. If you haven’t installed Python
packages before, read Brett Cannon’s quick-and-dirty guide.
Drawing
Once it’s installed, create an SvgTurtle
, telling it how big to make the SVG
file. Then give it some turtle commands, and save the file.
from svg_turtle import SvgTurtle
t = SvgTurtle(500, 500)
t.forward(200)
t.dot(10)
t.save_as('example.svg')
IPythonTurtle: IPython integration
To use SvgTurtle with IPython integration, create an instance of the
IPythonTurtle
class. It exposes the same interface as SvgTurtle
. It implements
both display on implicit return, and explicit display via the show()
method.
from svg_turtle import IPythonTurtle
t = IPythonTurtle(500, 500)
t.forward(200)
t.show() # explicit display
t.dot(10)
t # display via implicit return
More Information
If you’d like to help out with the project, see the CONTRIBUTING.md
file in
the source code.