drawsvg: Programmatic SVG and Animation
drawsvg is a Python 3 library designed for programmatically generating SVG (Scalable Vector Graphics) images and animations. It supports rendering drawings to various formats including PNG and MP4, and allows for interactive display within Jupyter Notebook and Jupyter Lab environments. The library maintains an active development status with regular updates, with version 2.4.1 being the latest stable release.
Common errors
-
ModuleNotFoundError: No module named 'drawSvg'
cause Attempting to import the library using the old `camelCase` package name `drawSvg` (capital S) instead of the current `snake_case` name `drawsvg`.fixChange your import statement from `import drawSvg` to `import drawsvg`. -
AttributeError: 'Drawing' object has no attribute 'saveSvg'
cause Calling a method using the old `camelCase` naming convention from version 1.x on a `drawsvg` 2.x+ object.fixUpdate the method call to `snake_case`, e.g., change `d.saveSvg('file.svg')` to `d.save_svg('file.svg')`. -
Error: Format: "png" not recognized. Use one of: [...]
cause This error typically occurs when trying to use `save_png()` or `save_video()` without the necessary Cairo system library installed, even if `drawsvg[all]` was installed via pip.fixInstall the Cairo library at the system level (e.g., `sudo apt-get install libcairo2` on Ubuntu or `brew install cairo` on macOS).
Warnings
- breaking Upgrading from drawsvg 1.x to 2.x introduces major breaking changes. Method and argument names transitioned from `camelCase` to `snake_case` (e.g., `saveSvg` to `save_svg`), and the package name for imports changed to lowercase (`drawSvg` to `drawsvg`). Additionally, the default coordinate system's Y-axis now matches SVG standards, increasing downwards.
- gotcha Generating raster image formats (PNG, MP4, GIF) with `save_png()` or `save_video()` requires the system-level Cairo library, which cannot be installed via `pip`. You must install it separately using your system's package manager (e.g., `apt-get` on Ubuntu, `brew` on macOS) in addition to `pip install "drawsvg[all]"`.
- gotcha The interactive Jupyter widget, `drawsvg.widgets.DrawingWidget`, currently works reliably in Jupyter Notebook but does not yet function in Jupyter Lab.
- gotcha Custom fonts embedded using `embed_google_font` or similar methods will work in most browsers when viewed as SVG, but they will not be rendered when using `rasterize()`, `save_png()`, or `save_video()` to convert to raster formats.
Install
-
pip install "drawsvg~=2.0" -
pip install "drawsvg[all]~=2.0" -
sudo apt-get install libcairo2 # For macOS with Homebrew: brew install cairo
Imports
- drawsvg
import drawSvg as draw
import drawsvg as draw
Quickstart
import drawsvg as draw
import os
# Create a new drawing with a viewBox of 200x100, origin at center
d = draw.Drawing(200, 100, origin='center')
# Draw a red circle centered at (0,0)
d.append(draw.Circle(0, 0, 40, fill='red'))
# Draw a blue rectangle
d.append(draw.Rectangle(-90, -45, 60, 30, fill='blue', stroke='black', stroke_width=2))
# Draw some text
d.append(draw.Text('Hello drawsvg!', 20, 0, 30, center=True, fill='green'))
# Save the drawing to an SVG file
d.save_svg('example.svg')
# To display in a Jupyter Notebook, simply put 'd' as the last line in a cell:
# d
# To save as PNG (requires Cairo and 'drawsvg[all]' installation)
# if os.path.exists('/usr/bin/cairo-svg'): # Simple check for Cairo-SVG presence
# d.save_png('example.png')
# else:
# print("Cairo not installed, cannot save PNG. See 'install' section.")
print("Generated example.svg")