Skip to main content

svg_snip

svg_snip is a Python package that provides a collection of functions for generating SVG snippets. These functions are designed to simplify the creation of SVG elements, such as rectangles, circles, lines, text and more. You can use this to display procedurally generated SVG graphics in your Jupyter notebook.

With an optional install of ipywidgets and ipycanvas, also as an interactive visualization tool, which generates SVG snippets in the backend and performs the rendering in the frontend.

Installation

You can install svg_snip from PyPI using pip:

pip install svg_snip

From source

You can also install directly from the GitHub repository:

pip install git+https://github.com/aaichert/svg_snip.git

Optional extras

The core package only requires numpy and supports SVG generation without Jupyter or Pillow.

  • Pillow is optional and enables embedding PIL images into SVG with svg_snip.Composer.image().
  • IPython, ipywidgets, and ipycanvas are optional and enable Jupyter display helpers.

Install optional extras as needed:

pip install svg_snip[pillow]
pip install svg_snip[jupyter]
pip install svg_snip[full]

Or clone and install locally:

git clone https://github.com/aaichert/svg_snip
cd svg_snip
pip install -e .

Basic Usage

from svg_snip.Composer import Composer
from svg_snip.Elements import circle
svg = Composer([200,200])
svg.add(circle, cx=100, cy=100, r=10, stroke='blue')
print(svg.render())

produces:

<svg width="200" height="200" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
  <circle cx="100.00" cy="100.00" r="10.00" stroke="blue" />
</svg>

Displaying SVG in Jupyter

from svg_snip.Composer import Composer
from svg_snip.Elements as e2d
svg = Composer([250,200])
svg.add(e2d.rect, x=0, y=25, width=250, height= 50, fill='blue')
svg.add(e2d.rect, x=0, y=125, width=250, height= 50, fill='blue')
svg.add(e2d.heart, x=10, y=100, size=100, angle=-45, fill='red')

svg.display(debug=True)

produces:

example.png

This example also demonstrates, that the library is able to correctly collect <defs> for later reference.

Groups

Here is an example that defins a Group. You can add SVG elements to the Group just like you would to a Composer.

This allows you to define complex objects once and re-use them as needed.

You can pass additional attributes, such as a transform to the group

g = Group()
g.add(e2d.rect, x=20, y=20, width=30, height=30, fill='blue')
svg.add(g, transform=f'rotate({10} 50 50)')

Extending the functionality

All you need to call Composer.add is a function that returns a string. Of course the string should be valid <svg>.

Here is a simple example which adds two lines that form an X to an SVG:

def x(x=0, y=0):
    return f""" \
<line x1="{x-5}" y1="{y-5}" x2="{x+5}" y2="{y+5}" stroke-width="2"/>
<line x1="{x-5}" y1="{y+5}" x2="{x+5}" y2="{y-5}" stroke-width="2"/>
"""

You can then use the x function along any other element from svg_snip.Elements, e.g. draw the green x on a white rect

from svg_snip.Composer import Composer
from svg_snip.Elements import rect

svg = Composer((100,100))
svg.add(rect, x=0, y=0, width=100, height=100, fill='white')
svg.add(x, x=50, y=50)
svg.display()

Interactive Visualization in Jupyter

Here is an example using ipywidgets to create a slider that can affect the visualization interactively. Jupyter is decently fast with updating short HTML fragments. You loose most time running the python kernel in the backend and sending resulting text to the frontend.

For debugging and scientific visualizations, this is easily fast enough.

import ipywidgets as w
from IPython.display import clear_output, display

s = w.IntSlider(value=0, min=0, max=360, step=2, description='Angle')
out = w.Output()

def update(c):
    with out:
        clear_output(wait=True)
        svg = Composer((100,100))
        svg.add(e2d.rect, x=0, y=0, width=100, height=100)
        g = Group()
        g.add(e2d.rect, x=20, y=20, width=30, height=30, fill='blue')
        svg.add(g, transform=f'rotate({c["new"]} 50 50)')
        svg.display()

s.observe(update, names='value')
display(s, out)
update({"new": s.value})

example.png

The package ProjectiveGeometry23 can be used for simple 3D graphics.

Please see example_3D_cube.ipynb for more information on how to make advanced use of this library. You can rotate this cube interactively when you run the Jupyter notebook.

example_3D_cube.svg

Here is an example on how to use the library for vector-overlays on existing raster images, e.g. for scientific publication.

example_overlay.svg

The package html_snippets is in an earlier development stage. However, it supports generating animations based on SVG frames for use in HTML documents.

from html_snippets import html_animation_css
import numpy as np

from svg_snip.Composer import Composer
import svg_snip.Elements as e2d
import svg_snip.Elements3D as e3d

from ProjectiveGeometry23.central_projection import ProjectionMatrix
from ProjectiveGeometry23.homography import rotation_x, rotation_z

P_lookat = ProjectionMatrix.perspective_look_at(
    eye=np.array([0, 0, 250]),
    center=np.array([0, 0, 0]),
    image_size=(300, 300),
    fovy_rad=0.7
)

def generate_svg_cube(angle_x, angle_z):
    svg = Composer((300,300))
    svg.add(e2d.rect, fill='white', width=300, height=300)
    svg.add(e3d.wire_cube, min=[-50,-50,-50], max=[50,50,50], stroke='black')
    T = rotation_x(angle_x) @ rotation_z(angle_z)
    return svg.render(P=P_lookat.P @ T)

# Generate an animation
frames = [generate_svg_cube(1,angle) for angle in np.linspace(0, np.pi / 2, 50)]
html_header, html_body = html_animation_css(frames, frame_duration_sec=0.05)
# Store as HTML file
with open('example_animation.html', 'w') as file:
    file.write(html_header+html_body)
    

example_animation.html

License

This project is licensed under the Apache 2.0 license.

Metadata

Release files for svg-snip 1.2.0

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

Source distribution (sdist)

Source distribution for svg-snip 1.2.0
File Size Uploaded Provenance Actions
svg_snip-1.2.0.tar.gz 132.9 kB With provenance Details for svg_snip-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for svg-snip 1.2.0
File Interpreter ABI Platform Actions
svg_snip-1.2.0-py3-none-any.whl Python 3 none any Details for svg_snip-1.2.0-py3-none-any.whl

Total release size: 157.6 kB

Release files / svg_snip-1.2.0.tar.gz

Download URL svg_snip-1.2.0.tar.gz
Size 132.9 kB
Tags Source
SHA-256 checksum
How to use checksums
896c9ccd5bd214212522884624a833f334165b575cca6abf26d1591b96db2b57
BLAKE2b-256 checksum
How to use checksums
1166eca1ed18ad85ad4b9b983280ee34c85363b69b2c8b5536e5fc6e5bddc2ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 30, 2026.

Transparency log

Release files / svg_snip-1.2.0-py3-none-any.whl

Download URL svg_snip-1.2.0-py3-none-any.whl
Size 24.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e19600837ea3ec87de613769d2af9c397971ea6b68c19e8f9c57a64226b9b167
BLAKE2b-256 checksum
How to use checksums
bd96541f3877acfa517e03d4291b2c7c7a3bfd3f7ea0415570b1e3c81ad020dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 30, 2026.

Transparency log

Security for svg-snip 1.2.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for svg_snip-1.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 30, 2026.

Transparency log

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