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.5.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 848b14c355043a8be057cef4c3b668012329805905179151184e326fd822045b |
|
MD5 | 2c734e95aea6a89d0433497345316275 |
|
BLAKE2b-256 | dd02aa5ba7e3dffb19b9984b3df92083b65044680e25af6897d032ef549f0041 |
Hashes for vl_convert_python-0.5.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0df1a057b13be85e5034866bc157efe64674480e2d5a25bb2d12f0eba2f0c43 |
|
MD5 | ce25fdc66139c79024a33521710e2439 |
|
BLAKE2b-256 | 04a650e7737e9abff22072a27bc16d05aaa6bf27d7e422567b7370ffb029e7d1 |
Hashes for vl_convert_python-0.5.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29bea0d8fbc36a6e03d7ae1a77a7a1301cecb47f48ee3a9e85a42e21b1e332eb |
|
MD5 | dbd55a7f7decc049023ada59d1e24357 |
|
BLAKE2b-256 | d882b0dfe9b3b9f83349578662d7e4b2477b3291c9684b4a3fb137f30cb289ba |
Hashes for vl_convert_python-0.5.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ebefd4b96543d8df70aced6575fa651f8ed9d2a9df589506ff8481bf3cbe834d |
|
MD5 | 9a77be5cd3817de8c486e1477c311fff |
|
BLAKE2b-256 | af8e3db7f5cbfd1466b51a834db3e2c626126e013927bd2254d4ff852c739d84 |
Hashes for vl_convert_python-0.5.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a6ee7f1c45d605f680af4cf63c34c00adf5d667d683bca4bd14db11bba1e2ab |
|
MD5 | ec08a9a45f2dc2885679555156b11583 |
|
BLAKE2b-256 | 63cbba831fd9e95b5b942c4c01838c122d96c0a1ad02b69b07b1dacc55f41edf |
Hashes for vl_convert_python-0.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f70ceddbfb4ef9121fd6382af662e05d56b37733f213fbf0c8df9f6edea42179 |
|
MD5 | 4280f182bd341465c3f66ffabe05ae77 |
|
BLAKE2b-256 | 89d8c1809c516770555ea779b29f412ef7413504851dcf56984b4de06e2c0794 |
Hashes for vl_convert_python-0.5.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dd9f46bcf533c9dba2d5ec6bc65c0fc7ae04eb6dcd300c4e7b1d672ea9670f1 |
|
MD5 | 4730141d795a35f4c7a5b42c68b8fa68 |
|
BLAKE2b-256 | 8c21eec5d7b7445965c779148433a81a37ae31b61c9d39a06301cc7a282bd841 |
Hashes for vl_convert_python-0.5.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcd48924659a1768d0af73a95876e18a6f141c6acb18d980462e8e9cc9fd966e |
|
MD5 | 279be2e5e3469b7384c53a22c88110a6 |
|
BLAKE2b-256 | af7e4440b1e363bb724e9fbf124972fb8a73ddb4dacdcbd9162838b21b38de37 |
Hashes for vl_convert_python-0.5.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65ec9d561efa018a09ca05e9f112e4c39bd1cb6ba019c3ea993b095f3e201535 |
|
MD5 | 0e7d89003e0f727438832b6e5d1554ba |
|
BLAKE2b-256 | 830da66d6bc82c7935a1eb9879205e0eb07d5c7c3359fc0d5f4bfd2161d9b3b5 |
Hashes for vl_convert_python-0.5.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 297edf826ca061cd7c903f5238d8f1af01a2f58469052aefd17215215fe7037b |
|
MD5 | e82121343873a1a645a86bbbfae9b1e3 |
|
BLAKE2b-256 | 62cd84fc618708e3df5a884e3485fe151a1e4514f24a97dc7d3f9613453ea133 |
Hashes for vl_convert_python-0.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34a7bb02de45e6c1a0460ef95a6508cbc466b77a497d2998a1bb80079e535810 |
|
MD5 | ec12c42a47fdc9c8e1ddda8df771aa2f |
|
BLAKE2b-256 | 46294a2cafa42960b3c7d9a9ce69b4ebc52cf9d3182ab3c4d09e6604bd4b82af |
Hashes for vl_convert_python-0.5.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b06d3477d9162a96bfd5657f2fc413c018df715abc7b5d47421934a13a5c1ab |
|
MD5 | 2d06a24b47989d34616603d6797d2118 |
|
BLAKE2b-256 | 280a86fec85ecfd9766fdf8ee396862dcee0b808b8236e48b777941488401da0 |
Hashes for vl_convert_python-0.5.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0717d330fba17ec1404ac4858b9a29227a4382fcdc7d1c44a9e3b964fc037d9e |
|
MD5 | a2549b155e09d899e0807f13b190038c |
|
BLAKE2b-256 | f7b189e0247447724a3abdae505b3cc772d9b5381f203212255c700861723e53 |
Hashes for vl_convert_python-0.5.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e12b8c1895e529a2cb6958af8c585082a4e46d4a84c7fa24ca5f9cb0484e88f |
|
MD5 | a097f46f6d0629b2d6062f1ccc86d741 |
|
BLAKE2b-256 | d29bd5bb2adf5773d65f4c85349e810d78d420ef414f5279ea8fc90ad5cc824a |
Hashes for vl_convert_python-0.5.0-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb5844d5505f67b2d06629efeb8826fedacf3334730fa7923bfaad42086d92ae |
|
MD5 | 1404e655b86853b53a4f2b88f152b33d |
|
BLAKE2b-256 | bf52229fcf840cdc4c03dae73778347a7dd0d66eea8c8fc1da28c983bda7d207 |
Hashes for vl_convert_python-0.5.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17e0715a5c4e37fa9eb98b45a5fa052a739bbc4f5f3e25034fd3b5260ec96f98 |
|
MD5 | d44541055a3b0e1a7d6e8ed88b403160 |
|
BLAKE2b-256 | d6b9d2e7aaea03a7130779121e81f69c845e9a3f2bc8e2764b72ecbe19cf980a |
Hashes for vl_convert_python-0.5.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c2601cc025f11a0936931088b319fa176be4702adada726b3ed963bc290da97 |
|
MD5 | cfb0e41f037c95b1a4a49c5b06e9a41c |
|
BLAKE2b-256 | d08833b4225c016630eca9bcb88213d01716b1b51003d7e55278b79a29817c8c |
Hashes for vl_convert_python-0.5.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9072057a1b127ca67767787ec75414d36346917e7f0e5e85f71b02afca7237b6 |
|
MD5 | f11d12732a9ccbc84de1841c6230f44f |
|
BLAKE2b-256 | cd314e8b18f8c46f4037d2f6ea325195768fc53acb94f1cf2924ff2ceb6d0e58 |
Hashes for vl_convert_python-0.5.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a74dc554669c554cddcfa05dae775949ecf9a1342489a9a06ac27a5f145879f0 |
|
MD5 | c12acb2f850fcea572469ee48a47efb1 |
|
BLAKE2b-256 | 4b7a5c15c843031934c16cb2b496b4fab8378f90232b4cfde8f639a8f18e8b7b |
Hashes for vl_convert_python-0.5.0-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eefbc5e8c3e8b637f8d6738ea1bb3488bdb118e35b32cae52a45a844f656ed48 |
|
MD5 | 136912d364decbd47aa329a2a815f8f1 |
|
BLAKE2b-256 | dc1aedd7e2c4f072561a3e93f6132f4eceff0d9087af4225f07ec53b2f5c0ed9 |
Hashes for vl_convert_python-0.5.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43a0589e32d96b65e1252a2ea0007dc50dbcfc5d929e7d84bd65d93cfdf1e5e3 |
|
MD5 | a0ea39af16e937289e6c3465d8853ec6 |
|
BLAKE2b-256 | 71c0f37f1cff0a6205b81ed35a3620340dc7b21ab8be94f8647bba7cc09d6ca4 |
Hashes for vl_convert_python-0.5.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e92ffbf058ff775b8debbb37bce797a9debd446303968850e8abe02a617a3687 |
|
MD5 | 2efd7b453baea5a825511ee67f44ee5d |
|
BLAKE2b-256 | aad105eea20786e025acecff5b67aa47c442df2106870d4e2c5361784ed0a7f4 |
Hashes for vl_convert_python-0.5.0-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0afe77cc2565b978fcf15c5fb8657babc23c6d7820db6332e7f75a3cd3dfbab7 |
|
MD5 | 9d218d472b47859c0c0722eb2f0f819b |
|
BLAKE2b-256 | 55893a3288bf903ff3e67f13d32c4ee15c616da7569dfba8e78c04d772aa2746 |
Hashes for vl_convert_python-0.5.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b91a13db5137bce9772bda27f8c6b842fd7a4f0ab2fa1cb2ba796384105fdcb |
|
MD5 | 2379d37e77048fe8643bb4e10d9cc2fb |
|
BLAKE2b-256 | 6754725cee0561c2d0ee28f58827060bf009dca52411a5ec7b69e378054111b4 |