Skip to main content

Simple classes for SVG generation

Project description

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.

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

pyvector-0.1.3.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

pyvector-0.1.3-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file pyvector-0.1.3.tar.gz.

File metadata

  • Download URL: pyvector-0.1.3.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pyvector-0.1.3.tar.gz
Algorithm Hash digest
SHA256 8d8f98bd3042faa045f38ec66ef619247f90bba688cf2da76b467e473002f480
MD5 85b71197e53ed7c2f0e1005b90ca9009
BLAKE2b-256 419f1ffc453f960901320bae12a0c790abf6a72e8c0c4b4e65846f5ac0111e79

See more details on using hashes here.

File details

Details for the file pyvector-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pyvector-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for pyvector-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 45f8b99394c5ba57d1399774234af4491e8f26a6d11b16544008bd722e31f352
MD5 e4b28a9446c1505653d1e4c8d7ccde1a
BLAKE2b-256 9e81205d2c253e239bcb374697471920aba0ccb5811dcff952c4216e2d588af9

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