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.1-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e24b25fd0d7e7fd77269054b29caf732c84bc7ec025980562f5e5e779f2fbb12 |
|
MD5 | f4a2dc21bb74f6ec8e7e117077f8f56a |
|
BLAKE2b-256 | 2c92ae4abbb4d7a466bf2692e29e4ede5435b4f19178d1442c5a81e4c75df946 |
Hashes for vl_convert_python-0.8.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d4eb62579764c99a4ce7830baf9367e46b7725491af44693ba18083d0212a88 |
|
MD5 | cba517ddcbfe81e835e7cc00090a90e4 |
|
BLAKE2b-256 | a76d7c44b115d0126f6ab926be388aceb9e44424c82528a2dcdef9617f277bb0 |
Hashes for vl_convert_python-0.8.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5da89ef55ba818270c5ec2bcebc73d4df5a97254bec48f6192a30e3ac67522fd |
|
MD5 | dbce5f156a20283e9b395042f5db26da |
|
BLAKE2b-256 | bfa7526ef7fb3e5219e760c8c7912d09493428241e22c467984194f18e63fba7 |
Hashes for vl_convert_python-0.8.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a9a206c05d2d0d53eced3b8025250cfa8a20420ef26d442852c2603b31d63cf |
|
MD5 | 259cf60a5f9ad0895c42a2f097b236ad |
|
BLAKE2b-256 | 89778a0b8b2f96964a074007a5d40c372d6cc8efde4b8fee7b1cb86a72e2a0e3 |
Hashes for vl_convert_python-0.8.1-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99a0ee31d02db48684c215dd322c44f56a671af9242e45a0b0d9a0b769b6c0ed |
|
MD5 | 54961880dc3a12684e5fc52c182d3098 |
|
BLAKE2b-256 | a8a25b02b9b9d8545ef0c8c9272af6d722ba7123d8177fd7a0a5136e0ba65502 |
Hashes for vl_convert_python-0.8.1-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc020c2cea4be72fccebc62ecca7655c413aadab80d60cbc413d3340abcae6d9 |
|
MD5 | d3f2b96bdde65db91a2fe64c3f1f2d82 |
|
BLAKE2b-256 | 321ae6992224eeb1912960e701e5413c2a1cbd33410f247765b5d3a168b777eb |
Hashes for vl_convert_python-0.8.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5573c2cd6405756bcf5027131241025dd2b20fc78ce2e4e3d61777c0c335c02c |
|
MD5 | 52657531153991b474f8e3a94ccff682 |
|
BLAKE2b-256 | 3415dff545a6a50cda349b47e98c1313da832efb214cdfeae92d646fa2723dc2 |
Hashes for vl_convert_python-0.8.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a415dbd35d9827fbdf9430368adbf70d50234845550f04d88c0e5a6885f2481d |
|
MD5 | 65cd9e0541d6107f6b568be94bf5969a |
|
BLAKE2b-256 | 5b7c87b07ddc891fa08ba00c1bff1b68a62b94bdcdccb00ea42202668d1d95a6 |
Hashes for vl_convert_python-0.8.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53f331f2ff855abcedbe5ab899fd3cabe47624d6c6e7202b5b7e677e3d268c4d |
|
MD5 | b08f49fa621716068813e708e03725ff |
|
BLAKE2b-256 | cc6f327db0ebdd82d885ec80d0ff19c06bf028826ed8f3587913e3a6d490beca |
Hashes for vl_convert_python-0.8.1-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0edf741856fa855dd23a62e2f2c2604bd76076b2a16a0e30654f7ad12d979479 |
|
MD5 | 65d49780773329d10f45eb2a4e833283 |
|
BLAKE2b-256 | 8c9a1d8c41e00b8b5e9ac378f1b44a7b9fe6b19009390050b2fdb8381ac5bd49 |
Hashes for vl_convert_python-0.8.1-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a228d819389a9726cfb8f357743110fcfc6ffd25d3bc0ff5766b8c64170b575a |
|
MD5 | 82a95c9148fed320c78d91b822e8dc3e |
|
BLAKE2b-256 | a04836e967252aeae9adb6273f3f90febeabf28d5bd48180d0aa3eefcfa99b74 |
Hashes for vl_convert_python-0.8.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d33bff33664cb5eb1151434d4cb7a0812b897b857698fa60e989eeaa358f447 |
|
MD5 | e2aafa71b68c41689d71bd133c0eba20 |
|
BLAKE2b-256 | f0de12d8840b43ede01e14a241b54095f1e002f9d2d807fd7a5416cc99db8cc1 |
Hashes for vl_convert_python-0.8.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cf62189395ae5cb83bb4c7831b8b99e7868128837ed6967e3a32bc747e01f8f |
|
MD5 | e3de9994b99355e6dfcbf1833c21e861 |
|
BLAKE2b-256 | c129785717c3b297b906692bb2e77a1ae1c5ba09e4a2baf7cb44c8705fb053a7 |
Hashes for vl_convert_python-0.8.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c771f8995c8a995e35042e92975d92a250ed7b569e45b1d4ca4378c215262df0 |
|
MD5 | b662e8a8f28d27e3b5e0947477813c1a |
|
BLAKE2b-256 | 469893c82e40c74343940b17a5fed555575bf234df66a93a2a5258868738fec9 |
Hashes for vl_convert_python-0.8.1-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74bb68d7024130dd87a18dbf5e6cb25feb11e7192885967c44dccd4bcb36636d |
|
MD5 | af1a8b89eab71086781fd956940c30f4 |
|
BLAKE2b-256 | 892df6479d7dc0b881d36e42a0f7c7b3f34bd64dbd89d1a2b3177b46ed68a6f1 |
Hashes for vl_convert_python-0.8.1-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3dc425f0062343742f5eee158cfe7850d596b101c5c0e1f8d178528a580d2e5b |
|
MD5 | 21a8a985a5b4f493b1f2ac10ea8fad8a |
|
BLAKE2b-256 | 7b300b4f54dce9cf0b75f2e455270286e317d67d2595687d57d068ccdf38cb59 |
Hashes for vl_convert_python-0.8.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f85e6ad4ff9bd9fd297c1aebd00c42738a0dab1c2e175ce4c2bd5f313b3c409 |
|
MD5 | 87b0f5c6a55750520170e28ea49decb8 |
|
BLAKE2b-256 | adabb594db86e85de1ce5b093dd15ac1d6b16515dc7f02d2b3aa3276c52bbd92 |
Hashes for vl_convert_python-0.8.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | abee1c03726673082a2a94e814b9b6b5fdbde6f7b40f098f7338a43a73161086 |
|
MD5 | 72f745c7f5bca206dda78b9755ffd897 |
|
BLAKE2b-256 | c1f2db9d9e140c8f4a012cffb3f1a7d1b82547ebf56d89d7abdd6871b3b750c4 |
Hashes for vl_convert_python-0.8.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f36b32bc4c3c9d190c41925d451866c57feb8fe730dbe3cc6814626bd112b84 |
|
MD5 | 21583d3619a6e18cc2ae81818b9fb307 |
|
BLAKE2b-256 | 8401f21b951509c4176b0e732e1d42d566c7a4f087e04d01d40620fd78dfaef1 |
Hashes for vl_convert_python-0.8.1-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25535918da66eca04498586bb259c3b7214f56a60b2e71a27ac2b31864560bfb |
|
MD5 | 011cdde8f6bc45d606c9f4f1a9def244 |
|
BLAKE2b-256 | 92c241fc15eb2c75c83aa21bc220a0db93a979693b8e36fec8b288bdb556f7ef |
Hashes for vl_convert_python-0.8.1-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e81387be9211abf0685721bed5f50c428e4cf353a5db60fb0cc4ad671f72ca7 |
|
MD5 | 8a587b23784ddca0a3416998799cae2f |
|
BLAKE2b-256 | 0e16960f974ab88da36e85a3b00f7570d0b8478bd89b49b76be90104fd01489b |
Hashes for vl_convert_python-0.8.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3038a4d7291f2d9a7f88bbb119a37acffd1e20ca754873ef8bc2600f5af02615 |
|
MD5 | 101c295cab57ebe8ac43164db51cd8a1 |
|
BLAKE2b-256 | 397c783845b70080f515f61da3393c8dc16ad994ecf9da450fed7eeb74b5a4e4 |
Hashes for vl_convert_python-0.8.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5915f47aed0eb7f217c7b3cd1fac9b56ded67d27a86f527ccd9f69340c8a70c |
|
MD5 | 3088935f1b32ace75bae06ef20d332d5 |
|
BLAKE2b-256 | 4ad0eb04121b3b17aadd0df738f180fb5ec9131d26bb1006e9ef27e77c472fd7 |
Hashes for vl_convert_python-0.8.1-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a89838e68983fb65a9e3658665eec438def5d4cf4426117b30d1aada423a1b83 |
|
MD5 | 9f6aaa91dcdf1d4ec72c43f569b15d55 |
|
BLAKE2b-256 | 20f6f0d41e3769b1205fe8c505c19e3c706c3075b0b991a88cf02699bd032db0 |
Hashes for vl_convert_python-0.8.1-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 369eeae4d52b0e567baca3c05b118e55e43bb795d74864d94d9549fd0d6400a6 |
|
MD5 | aad8d59ab24e9dcc92b2712df214928b |
|
BLAKE2b-256 | 0eb44587be5f7b4bb7e1f0aeb9299cf29f9aa369f1800e5df5e0480c649ba673 |