Generate simple SVG snippets for use in Jupyter.
Project description
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
Using pip (recommended)
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.
Pillowis optional and enables embedding PIL images into SVG withsvg_snip.Composer.image().IPython,ipywidgets, andipycanvasare 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 import heart, rect
svg = Composer([250,200])
svg.add(rect, x=0, y=25, width=250, height= 50, fill='blue')
svg.add(rect, x=0, y=125, width=250, height= 50, fill='blue')
svg.add(heart, x=10, y=100, size=100, angle=-45, fill='red')
svg.display(debug=True)
produces:
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()
Advanced examples
Please see example.ipynb for more information on how to make advanced use of this library.
License
This project is licensed under the Apache 2.0 license.
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
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_snip-1.1.0.tar.gz.
File metadata
- Download URL: svg_snip-1.1.0.tar.gz
- Upload date:
- Size: 49.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f98d329caaddaeec2ec7ea73b078fbdda26e1c5b52dd79df61f12b161ad48937
|
|
| MD5 |
512b296080479e2bb7f853d8a6d5295d
|
|
| BLAKE2b-256 |
da8a3b92e7ad1041b05598b83712cffc4eed6aca4274897582636d2b9740c893
|
Provenance
The following attestation bundles were made for svg_snip-1.1.0.tar.gz:
Publisher:
publish.yml on aaichert/svg_snip
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
svg_snip-1.1.0.tar.gz -
Subject digest:
f98d329caaddaeec2ec7ea73b078fbdda26e1c5b52dd79df61f12b161ad48937 - Sigstore transparency entry: 1560123932
- Sigstore integration time:
-
Permalink:
aaichert/svg_snip@1466ca13b2a0cc9600a17741412689f1e0393c56 -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/aaichert
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1466ca13b2a0cc9600a17741412689f1e0393c56 -
Trigger Event:
release
-
Statement type:
File details
Details for the file svg_snip-1.1.0-py3-none-any.whl.
File metadata
- Download URL: svg_snip-1.1.0-py3-none-any.whl
- Upload date:
- Size: 22.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
beed5a9485543069175aab39a959f85437c21a9921638f0a3aa03ad6a7ecbe1b
|
|
| MD5 |
a4b130d1d0a72d3c611cde053160c2e2
|
|
| BLAKE2b-256 |
7214f8277078b2277517fdce9bf07b25e9db99bfa5881e6a0b050733fb8c3f9c
|
Provenance
The following attestation bundles were made for svg_snip-1.1.0-py3-none-any.whl:
Publisher:
publish.yml on aaichert/svg_snip
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
svg_snip-1.1.0-py3-none-any.whl -
Subject digest:
beed5a9485543069175aab39a959f85437c21a9921638f0a3aa03ad6a7ecbe1b - Sigstore transparency entry: 1560124102
- Sigstore integration time:
-
Permalink:
aaichert/svg_snip@1466ca13b2a0cc9600a17741412689f1e0393c56 -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/aaichert
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1466ca13b2a0cc9600a17741412689f1e0393c56 -
Trigger Event:
release
-
Statement type: