1

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)

cforster
  • 577
  • 2
  • 7
  • 19

2 Answers2

2

You seem to be using entirely the wrong syntax for element creation in svgwrite.

According to the documentation links are created via a call to svgwrite.container.Hyperlink and lines via a call to svgwrite.shapes.Line

Once that's fixed you still won't see anything as a line without a stroke set is invisible. I've added a stroke and set the stroke width wider than normal below so there's something to click on.

import svgwrite

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

link = test.add(svgwrite.container.Hyperlink('http://stackoverflow.com'))
link.add(svgwrite.shapes.Line(start=(0,0),end=(100,100),stroke_width="5",stroke="black"))

test.save()

This produces the following output

<?xml version="1.0" encoding="utf-8" ?>
<svg baseProfile="tiny" height="100" version="1.2" width="100" xmlns="http://www.w3.org/2000/svg" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xlink="http://www.w3.org/1999/xlink"><defs /><a target="_blank" xlink:href="http://stackoverflow.com"><line stroke="black" stroke-width="5" x1="0" x2="100" y1="0" y2="100" /></a></svg>
Robert Longson
  • 118,664
  • 26
  • 252
  • 242
  • Thanks for the suggestion; the syntax in my MWE does work, calling the created objects rather than the module itself seems more typical from examples of svgwrite use you can find. (I'm not really a programmer, and esp. not a OO programmer). You're absolutely right that without specifying `stroke_width` and `color` nothing would be visible Your version works. But just removing `profile='tiny'` will also work. – cforster Feb 17 '18 at 17:19
0

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.

cforster
  • 577
  • 2
  • 7
  • 19