···11-[](https://github.com/cduck/drawSvg/blob/v2/examples/logo.ipynb)
11+[](https://github.com/cduck/drawSvg/blob/master/examples/logo.svg)
2233A Python 3 library for programmatically generating SVG images and animations that can render and display your drawings in a Jupyter notebook or Jupyter lab.
44···6677An interactive [Jupyter notebook](https://jupyter.org) widget, `drawsvg.widgets.DrawingWidget`, [is included](#interactive-widget) that can update drawings based on mouse events. The widget does not yet work in Jupyter lab.
8899+910# Install
10111111-~drawsvg is available on PyPI:~ Install the pre-release of drawsvg 2.0:
1212-1212+Drawsvg is available on PyPI:
1313```bash
1414-$ python3 -m pip install -e "git+https://github.com/cduck/drawsvg.git@v2#egg=drawsvg[all]"
1414+$ python3 -m pip install "drawsvg~=2.0"
1515```
1616-~`$ pip3 install "drawsvg[all]"`~
17161818-## Prerequisites (optional)
1717+To enable raster image support (PNG, MP4, and GIF), follow the [full-feature install instructions](#full-feature-install).
19182020-Cairo needs to be installed separately. When Cairo is installed, drawsvg can output PNG or other image formats in addition to SVG. See platform-specific [instructions for Linux, Windows, and macOS from Cairo](https://www.cairographics.org/download/). Below are some examples for installing Cairo on Linux distributions and macOS.
21192222-**Ubuntu**
2020+## Upgrading from version 1.x
23212424-```bash
2525-$ sudo apt-get install libcairo2
2626-```
2222+Major breaking changes:
27232828-**macOS**
2424+- camelCase method and argument names are now snake\_case and the package name is lowercase (except for arguments that correspond to camelCase SVG attributes).
2525+- The default coordinate system y-axis now matches the SVG coordinate system (y increases down the screen, x increases right)
2626+- How to fix `ModuleNotFoundError: No module named 'drawSvg'` (with a capital S)? Either pip install `"drawSvg~=1.9"` or update your code for drawsvg 2.x (for example, change `drawSvg` to `drawsvg` and `d.saveSvg` to `d.save_svg`).
29273030-Using [homebrew](https://brew.sh/):
3131-3232-```bash
3333-$ brew install cairo
3434-```
35283629# Examples
3730···9588d.save_png('example.png')
96899790# Display in Jupyter notebook
9898-d.rasterize() # Display as PNG
9191+#d.rasterize() # Display as PNG
9992d # Display as SVG
10093```
10194···10699import drawsvg as draw
107100108101d = draw.Drawing(400, 200, origin='center',
109109- animation_config=draw.types.SyncedAnimationConfig(
110110- # Animation configuration
111111- duration=8, # Seconds
112112- show_playback_progress=True,
113113- show_playback_controls=True,
114114- )
115115-)
102102+ animation_config=draw.types.SyncedAnimationConfig(
103103+ # Animation configuration
104104+ duration=8, # Seconds
105105+ show_playback_progress=True,
106106+ show_playback_controls=True))
116107d.append(draw.Rectangle(-200, -100, 400, 200, fill='#eee')) # Background
117108d.append(draw.Circle(0, 0, 40, fill='green')) # Center circle
118118-circle = draw.Circle(0, 0, 0, fill='silver', stroke='gray') # Moving circle
109109+119110# Animation
120120-circle.add_key_frame(0, cx=-100, cy=0, r=0, stroke_width=0)
121121-circle.add_key_frame(2, cx=0, cy=-100, r=40, stroke_width=5)
122122-circle.add_key_frame(4, cx=100, cy=0, r=0, stroke_width=0)
123123-circle.add_key_frame(6, cx=0, cy=100, r=40, stroke_width=5)
124124-circle.add_key_frame(8, cx=-100, cy=0, r=0, stroke_width=0)
111111+circle = draw.Circle(0, 0, 0, fill='gray') # Moving circle
112112+circle.add_key_frame(0, cx=-100, cy=0, r=0)
113113+circle.add_key_frame(2, cx=0, cy=-100, r=40)
114114+circle.add_key_frame(4, cx=100, cy=0, r=0)
115115+circle.add_key_frame(6, cx=0, cy=100, r=40)
116116+circle.add_key_frame(8, cx=-100, cy=0, r=0)
125117d.append(circle)
118118+r = draw.Rectangle(0, 0, 0, 0, fill='silver') # Moving square
119119+r.add_key_frame(0, x=-100, y=0, width=0, height=0)
120120+r.add_key_frame(2, x=0-20, y=-100-20, width=40, height=40)
121121+r.add_key_frame(4, x=100, y=0, width=0, height=0)
122122+r.add_key_frame(6, x=0-20, y=100-20, width=40, height=40)
123123+r.add_key_frame(8, x=-100, y=0, width=0, height=0)
124124+d.append(r)
126125127126# Changing text
128127draw.native_animation.animate_text_sequence(
···138137# Display in Jupyter notebook
139138#d.display_image() # Display SVG as an image (will not be interactive)
140139#d.display_iframe() # Display as interactive SVG (alternative)
140140+#d.as_gif('orbit.gif', fps=10) # Render as a GIF image, optionally save to file
141141+#d.as_mp4('orbig.mp4', fps=60) # Render as an MP4 video, optionally save to file
141142d.display_inline() # Display as interactive SVG
142143```
143144···146147Note: GitHub blocks the playback controls.
147148Download the above SVG and open it in a web browser to try.
148149150150+https://user-images.githubusercontent.com/2476062/221400434-1529d237-e9bf-4363-a143-0ece75cd349a.mp4
151151+149152### Patterns and gradients
150153```python
151154import drawsvg as draw
152155153156d = draw.Drawing(1.5, 0.8, origin='center')
154157155155-# Background pattern (not supported by Cairo, rasterize will not show it)
158158+# Background pattern (not supported by Cairo, d.rasterize() will not show it)
156159pattern = draw.Pattern(width=0.13, height=0.23)
157160pattern.append(draw.Rectangle(0, 0, .1, .1, fill='yellow'))
158161pattern.append(draw.Rectangle(0, .1, .1, .1, fill='orange'))
···218221219222# Display
220223d.set_render_size(400)
221221-d.rasterize()
224224+#d.rasterize() # Display as PNG
225225+d # Display as SVG
222226```
223227224228[](https://github.com/cduck/drawsvg/blob/master/examples/example3.svg)
···329333```python
330334import drawsvg as draw
331335from drawsvg.widgets import DrawingWidget
332332-import hyperbolic.poincare.shapes as hyper # pip3 install hyperbolic
336336+import hyperbolic.poincare.shapes as hyper # python3 -m pip install hyperbolic
333337from hyperbolic import euclid
334338335339# Patch the hyperbolic package for drawsvg version 2
···439443
440444441445Note: The above example currently only works in `jupyter notebook`, not `jupyter lab`.
446446+447447+448448+---
449449+450450+# Full-feature install
451451+Drawsvg may be either be installed with no dependencies (only SVG and SVG-native animation will work):
452452+```bash
453453+$ python3 -m pip install "drawsvg~=2.0"
454454+```
455455+456456+Or drawsvg may be installed with extra dependencies to support PNG, MP4, and GIF output:
457457+```bash
458458+$ python3 -m pip install "drawsvg[all]~=2.0"
459459+```
460460+461461+An additional required package, [Cairo](https://www.cairographics.org/download/), cannot be installed with pip and must be installed separately. When Cairo is installed, drawsvg can output PNG and other image formats in addition to SVG. Install it with your preferred package manager. Examples:
462462+463463+**Ubuntu**
464464+465465+```bash
466466+$ sudo apt install libcairo2
467467+```
468468+469469+**macOS**
470470+471471+Using [homebrew](https://brew.sh/) (may require a Python version installed with `brew install python`):
472472+473473+```bash
474474+$ brew install cairo
475475+```
476476+477477+**Any platform**
478478+479479+Using [Anaconda](https://docs.conda.io/en/latest/miniconda.html) (may require Python and cairo installed in the same conda environment):
480480+481481+```bash
482482+$ conda install -c anaconda cairo
483483+```
+4-1
drawsvg/color.py
···66except ImportError as e:
77 raise ImportError(
88 'Optional dependencies not installed. '
99- 'Install with `python3 -m pip install "drawsvg[color]"'
99+ 'Install with `python3 -m pip install "drawsvg[all]"` '
1010+ 'or `python3 -m pip install "drawsvg[color]"`. '
1111+ 'See https://github.com/cduck/drawsvg#full-feature-install '
1212+ 'for more details.'
1013 ) from e
11141215
+8-3
drawsvg/raster.py
···1010 except OSError as e:
1111 raise ImportError(
1212 'Failed to load CairoSVG. '
1313- 'drawSvg will be unable to output PNG or other raster image formats. '
1414- 'See https://github.com/cduck/drawsvg#prerequisites for more details.'
1313+ 'drawSvg will be unable to output PNG or other raster image '
1414+ 'formats. '
1515+ 'See https://github.com/cduck/drawsvg#full-feature-install '
1616+ 'for more details.'
1517 ) from e
1618 except ImportError as e:
1719 raise ImportError(
1820 'CairoSVG will need to be installed to rasterize images. '
1919- 'Install with `pip3 install cairosvg`.'
2121+ 'Install with `python3 -m pip install "drawsvg[all]"` '
2222+ 'or `python3 -m pip install "drawsvg[raster]"`. '
2323+ 'See https://github.com/cduck/drawsvg#full-feature-install '
2424+ 'for more details.'
2025 ) from e
2126 return cairosvg
2227