vl-convert-python 2.0.0rc2__cp39-abi3-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- vl_convert/__init__.py +5 -0
- vl_convert/__init__.pyi +1843 -0
- vl_convert/py.typed +0 -0
- vl_convert/vl_convert.pyd +0 -0
- vl_convert_python-2.0.0rc2.dist-info/METADATA +228 -0
- vl_convert_python-2.0.0rc2.dist-info/RECORD +9 -0
- vl_convert_python-2.0.0rc2.dist-info/WHEEL +4 -0
- vl_convert_python-2.0.0rc2.dist-info/licenses/LICENSE +11 -0
- vl_convert_python-2.0.0rc2.dist-info/sboms/vl-convert-python.cyclonedx.json +38068 -0
vl_convert/py.typed
ADDED
|
File without changes
|
|
Binary file
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vl-convert-python
|
|
3
|
+
Version: 2.0.0rc2
|
|
4
|
+
Classifier: Programming Language :: Rust
|
|
5
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
6
|
+
Requires-Dist: pytest>=8.3,<9 ; extra == 'test'
|
|
7
|
+
Requires-Dist: inline-snapshot>=0.20,<1 ; extra == 'test'
|
|
8
|
+
Requires-Dist: scikit-image>=0.23.2,<0.24 ; extra == 'test'
|
|
9
|
+
Requires-Dist: pypdfium2>=4.30.0,<5 ; extra == 'test'
|
|
10
|
+
Provides-Extra: test
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Summary: Convert Vega-Lite chart specifications to SVG, PNG, or Vega
|
|
13
|
+
Home-Page: https://github.com/jonmmease/vl-convert
|
|
14
|
+
License: BSD-3-Clause
|
|
15
|
+
Requires-Python: >=3.9
|
|
16
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
17
|
+
|
|
18
|
+
# Overview
|
|
19
|
+
`vl-convert-python` is a dependency-free Python package for converting [Vega-Lite](https://vega.github.io/vega-lite/) chart specifications into static images (SVG or PNG) or [Vega](https://vega.github.io/vega/) chart specifications.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
Since an Altair chart can generate Vega-Lite, this package can be used to easily create static images from Altair charts.
|
|
23
|
+
|
|
24
|
+
Try it out on Binder! \
|
|
25
|
+
[](https://mybinder.org/v2/gh/jonmmease/vl-convert/main?labpath=vl-convert-python%2Fnotebooks%2Fconvert_vegalite.ipynb)
|
|
26
|
+
|
|
27
|
+
# Installation
|
|
28
|
+
`vl-convert-python` can be installed using pip with
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
$ pip install vl-convert-python
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
# Usage
|
|
35
|
+
The `vl-convert-python` package provides a series of conversion functions under the `vl_convert` module.
|
|
36
|
+
|
|
37
|
+
## Convert Vega-Lite to SVG, PNG, and Vega
|
|
38
|
+
The `vegalite_to_svg` and `vegalite_to_png` functions can be used to convert Vega-Lite specifications to static SVG and PNG images respectively. The `vegalite_to_vega` function can be used to convert a Vega-Lite specification to a Vega specification.
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
import vl_convert as vlc
|
|
42
|
+
import json
|
|
43
|
+
|
|
44
|
+
vl_spec = r"""
|
|
45
|
+
{
|
|
46
|
+
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
|
|
47
|
+
"data": {"url": "https://raw.githubusercontent.com/vega/vega-datasets/next/data/movies.json"},
|
|
48
|
+
"mark": "circle",
|
|
49
|
+
"encoding": {
|
|
50
|
+
"x": {
|
|
51
|
+
"bin": {"maxbins": 10},
|
|
52
|
+
"field": "IMDB Rating"
|
|
53
|
+
},
|
|
54
|
+
"y": {
|
|
55
|
+
"bin": {"maxbins": 10},
|
|
56
|
+
"field": "Rotten Tomatoes Rating"
|
|
57
|
+
},
|
|
58
|
+
"size": {"aggregate": "count"}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
# Create SVG image string and then write to a file
|
|
64
|
+
svg_str = vlc.vegalite_to_svg(vl_spec=vl_spec)
|
|
65
|
+
with open("chart.svg", "wt") as f:
|
|
66
|
+
f.write(svg_str)
|
|
67
|
+
|
|
68
|
+
# Create PNG image data and then write to a file
|
|
69
|
+
png_data = vlc.vegalite_to_png(vl_spec=vl_spec, scale=2)
|
|
70
|
+
with open("chart.png", "wb") as f:
|
|
71
|
+
f.write(png_data)
|
|
72
|
+
|
|
73
|
+
# Create low-level Vega representation of chart and write to file
|
|
74
|
+
vg_spec = vlc.vegalite_to_vega(vl_spec)
|
|
75
|
+
with open("chart.vg.json", "wt") as f:
|
|
76
|
+
json.dump(vg_spec, f)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Convert Altair Chart to SVG, PNG, and Vega
|
|
80
|
+
The Altair visualization library provides a Pythonic API for generating Vega-Lite visualizations. As such, `vl-convert-python` can be used to convert Altair charts to PNG, SVG, or Vega. The `vegalite_*` functions support an optional `vl_version` argument that can be used to specify the particular version of the Vega-Lite JavaScript library to use. Version 4.2 of the Altair package uses Vega-Lite version 4.17, so this is the version that should be specified when converting Altair charts.
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
import altair as alt
|
|
84
|
+
from vega_datasets import data
|
|
85
|
+
import vl_convert as vlc
|
|
86
|
+
import json
|
|
87
|
+
|
|
88
|
+
source = data.barley()
|
|
89
|
+
|
|
90
|
+
chart = alt.Chart(source).mark_bar().encode(
|
|
91
|
+
x='sum(yield)',
|
|
92
|
+
y='variety',
|
|
93
|
+
color='site'
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
# Create SVG image string and then write to a file
|
|
97
|
+
svg_str = vlc.vegalite_to_svg(chart.to_json(), vl_version="4.17")
|
|
98
|
+
with open("altair_chart.svg", "wt") as f:
|
|
99
|
+
f.write(svg_str)
|
|
100
|
+
|
|
101
|
+
# Create PNG image data and then write to a file
|
|
102
|
+
png_data = vlc.vegalite_to_png(chart.to_json(), vl_version="4.17", scale=2)
|
|
103
|
+
with open("altair_chart.png", "wb") as f:
|
|
104
|
+
f.write(png_data)
|
|
105
|
+
|
|
106
|
+
# Create low-level Vega representation of chart and write to file
|
|
107
|
+
vg_spec = vlc.vegalite_to_vega(chart.to_json(), vl_version="4.17")
|
|
108
|
+
with open("altair_chart.vg.json", "wt") as f:
|
|
109
|
+
json.dump(vg_spec, f)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Configure Worker Parallelism
|
|
113
|
+
By default, `vl-convert-python` uses `1` converter worker. You can configure this globally:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
import vl_convert as vlc
|
|
117
|
+
|
|
118
|
+
cfg = vlc.get_config()
|
|
119
|
+
print(cfg["num_workers"]) # 1
|
|
120
|
+
|
|
121
|
+
vlc.configure(num_workers=4) # enable parallel worker pool
|
|
122
|
+
vlc.warm_up_workers() # optional: pre-initialize workers before first conversion
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
This setting applies to subsequent conversions and enables parallel work across Python threads.
|
|
126
|
+
|
|
127
|
+
## Google Fonts
|
|
128
|
+
Charts that reference [Google Fonts](https://fonts.google.com/) can download and register them automatically. There are two approaches:
|
|
129
|
+
|
|
130
|
+
### Explicit Registration
|
|
131
|
+
Use `register_google_fonts_font` to download specific font families before conversion:
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
import vl_convert as vlc
|
|
135
|
+
|
|
136
|
+
# Download all variants of Roboto
|
|
137
|
+
vlc.register_google_fonts_font("Roboto")
|
|
138
|
+
|
|
139
|
+
# Download specific weight/style variants
|
|
140
|
+
vlc.register_google_fonts_font("Playfair Display", variants=[(400, "normal"), (700, "italic")])
|
|
141
|
+
|
|
142
|
+
svg_str = vlc.vegalite_to_svg(vl_spec=vl_spec)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Automatic Detection
|
|
146
|
+
Enable `auto_google_fonts` to have vl-convert scan the chart specification for font references and download matching Google Fonts automatically:
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
import vl_convert as vlc
|
|
150
|
+
|
|
151
|
+
vlc.configure(auto_google_fonts=True)
|
|
152
|
+
|
|
153
|
+
# Fonts referenced in the spec are downloaded automatically
|
|
154
|
+
svg_str = vlc.vegalite_to_svg(vl_spec=vl_spec)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Cache Configuration
|
|
158
|
+
Downloaded fonts are cached on disk (default `~/.cache/vl-convert/google-fonts/`). You can limit the cache size:
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
vlc.configure(google_fonts_cache_size_mb=500)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Asyncio API
|
|
165
|
+
An async API with matching function names is available under `vl_convert.asyncio`.
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
import asyncio
|
|
169
|
+
import vl_convert.asyncio as vlca
|
|
170
|
+
|
|
171
|
+
vl_spec = {
|
|
172
|
+
"data": {"values": [{"a": "A", "b": 1}, {"a": "B", "b": 2}]},
|
|
173
|
+
"mark": "bar",
|
|
174
|
+
"encoding": {
|
|
175
|
+
"x": {"field": "a", "type": "nominal"},
|
|
176
|
+
"y": {"field": "b", "type": "quantitative"},
|
|
177
|
+
},
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async def main():
|
|
181
|
+
await vlca.configure(num_workers=4)
|
|
182
|
+
await vlca.warm_up_workers() # optional
|
|
183
|
+
|
|
184
|
+
svg = await vlca.vegalite_to_svg(vl_spec, "v5_16")
|
|
185
|
+
print(svg[:5])
|
|
186
|
+
|
|
187
|
+
svgs = await asyncio.gather(
|
|
188
|
+
*[vlca.vegalite_to_svg(vl_spec, "v5_16") for _ in range(8)]
|
|
189
|
+
)
|
|
190
|
+
print(len(svgs))
|
|
191
|
+
|
|
192
|
+
asyncio.run(main())
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
The top-level sync API (`vl_convert.<function>`) is unchanged. The async namespace is additive.
|
|
196
|
+
|
|
197
|
+
# How it works
|
|
198
|
+
This crate uses [PyO3](https://pyo3.rs/) to wrap the [`vl-convert-rs`](https://crates.io/crates/vl-convert-rs) Rust crate as a Python library. The `vl-convert-rs` crate is a self-contained Rust library for converting [Vega-Lite](https://vega.github.io/vega-lite/) visualization specifications into various formats. The conversions are performed using the Vega-Lite and Vega JavaScript libraries running in a v8 JavaScript runtime provided by the [`deno_runtime`](https://crates.io/crates/deno_runtime) crate. Font metrics and SVG-to-PNG conversions are provided by the [`resvg`](https://crates.io/crates/resvg) crate.
|
|
199
|
+
|
|
200
|
+
Of note, `vl-convert-python` is fully self-contained and has no dependency on an external web browser or Node.js runtime.
|
|
201
|
+
|
|
202
|
+
# Development setup
|
|
203
|
+
Create development conda environment
|
|
204
|
+
```
|
|
205
|
+
$ conda create -n vl-convert-dev -c conda-forge python=3.10 deno maturin altair pytest black black-jupyter scikit-image
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Activate environment and pip install remaining dependencies
|
|
209
|
+
```
|
|
210
|
+
$ conda activate vl-convert-dev
|
|
211
|
+
$ pip install pypdfium2
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Change to Python package directory
|
|
215
|
+
```
|
|
216
|
+
$ cd vl-convert-python
|
|
217
|
+
|
|
218
|
+
```
|
|
219
|
+
Build Rust python package with maturin in develop mode
|
|
220
|
+
```
|
|
221
|
+
$ maturin develop --release
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Run tests
|
|
225
|
+
```
|
|
226
|
+
$ pytest tests
|
|
227
|
+
```
|
|
228
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
vl_convert/__init__.py,sha256=sfoYSsqjBnXQeASPiuxP9kisQrYoK0I4WTMNn2GTgm8,123
|
|
2
|
+
vl_convert/__init__.pyi,sha256=gnS6TAK6PesKURvz-gsGjW7iPKzLbFIRLU4qcUp0zGo,61380
|
|
3
|
+
vl_convert/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
vl_convert/vl_convert.pyd,sha256=9uAljXnu3E5TnI5kf_EFuyUuHIOAkbr6Dd3EiKAWH2U,114790400
|
|
5
|
+
vl_convert_python-2.0.0rc2.dist-info/METADATA,sha256=5geKwnfjtRr29TVICTw-Av20lRtuzxH9-rxQaB0AnCw,7731
|
|
6
|
+
vl_convert_python-2.0.0rc2.dist-info/WHEEL,sha256=xe4_tbg8wYdeh4O7nLbnWckzIQtCR4nODDgwke_TKy8,95
|
|
7
|
+
vl_convert_python-2.0.0rc2.dist-info/licenses/LICENSE,sha256=mo5JuEGkBirmmLrlxBZ8sINIZ-SqgbCoL3UOhwvJE6w,1453
|
|
8
|
+
vl_convert_python-2.0.0rc2.dist-info/sboms/vl-convert-python.cyclonedx.json,sha256=Rh-3zLJfwwZ__g-S-It5B1UacMpIdOJUPKcJblSp7Ao,1293834
|
|
9
|
+
vl_convert_python-2.0.0rc2.dist-info/RECORD,,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Copyright 2023 Jon Mease
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
+
|
|
11
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|