Skip to main content

Tools to help make SVG graphics with Python.

Project description

SVG helpers

Tools to help make SVG graphics with python.

pip install svg-helpers
import svg_helpers

width = 150
height = width * 2 / 3
r = height * 3 / 10

# this will make a japanese flag:
svg = svg_helpers.make_svg(width=width, height=height)
svg.add_element("rect", width=width, height=height, fill="white", stroke="#eee")
svg.add_element("circle", cx=width/2, cy=height/2, r=r, fill="#bc002d")
print(svg)

Though with something as simple as this, consider just using f-string formatting. It can sometimes be difficult to get the quotes right, though.

print(f"""
<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}">
  <rect width="{width}" height="{height}" fill="white" stroke="#eee" />
  <circle cx="{width/2}" cy="{height/2}" r="{r}" fill="#bc002d" />
</svg>
""")

Names always match the SVG names. For example, svg rect elements have an x and y property, so use x and y to make a rect. Underscores in attribute names become dashes (stroke_width=1stroke-width="1"), and a trailing underscore is stripped so you can escape Python keywords (class_="hi"class="hi").

Adding raw markup

Adding elements from strings can be helpful for text, especially when it has <tspan> elements in it:

from svg_helpers import make_svg

size = 400
noun = "banana"

svg = make_svg(width=size, height=size)
svg.add_from_string("""
<style>
  .small {
    font: italic 12px serif;
  }
  .small > tspan {
    font: bold 10px sans-serif;
    fill: red;
  }
</style>
""")
svg.add_element("rect", width=size, height=size, fill="white", stroke="#eee")
svg.add_from_string(
    f'<text x="{size/2}" y="{size/2}" class="small">'
    f'You are <tspan>not</tspan> a {noun}!'
    '</text>'
)
print(svg)

Shapes from shapely

If you have shapely installed, you can pass any geometry directly to add_shape and it'll be drawn as a <path> (or a group of paths for compound shapes):

import shapely
from svg_helpers import make_svg

svg = make_svg(width=200, height=200)
svg.add_element("rect", width=200, height=200, fill="white")

# any shapely geometry: Point, LineString, Polygon, MultiPolygon, ...
circle = shapely.Point(100, 100).buffer(50)
svg.add_shape(circle, fill="none", stroke="black", stroke_width=2)

print(svg)

For shapes with many points, pass precision=N to round coordinates and strip trailing zeros:

svg.add_shape(big_polygon, precision=2, fill="red")

Multi-line text

add_text is a convenience for laying out multi-line text using <tspan> children, with vertical_align of "top", "middle", or "bottom":

svg.add_text(
    "first line\nsecond line\nthird line",
    x=100, y=100,
    vertical_align="middle",
    font_family="sans-serif",
)

Alternatives

This is really just a few small functions. If you want something more comprehensive:

Goals

  • friendly syntax that's easy to read and remember. Trying to be as close as is practical to simply writing SVG directly.
  • no baggage. no dependencies. can import just about anywhere, and other projects can import without importing fifteen billion other packages.
  • low maintenance.

Anti-goals

  • comprehensive: it's ok if there are things missing.
  • opinionated: it should not coerce using in a particular way.

License

MIT.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

svg_helpers-0.4.1.tar.gz (76.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

svg_helpers-0.4.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file svg_helpers-0.4.1.tar.gz.

File metadata

  • Download URL: svg_helpers-0.4.1.tar.gz
  • Upload date:
  • Size: 76.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for svg_helpers-0.4.1.tar.gz
Algorithm Hash digest
SHA256 a1cde9fb28a5ae1968b7844ec9b5deca2f0b26be86c6de9376d9af28d0632e66
MD5 60f44fec32b8fad9503d04aa8faec0bd
BLAKE2b-256 8c21e10f687b86ad17bd9130609c9ceaf1db59ea955c8d0c5bad51bccd997ae8

See more details on using hashes here.

File details

Details for the file svg_helpers-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: svg_helpers-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for svg_helpers-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 abcf942cc68d637797e4e49deee9324b283f8db2f029d3891f4011f6a754f004
MD5 cf28351b2d1b99583132a6eda26c2ed6
BLAKE2b-256 e32e53948761468313a16f85e2b997cd1a65bd977e320c09f65d11bbb96ce391

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page