Convert Vega-Lite chart specifications to SVG, PNG, or Vega
Project description
Overview
vl-convert-python
is a dependency-free Python package for converting Vega-Lite chart specifications into static images (SVG or PNG) or Vega chart specifications.
Since an Altair chart can generate Vega-Lite, this package can be used to easily create static images from Altair charts.
Installation
vl-convert-python
can be installed using pip with
$ pip install vl-convert-python
Usage
The vl-convert-python
package provides a series of conversion functions under the vl_convert
module.
Convert Vega-Lite to SVG, PNG, and Vega
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.
import vl_convert as vlc
import json
vl_spec = r"""
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {"url": "https://raw.githubusercontent.com/vega/vega-datasets/next/data/movies.json"},
"mark": "circle",
"encoding": {
"x": {
"bin": {"maxbins": 10},
"field": "IMDB Rating"
},
"y": {
"bin": {"maxbins": 10},
"field": "Rotten Tomatoes Rating"
},
"size": {"aggregate": "count"}
}
}
"""
# Create SVG image string and then write to a file
svg_str = vlc.vegalite_to_svg(vl_spec=vl_spec)
with open("chart.svg", "wt") as f:
f.write(svg_str)
# Create PNG image data and then write to a file
png_data = vlc.vegalite_to_png(vl_spec=vl_spec, scale=2)
with open("chart.png", "wb") as f:
f.write(png_data)
# Create low-level Vega representation of chart and write to file
vg_spec = vlc.vegalite_to_vega(vl_spec)
with open("chart.vg.json", "wt") as f:
json.dump(vg_spec, f)
Convert Altair Chart to SVG, PNG, and Vega
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.
import altair as alt
from vega_datasets import data
import vl_convert as vlc
import json
source = data.barley()
chart = alt.Chart(source).mark_bar().encode(
x='sum(yield)',
y='variety',
color='site'
)
# Create SVG image string and then write to a file
svg_str = vlc.vegalite_to_svg(chart.to_json(), vl_version="4.17")
with open("altair_chart.svg", "wt") as f:
f.write(svg_str)
# Create PNG image data and then write to a file
png_data = vlc.vegalite_to_png(chart.to_json(), vl_version="4.17", scale=2)
with open("altair_chart.png", "wb") as f:
f.write(png_data)
# Create low-level Vega representation of chart and write to file
vg_spec = vlc.vegalite_to_vega(chart.to_json(), vl_version="4.17")
with open("altair_chart.vg.json", "wt") as f:
json.dump(vg_spec, f)
How it works
This crate uses PyO3 to wrap the vl-convert-rs
Rust crate as a Python library. The vl-convert-rs
crate is a self-contained Rust library for converting 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
crate. Font metrics and SVG-to-PNG conversions are provided by the resvg
crate.
Of note, vl-convert-python
is fully self-contained and has no dependency on an external web browser or Node.js runtime.
Development setup
Create development conda environment
$ conda create -n vl-convert-dev -c conda-forge python=3.10 deno maturin altair pytest black black-jupyter
Activate environment
$ conda activate vl-convert-dev
Change to Python package directory
$ cd vl-convert-python
Build Rust python package with maturin in develop mode
$ maturin develop --release
Run tests
$ pytest tests
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for vl_convert_python-0.8.0-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd9340ec789c48eebb1beae796667bad4e2a28d7e1f75703ba42703a8d2c61bb |
|
MD5 | 6c9970f4d0b9f9acd84a8f8a065cc7e3 |
|
BLAKE2b-256 | e6b3fb73dc294269caea472ca078eeda16b824b5f42cafe6944a7ddabd111a7f |
Hashes for vl_convert_python-0.8.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bea19149deda7bfffe37ce9812b38bed842671240ad28aee5ae4a19c39cb2833 |
|
MD5 | 89dc006e83d5a8ec8376eb43313d6440 |
|
BLAKE2b-256 | c523829f41edcab40b2fa0eac4b50558f76b66a52d6c49865eb7125c79db20b6 |
Hashes for vl_convert_python-0.8.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15864ba2fa93ef59e00fd47aa315b5814eff9f2e4124a5ced97bf22545cad512 |
|
MD5 | 64ca4071fb7de4014d73382608303260 |
|
BLAKE2b-256 | cb5468fd7167f95887d9b603b47ef6724c2c23be6d2e85532e8197e9f44c3418 |
Hashes for vl_convert_python-0.8.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d903ef060c95d5f2ae287361d0b90e4f4b9235531130a49cc2f9e936692b85b |
|
MD5 | 783adff6760594d2a94d15b2f0a7c4ed |
|
BLAKE2b-256 | 61e56f8eeac36d17d4b1804083b96e8095459c21917a334e4f212b1e00d4c0a8 |
Hashes for vl_convert_python-0.8.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d337c2f96f921fd4b90649d21765be5abb8877ade81dafc17f67e658d4dec0e1 |
|
MD5 | 94646705318b359e61cfae4d186ee5bb |
|
BLAKE2b-256 | 204a2f7a53370d2645202ebce921598cb761269e6496b41ccdd2fb969243da0f |
Hashes for vl_convert_python-0.8.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8210b634735d569016cb56e5d48380341d51eddde7b83e2a99e802db66e9e12 |
|
MD5 | fc9aab181719b90f3129e1f33d25a5ff |
|
BLAKE2b-256 | e8f4c4bda06490ae8797cf0401ae4e5308a6a38117d0e740e85b1c00a6044c7f |
Hashes for vl_convert_python-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6e2cfd278ddc61e82a40648192a965c4bb8f632ce4d2a75c1843fa1b98a9845 |
|
MD5 | c2bfdd4480702021c8f84427727d67da |
|
BLAKE2b-256 | fdf3f16a78529e56d4abc53d897d69a69fb368b37c7c40de244ae9dec2f9f8b0 |
Hashes for vl_convert_python-0.8.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a44715cc40ccaee3d02f2a5d79abc005399c5e80e83b8c388fdec0a9cc2daa73 |
|
MD5 | af61c1e331ba342497f751946cd9adcb |
|
BLAKE2b-256 | 6a78c22b43bbb78647d0b07b43389065e0ddfd307d73461e2093b7f578e5e688 |
Hashes for vl_convert_python-0.8.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c8177e6c14c32a5010ccfb22caa06aa7e9e39736589f4cc29302305d1ccd10d |
|
MD5 | 1578af7e5518482cf7c83502c06de165 |
|
BLAKE2b-256 | a30782082981e5c4e0fbecba1492f9e23cb2635eec5f826b88789a5a0311a5f9 |
Hashes for vl_convert_python-0.8.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ce1b35aabeab483099412dd6a5ce56ecb58a14f8ab968928270c1b37d017208 |
|
MD5 | 6d9a7b4ce9a8c4ebf81f7b4f433debfc |
|
BLAKE2b-256 | 85c9a5393383916384d78f9e5d863ec64a658ec316aa6429bdcb38ca0b1a8ea9 |
Hashes for vl_convert_python-0.8.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2616da2db2eb1b2929edb457f7d3cc6f9b7fc9f50e92c77243ee3033b915d4c |
|
MD5 | 859a892cfa2c0a29fcd88d1e59233eec |
|
BLAKE2b-256 | 8438f9e503598087e43131b9d7dd4cc07be4d11b958488c05155002490623833 |
Hashes for vl_convert_python-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11efe297c4ce22777fde93bb2be9ae4776e254a59690fb392d2cc077687ddaa6 |
|
MD5 | 54f146d16730a9ac6256c0acea1a034a |
|
BLAKE2b-256 | f1c4e1584a2e58f762dd722d95d7e10fee6776a34cc996ea6e210743f04cdbad |
Hashes for vl_convert_python-0.8.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d12f816cb4500e32a808a4690979b4c052f30d058a14abf9843aa734f6ed38a |
|
MD5 | 39541c914e62b7b521dbc536694907d5 |
|
BLAKE2b-256 | b3c1f5069fc4b82cba3db1166f919c43534a7f9ccfd9864c2083637bb2fe76f7 |
Hashes for vl_convert_python-0.8.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fbcd783b0c05feaa5dbf1bfc285c288ed79f86342a3e85d9d2e3ef85ccf3d0f |
|
MD5 | 2b9b5bab84adbb8a98e645f309f198a0 |
|
BLAKE2b-256 | 645be0d099ce899c6b0b0d0f932562750810ff20544ac929838cfe4c51dccbcb |
Hashes for vl_convert_python-0.8.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b19466bb53e54773bc752047342447aa97ccaec5b4100340b54fcbfec9704d1e |
|
MD5 | e5ecac40ba8668e91ef0c5c89f02a7a4 |
|
BLAKE2b-256 | 2af18213168cbee1ac4c18cfc88be38087bcc8f9a82f3e87bed151ce5b8d2388 |
Hashes for vl_convert_python-0.8.0-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52661cc7a8e74df7ae33105f9dba62b1ceeb274f4a0080b92feb14f80b479d41 |
|
MD5 | d3ba4c0cb980f671e1da3fb1fc74494c |
|
BLAKE2b-256 | 7d26e0c38664cf1b6647daa23936ccd426f1d14c0b2509e9aa110d060fdb8a44 |
Hashes for vl_convert_python-0.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 086ca1b38f3f037dd26809133044048f30d139f0b9a1ea79e26c7067cb120dde |
|
MD5 | 50d8b41064d04e50b5f92529ed1315ef |
|
BLAKE2b-256 | 96252a3a1c6a6389da2057d4c64aa753fb526c456bd8e563fffd137f08dc10ff |
Hashes for vl_convert_python-0.8.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 303f3edbcd5ddbaf36568737b7694c9e8dd34f8af016a93f9a7dbdd8ae345e51 |
|
MD5 | 1ac182d526f2f31ab98a547d33d23682 |
|
BLAKE2b-256 | 05d47164ca9c36245b221ef5c0fed229599fbd2692b1bc814e8f68058c5fb84a |
Hashes for vl_convert_python-0.8.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b09cb1ef5133f060ac7bfb838d8147fd80c44c795646f6ab2bd9eaeae320a3a |
|
MD5 | 350e061d12a631b2eafe861dbf5afdec |
|
BLAKE2b-256 | 7a15742f8a03c1a0373f40131f2d6bcc418658920090348aa96218012807beb5 |
Hashes for vl_convert_python-0.8.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ea094b1fcbc038a70dbb04e0ec72ae41ebc51273d30626ae8a96e09b4ac8c7c |
|
MD5 | b2f1751808b3be7e368090d07ceb217d |
|
BLAKE2b-256 | 43fb9e97924ff6060b03ee72e9e40345fc067a39ed8371ec4b90fb7c9985cf8c |
Hashes for vl_convert_python-0.8.0-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 987d765c6a051891e2b8311ae72ba09a0facf6b9bba6a7c0d789a0d2667d7cbb |
|
MD5 | 37cdb15f732f87aada25f07ffa846e42 |
|
BLAKE2b-256 | 91ef32417a9c8c14294e3c1f601dcf6ee2eb35343fac5b3d37814d9338a9cac1 |
Hashes for vl_convert_python-0.8.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3edafe86b13349cda5f4834aa49c845bd68b889d573e660d2d0ed013a4dc65cb |
|
MD5 | 681c5460bd5a9001f355748c8a8b9bed |
|
BLAKE2b-256 | f8af8c33f7768e3ef05fa296e09d4bb567e8363bd28e3c034fcc571adf29df4e |
Hashes for vl_convert_python-0.8.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f9321d5641011c94259e93e868565915f11f9025f1212e34576bd25b548f270 |
|
MD5 | b23cc31657e3779cd177d5e0a75933cc |
|
BLAKE2b-256 | df392bc1d94ef050bc8c8a92ade52412c78a58e51c0cd8622deb1e00b20f8445 |
Hashes for vl_convert_python-0.8.0-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0ac7c7909360570a44c02cc946233cc9331e94d3efe259f16fc3c987b9de543 |
|
MD5 | 4064761466b6378d32340a33775f507c |
|
BLAKE2b-256 | a149b2738f5b51951baaaf7631096a6c4d41a5cd36ee55f7a5c8d51770a816e8 |
Hashes for vl_convert_python-0.8.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 130e4fb85ed325fbd8cc0facb97ca05538e1544622b8833489de8ed66ce1cbfb |
|
MD5 | 15707bc568d8e73f26b35bfd1d5265de |
|
BLAKE2b-256 | 98f749d039ed3a549ffac075e713e6f59afbfe88ff473bf1e1dc3584c20969da |