{"id":7191,"library":"drawsvg","title":"drawsvg: Programmatic SVG and Animation","description":"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.","status":"active","version":"2.4.1","language":"en","source_language":"en","source_url":"https://github.com/cduck/drawsvg","tags":["SVG","vector graphics","drawing","animation","Jupyter","data visualization"],"install":[{"cmd":"pip install \"drawsvg~=2.0\"","lang":"bash","label":"Base installation (SVG only)"},{"cmd":"pip install \"drawsvg[all]~=2.0\"","lang":"bash","label":"Full-feature installation (includes raster support dependencies)"},{"cmd":"sudo apt-get install libcairo2\n# For macOS with Homebrew: brew install cairo","lang":"bash","label":"System-level dependency for raster output (Cairo)"}],"dependencies":[{"reason":"Required for raster image output (PNG, MP4, GIF) using `save_png()` or `save_video()`. Must be installed at the system level.","package":"Cairo","optional":true}],"imports":[{"note":"The package name in Python 2.x and early 1.x versions used 'drawSvg' (camelCase). Version 2.x+ uses 'drawsvg' (snake_case).","wrong":"import drawSvg as draw","symbol":"drawsvg","correct":"import drawsvg as draw"}],"quickstart":{"code":"import drawsvg as draw\nimport os\n\n# Create a new drawing with a viewBox of 200x100, origin at center\nd = draw.Drawing(200, 100, origin='center')\n\n# Draw a red circle centered at (0,0)\nd.append(draw.Circle(0, 0, 40, fill='red'))\n\n# Draw a blue rectangle\nd.append(draw.Rectangle(-90, -45, 60, 30, fill='blue', stroke='black', stroke_width=2))\n\n# Draw some text\nd.append(draw.Text('Hello drawsvg!', 20, 0, 30, center=True, fill='green'))\n\n# Save the drawing to an SVG file\nd.save_svg('example.svg')\n\n# To display in a Jupyter Notebook, simply put 'd' as the last line in a cell:\n# d\n\n# To save as PNG (requires Cairo and 'drawsvg[all]' installation)\n# if os.path.exists('/usr/bin/cairo-svg'): # Simple check for Cairo-SVG presence\n#    d.save_png('example.png')\n# else:\n#    print(\"Cairo not installed, cannot save PNG. See 'install' section.\")\n\nprint(\"Generated example.svg\")","lang":"python","description":"This example demonstrates how to create a simple SVG drawing with a circle, rectangle, and text. It then saves the output to an SVG file. Instructions for displaying in Jupyter Notebook and saving to PNG are also included, highlighting the optional dependencies for the latter."},"warnings":[{"fix":"Update all method/argument calls and import statements to use `snake_case` and the lowercase package name `drawsvg`. Adjust Y-coordinate logic where necessary.","message":"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.","severity":"breaking","affected_versions":"1.x to 2.x"},{"fix":"Install Cairo on your operating system (e.g., `sudo apt-get install libcairo2` on Ubuntu, `brew install cairo` on macOS) and ensure `drawsvg` is installed with the `[all]` extra.","message":"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]\"`.","severity":"gotcha","affected_versions":"2.x+"},{"fix":"If interactive widget functionality is crucial, use Jupyter Notebook instead of Jupyter Lab.","message":"The interactive Jupyter widget, `drawsvg.widgets.DrawingWidget`, currently works reliably in Jupyter Notebook but does not yet function in Jupyter Lab.","severity":"gotcha","affected_versions":"2.x+"},{"fix":"For raster output, ensure you are using system-installed fonts or fallback fonts that are universally available and don't rely on embedded custom fonts.","message":"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.","severity":"gotcha","affected_versions":"2.x+"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Change your import statement from `import drawSvg` to `import drawsvg`.","cause":"Attempting to import the library using the old `camelCase` package name `drawSvg` (capital S) instead of the current `snake_case` name `drawsvg`.","error":"ModuleNotFoundError: No module named 'drawSvg'"},{"fix":"Update the method call to `snake_case`, e.g., change `d.saveSvg('file.svg')` to `d.save_svg('file.svg')`.","cause":"Calling a method using the old `camelCase` naming convention from version 1.x on a `drawsvg` 2.x+ object.","error":"AttributeError: 'Drawing' object has no attribute 'saveSvg'"},{"fix":"Install the Cairo library at the system level (e.g., `sudo apt-get install libcairo2` on Ubuntu or `brew install cairo` on macOS).","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.","error":"Error: Format: \"png\" not recognized. Use one of: [...]"}]}