Skip to main content

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.

  • 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 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:

example.png

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()

example.png

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

svg_snip-1.0.0.tar.gz (46.1 kB view details)

Uploaded Source

Built Distribution

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

svg_snip-1.0.0-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file svg_snip-1.0.0.tar.gz.

File metadata

  • Download URL: svg_snip-1.0.0.tar.gz
  • Upload date:
  • Size: 46.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for svg_snip-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1c5507ff978aedca7d53f070b983e5a847f87f1bae7075330de5949cbc74c9dd
MD5 b31c9d303407c2185eca3ebba2eb4c30
BLAKE2b-256 1251a8d73027d03a4f1a4f00673ab3b6051e5d2b8c6f2e4697d93e1e60b8d5c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for svg_snip-1.0.0.tar.gz:

Publisher: publish.yml on aaichert/svg_snip

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file svg_snip-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: svg_snip-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for svg_snip-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 780d171ea68db1e26d9b9908661c79ecf62a6c8d67c43b6a17836d1513fdfa8e
MD5 eab6665cf00e96ae9a86c3e632035c13
BLAKE2b-256 ff88ca94e155c1ff86bbf53cfb3f3c47e3356b1c425f09d6853b26cdf5826da2

See more details on using hashes here.

Provenance

The following attestation bundles were made for svg_snip-1.0.0-py3-none-any.whl:

Publisher: publish.yml on aaichert/svg_snip

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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