Metadata-Version: 2.4
Name: svg_snip
Version: 1.1.0
Summary: Generate simple SVG snippets for use in Jupyter.
Author-email: Andre Aichert <aaichert@gmail.com>
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/aaichert/svg_snip
Project-URL: Bug Tracker, https://github.com/aaichert/svg_snip/issues
Project-URL: Repository, https://github.com/aaichert/svg_snip.git
Keywords: svg,jupyter,visualization,graphics
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Operating System :: OS Independent
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Multimedia :: Graphics
Classifier: Topic :: Scientific/Engineering :: Visualization
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Provides-Extra: pillow
Requires-Dist: Pillow; extra == "pillow"
Provides-Extra: jupyter
Requires-Dist: IPython; extra == "jupyter"
Requires-Dist: ipywidgets; extra == "jupyter"
Requires-Dist: ipycanvas; extra == "jupyter"
Provides-Extra: full
Requires-Dist: Pillow; extra == "full"
Requires-Dist: IPython; extra == "full"
Requires-Dist: ipywidgets; extra == "full"
Requires-Dist: ipycanvas; extra == "full"
Dynamic: license-file

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

```bash
pip install svg_snip
```

### From source
You can also install directly from the GitHub repository:

```bash
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:

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

Or clone and install locally:

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

## Basic Usage

```py
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:

```html
<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

```py
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](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:

```py
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`

```py
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](example_x.png)


### Advanced examples

Please see [example.ipynb](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.
