No project description provided
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=1 →
stroke-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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file svg_helpers-0.4.0.tar.gz.
File metadata
- Download URL: svg_helpers-0.4.0.tar.gz
- Upload date:
- Size: 75.7 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
594050336585a7383188e78a4feec7a3424f9eaea316107589f78f8e0fb90369
|
|
| MD5 |
5513dd31c36eae165102831ed36444e4
|
|
| BLAKE2b-256 |
c7521f4c95bb8cf6fc961e3b00879316346b55557d8eb91f8c4cc725b64d9f88
|
File details
Details for the file svg_helpers-0.4.0-py3-none-any.whl.
File metadata
- Download URL: svg_helpers-0.4.0-py3-none-any.whl
- Upload date:
- Size: 7.8 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d871ee273203a7525e05a8f6d48d5f66277fd251dc20cbf91b702a1a797a2438
|
|
| MD5 |
52181b2cee8ff738b675d470d447167d
|
|
| BLAKE2b-256 |
c0c9c623e060dd88e9bd185f76b187fc7ab1554be47e031365297db6d82ac94e
|