Skip to main content

pyvector - Simple Python Classes for easier SVG generation

The module contains the most important SVG tags as classes with their most used attributes by default to reduce the need of writing SVG markup inside your Python code.

Installation

pyvector is available on PyPI:

    pip install pyvector

Usage

Quickstart

For the first steps, let's draw a purple rect in a g:

    from pyvector import SVG, Group, Rect
    svg = SVG()
    g = Group()
    svg.add(g)
    r = Rect(x=1, y=1, width=100, height=20)
    r.style.fill = '#FF00FF'
    g.add(r)
    svg.auto_resize()
    print(svg)

Output:

    <?xml version="1.0" encoding="UTF-8"?>
    <svg viewBox="1 1 100 20" xmlns="http://www.w3.org/2000/svg">
    <g>
    <rect x="1" width="100" y="1" height="20" style="fill:#FF00FF;"/>
    </g>
    </svg>

Result:

rect.svg

Classes

The main focus of this package is to provide auxiliary classes to write legible SVG code from within Python. Thus, the following classes simply represent the most used SVG tags with their inherent properties for code completion in most IDEs.

  • SVG: Top level container class for the whole canvas.
  • Group: g container class for grouping elements.
  • Text: Stores text in the .text property.
  • Rect: Draws rectangles.
  • Circle: Draws circles.
  • Ellipse: Draws ellipses.
  • Line: Draws a line from x1,y1 to x2,y2.

Transformations

All Elements can be transformed by .translate(), .scale() or .rotate(). By default these transformations are simply passed to the transform property of the respective element. By invoking .apply_transform() all non-rotational transformations are applied to the element (and its children, in the case of containers) directly.

Beware: In the current implementation only a few transformations are implemented and these are most likely not even correct. Just don't use .apply_transform() for now!

Styling

Each element has a style attribute, which in turn can have further attributes that correspond to the style="fill: #F00; stroke: #555;" attribute in SVG tags.

Alternatively SVG let's you put style attributes directly into the SVG element <rect fill="#F00" stroke="#555">. This can be achieved by liberally adding properties to an element's instance on the fly. Note, that attributes with hyphens have to be written with an underscore in python (to work around the minus sign). All underscores in attribute names will be replaced by hyphens during rendering. Not sure, who ever thought fontminussize made for a good attribute name.

The third option is to provide styling through a style sheet in CSS by setting the SVG.CSS property which is global to the SVG class. To style individual elements by .class selection, elements have to be given a class name by setting the .class_name property. This is a workaround for the fact that python syntax does not allow you to set the object.class property. So tag.class_name = 'grid' will be used as <tag class="grid"> during rendering.

Viewbox

SVG tries to calculate and set the viewBox attribute automatically so, all elements inside are visible in the resulting image when .auto_resize() is called. So do this just before rendering to see the proper image in most preview programs (that don't let you zoom out).

Troubleshooting

If you find a bug, gross inconsistencies or just have a question regarding this project please contact me via github or open an issue. I would love some feedback on this package.

Metadata

Release files for pyvector 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyvector 0.1.3
File Size Uploaded Provenance Actions
pyvector-0.1.3.tar.gz 5.4 kB Without provenance Details for pyvector-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyvector 0.1.3
File Interpreter ABI Platform Actions
pyvector-0.1.3-py3-none-any.whl Python 3 none any Details for pyvector-0.1.3-py3-none-any.whl

Total release size: 11.1 kB

Release files / pyvector-0.1.3.tar.gz

Download URL pyvector-0.1.3.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8d8f98bd3042faa045f38ec66ef619247f90bba688cf2da76b467e473002f480
BLAKE2b-256 checksum
How to use checksums
419f1ffc453f960901320bae12a0c790abf6a72e8c0c4b4e65846f5ac0111e79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.1 importlib-metadata/4.8.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release files / pyvector-0.1.3-py3-none-any.whl

Download URL pyvector-0.1.3-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
45f8b99394c5ba57d1399774234af4491e8f26a6d11b16544008bd722e31f352
BLAKE2b-256 checksum
How to use checksums
9e81205d2c253e239bcb374697471920aba0ccb5811dcff952c4216e2d588af9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.1 importlib-metadata/4.8.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Security for pyvector 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page