[Baypiggies] Python + SVG

Mark Voorhies mvoorhie at yahoo.com
Tue Dec 14 19:04:30 CET 2010

On Monday, December 13, 2010 08:49:14 pm Keith Dart wrote:
> === On Tue, 12/14, Venkatraman S wrote: ===
> > Has anyone played around creating SVGs with Python? Is this as simple
> > as creating a
> > 'proper' svg compatible XML?
> ===
> I took a cursory look around for such a thing some time ago. I think
> the best option is to use the Inkscape APIs. Inkscape is mostly
> implemented in Python, and uses the lxml module, which is widely
> regarded as being the best XML module for Python.
> Inkscape allows you to write plugins in Python, and has libraries for
> creating SVG. I haven't actually got a chance to really dive in and use
> it, however, but it looks promising.

  Yes, lxml is very good for parsing SVG.  Chapter 12 of Dive into Python 3
(http://diveintopython3.org/) has a good introduction to lxml (in the context
of parsing Atom feeds).
  Inkscape is very good for debugging SVG -- ctrl-shift-X brings up an XML
editor linked to the current document (so you can select graphical objects,
see the corresponding XML elements, edit the XML elements and see the
change in the displayed object, etc.)  The SVGs that Inkscape emits are
a bit heavy (it's adding some higher level information for the benefit of the
user interface) so depending on your use case, it may be simpler to write
SVG directly from Python (similar tradeoffs to generating HTML directly vs.
using a templating engine, etc.)  Another trick is to generate an SVG with
placeholder strings in Inkscape (e.g., for colors, text, etc.) and then
dynamically fill them in from Python.

  If you're generating SVG from data (e.g., plots) Matplotlib 
(http://matplotlib.sourceforge.net/) is very good.


P.S. The current IPython trunk (https://github.com/ipython/ipython) has a nice
Qt interface for playing with Matplotlib and supports copying SVG source
directly from a plot in an interactive session.

More information about the Baypiggies mailing list