Search code examples
pythonsvgsvgwrite

Adding links to SVG elements in Python using SVGWRITE


I am trying to build an SVG that would feature lines, each line would link to a part elsewhere in the same document. I keep getting a ValueError, however, stating that "Invalid children 'line' for svg-element <a>."

This MWE reproduces the error:

import svgwrite

test = svgwrite.Drawing('test.svg', profile='tiny',size=(100, 100))

link = test.add(test.a('http://stackoverflow.com'))
link.add(test.line(start=(0,0),end=(100,100)))

test.save()

I get the same error with other drawing elements (ellipsis, rect, etc), but these are certainly allowed as children of a link.

What am I missing?

Python version: 2.7.10 svgwrite version: 1.1.6 (reported by pkg_resources)


Solution

  • Robert Longson provides working code, but it is not idiomatic from what I've seen. And I think the suggestion that the syntax is "entirely wrong" is misleading (though I'm open to a more convincing demonstration).

    Removing the profile specification in my example fixes it---and works as well in the larger project from which this minimal example was derived. This is not an entirely satisfactory answer to me, because I don't understand, based on reading the tiny spec why links are causing this problem, but it is an answer, which achieves the desired result without departing from the typical uses of svgwrite that I've seen.