Home:ALL Converter>Adding links to SVG elements in Python using SVGWRITE

Adding links to SVG elements in Python using SVGWRITE

Ask Time:2018-02-17T23:17:55         Author:cforster

Json Formatter

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)

Author:cforster,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48842782/adding-links-to-svg-elements-in-python-using-svgwrite
Robert Longson :

You seem to be using entirely the wrong syntax for element creation in svgwrite. \n\nAccording to the documentation links are created via a call to svgwrite.container.Hyperlink and lines via a call to svgwrite.shapes.Line\n\nOnce 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.\n\nimport svgwrite\n\ntest = svgwrite.Drawing('test.svg', profile='tiny',size=(100, 100))\n\nlink = test.add(svgwrite.container.Hyperlink('http://stackoverflow.com'))\nlink.add(svgwrite.shapes.Line(start=(0,0),end=(100,100),stroke_width=\"5\",stroke=\"black\"))\n\ntest.save()\n\n\nThis produces the following output\n\n\r\n\r\n<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<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>",
2018-02-17T16:21:49
yy