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.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b95f24f1e4df8f0e09dc75309c2f6e92cca99ac78f6faeb56a7a84b37aa9d92 |
|
MD5 | 0ca5654a138a5d0ba6012ee7ffa8f0a3 |
|
BLAKE2b-256 | 8602cfb2f8994080d7fe90952eb98b8022640087b48d254a28f41751c20e9af6 |
Hashes for vl_convert_python-0.4.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17a36b4db96e0bed79a2b6989850dd031d36c9b89b5cc7fbfa51607e7c39ec66 |
|
MD5 | d90d266ec682d90b786ba68053ab4477 |
|
BLAKE2b-256 | 93f10508e78f6325f3e4160c9d39e215a8804ca1c2fcfc0aba82e1b04b0cd6c7 |
Hashes for vl_convert_python-0.4.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66984716de79710efc049b6e3bd36252cecc3f35a2b094ebc1e38fa5e8a74805 |
|
MD5 | 4773618a4f3149cbe975961e5d05952b |
|
BLAKE2b-256 | 8e78ff7166be332df9451c69b4c01b1e7fe6b96a87926e95c9da50371ed61539 |
Hashes for vl_convert_python-0.4.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa80e2eb5b265dbaaf58d3f03bb596448561af35a4cefa9bdc4cfb2c5c0408b6 |
|
MD5 | 6eb1dd9ec8b6a076f04d7f4d2ce7c1b9 |
|
BLAKE2b-256 | 49082f2106cd8e19121044d36bb5479271b668637758b4e8a98575e7ab956329 |
Hashes for vl_convert_python-0.4.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ac5fc72738eed688994082ed331a8e3de0f4869de2c44441d0a8ac4cadb0bcc |
|
MD5 | 9e8a0c6f21a5d126b206ec7be28200cd |
|
BLAKE2b-256 | 7dace13293b54b729d5ad155dafdb523612753a91dc4d7da34c8e09796e28cb6 |
Hashes for vl_convert_python-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb94b65b726127f3954e04ecd9056fb0ff32e8f3250f2adac57edd9523c86276 |
|
MD5 | d9897368e89668ea3d0cd38c3902c116 |
|
BLAKE2b-256 | 35ffdbf4c880043c4c772479acbd6554fb28c982d3da462eed44a484d5e73d55 |
Hashes for vl_convert_python-0.4.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ee666464bfabd52261d416a0988507e4e58ebdbbf3cb3f7fbac5cf9525a48fc |
|
MD5 | 065b4057e0f7014409957138565200ac |
|
BLAKE2b-256 | 2d0d2f9c150773ae48121a120efe22bc2f7369156fd0fc908f20dc2f00034b35 |
Hashes for vl_convert_python-0.4.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7673e1e4a2d10a0a89372b49a8a8f2b665d81e3aef2be7bd2ed8341bc73b3021 |
|
MD5 | fa958f8ba8872d12c1fdc893e7f9ad29 |
|
BLAKE2b-256 | 317db4d987e4d7ba2359ee65a197a096e63aece7b17ba3ba7ad5f4b1d3929220 |
Hashes for vl_convert_python-0.4.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf559a3fe5bfe4be854c0d82b2b5cb3afd24597abcb56d30c8d35c4b02f9bb1f |
|
MD5 | 7eafb58d6feb02d14be281ea8de320e2 |
|
BLAKE2b-256 | 430dd0eaf6bf08394a0cb658c832ee85362ea7a797f36e58d87028dd4d24bca8 |
Hashes for vl_convert_python-0.4.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8368c0a8b6dcdfbfb7b964981697865f24086f60d3bd3135763483586f595bd7 |
|
MD5 | 2afe51120e2c6a842de43449f4b5d43e |
|
BLAKE2b-256 | 72fed588f3e0ed658e7b93ecd7a9b13a8dfb5f2be24cf1b626814cd4a9df34f6 |
Hashes for vl_convert_python-0.4.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06ce3b54d280cea636cbca9718d75bfd13d653d38c05833daff4a1ce8d0d45d6 |
|
MD5 | 3f4ec2642a744753a3a5259a850c293e |
|
BLAKE2b-256 | cc9bedff2c788e0e0471c594118eecff1ff9dc2659eea9e0adc15abc98a26b04 |
Hashes for vl_convert_python-0.4.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09089e9d29201f89a06907cafbead63e9ef66023802521133bd001f660d002c6 |
|
MD5 | 9f9b14eb35bc0c3edbfacd99b940254c |
|
BLAKE2b-256 | 83112240f495b3711b4e4527a9d1ef2fcefa14a3564e4e17f1dc5cd157aceffa |
Hashes for vl_convert_python-0.4.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f1cecab7e5660a54a8a9d028fa307be8a712dea0bf364c19053416202efb9ec |
|
MD5 | 2da913b3a9b3b55bcbdac9d76a15b8ff |
|
BLAKE2b-256 | a1e51308472dfef47a346fa51c1dd14386770e212f46a073d2210115ca69874f |
Hashes for vl_convert_python-0.4.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e664444b65da3fe5d83afb39189759b31e1ad6e031e9eacb74227b4ca770236a |
|
MD5 | 96ca69a8341485cb2c8f77197cece8e8 |
|
BLAKE2b-256 | 0eb018e010fc6bb29b9e0725136fc15ed544cc7095d66a30c4cbda78e2bb75ac |
Hashes for vl_convert_python-0.4.0-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be309a365c46385a7fb1a0724d36aa5943325f2748b860ecefaead3a92816aa1 |
|
MD5 | 2766c9891abc4515944fe7aaebc8b948 |
|
BLAKE2b-256 | 6dc88bb75203d57dd50cebca2b97b6c4340d09aa3623cb399a90459b3b5d493f |
Hashes for vl_convert_python-0.4.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ca69cff7e286c0cc9641c562fda9ea22ca7d830ce3ec70b01e91b554fff4699 |
|
MD5 | f766d2e15d680feb3e7eed542355748b |
|
BLAKE2b-256 | 0fc6bcfd11eb3931423d0f88b6df0d313ece2370ebd45f8167e3142134378601 |
Hashes for vl_convert_python-0.4.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c02e61b7d1ca241fbfe19798bd3bb3f96f581fb8020a4cbbc090781befe6c8c |
|
MD5 | d791a11030d29e15e9ddb10b174b2ea9 |
|
BLAKE2b-256 | e33fa4f2fa8f62d52f696525b1cb86e9f89f1d6b16c57f2a90c50125dad54c09 |
Hashes for vl_convert_python-0.4.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d5aaf9ca7e3390b968725f66664553cf96f9a09ca43e975f90f0837a808d836 |
|
MD5 | 4811206cd6e3509af4be048fe3f47f40 |
|
BLAKE2b-256 | 4e7fc404b3ec5e81808ee4d9904136800111387696f0139fe7552053197b07b9 |
Hashes for vl_convert_python-0.4.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 959a7171b7c344c4fc7a5093341cc76075acf11dd6f3a4475467d8006378a6cd |
|
MD5 | 2d7d46254312e66000d86429e27865c4 |
|
BLAKE2b-256 | b63d12101f1dd09c93e951cc8a2e931c35f3c1b7ffbe28b4ea31561be460b2e0 |
Hashes for vl_convert_python-0.4.0-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a5d12993bb4f9eca868d7f30ab38790d8a893efa83872b2e34c853f38412d7a |
|
MD5 | bec1290a2522c80953e3b4966fb97333 |
|
BLAKE2b-256 | d4e3b46ab5c1d2511feb21b0dfb5a32190d951e9cef203724553be4a870a4e5e |
Hashes for vl_convert_python-0.4.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc72f39fab7c1b9b7d8cb89d879705a46ae61c583ef1b9c34e44d62c5a94bde3 |
|
MD5 | 99a820c89bbd9fba3476383f6bc38ec9 |
|
BLAKE2b-256 | c5a30821cfb9816ae36229e861675deac785f3769697b21922c15019f4e6900d |
Hashes for vl_convert_python-0.4.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b699f108562c6f36ff725e2e79716106973412386821bb4f013070dc39a23f9 |
|
MD5 | 2c77944357c563b91f83445134bea858 |
|
BLAKE2b-256 | 8143560a2f0718822ddaa96a0992a3eb59e13ef519a4afb258f370bec9e37218 |
Hashes for vl_convert_python-0.4.0-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06be01eb7967d2fcdafa13d7ca4e51933911fe4ae25c0928a052b31687ae2427 |
|
MD5 | 7aa4758f3f82dbd92229ee2ea0c8d5dc |
|
BLAKE2b-256 | 897005a4032ae592b50b492f9cedb961f90674d38d76af4cbdb885a91bd6320f |
Hashes for vl_convert_python-0.4.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 33d914c98bcd60b82108dea35aa19b04446f27c142776e69545ead7dec2db3ca |
|
MD5 | 892c36cad1fbb8e83982b0085cd7f3f9 |
|
BLAKE2b-256 | fcd36f3e2e12a87bfbb9784b5651798cb23603c2cd8bd534db3b883a85f3ba34 |