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 scikit-image
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.11.0-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0135b84340f6c6c56dfe2bdfeb754cd9360454e1a8341a525f86bc4ce85096e4 |
|
MD5 | d0b94d2d0272f3bc77cdc259315211e5 |
|
BLAKE2b-256 | fa0c74702183b2d6365e9c90afe2dbac9093327762062a451474df59099305b5 |
Hashes for vl_convert_python-0.11.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c2cfb8d45e36124fae388c44c5e1ab09d1edd5068f31be656f554679e177854 |
|
MD5 | 63876d52335aea7d134bd9b4a4579f14 |
|
BLAKE2b-256 | 6e10730f65487af6ec8eec50ddd6b4eff73a62462ffa5c8f6c4b6bc03cc16aa3 |
Hashes for vl_convert_python-0.11.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6165e381ec5523f23f7a41443c97a246471c653b8991ae301bc2e894671b1aa4 |
|
MD5 | 1305fdb5ea1d5701c5b44734f8ee4fa9 |
|
BLAKE2b-256 | c55b1dfb10649c8fcacbc8081c7b0c080cd1321747bae7d4685505f18adc8acf |
Hashes for vl_convert_python-0.11.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc183973c52c6960f226021305147837522afdeffd5c5c78eef22f0d7ba70da3 |
|
MD5 | 2a737d5874b8a37581056091775c4b5c |
|
BLAKE2b-256 | 8962027a02e41bd9e902c7f73750f2d67de484eecc882be441e5b1b103f1d24a |
Hashes for vl_convert_python-0.11.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d9aff6d4e561ddf6d455ceea5f56bc8c82141412e732d58e6ba6d12bafe6dbe |
|
MD5 | e7042d0d054917413099ecf501bf118d |
|
BLAKE2b-256 | bc3bbe090c1a99a219ee8097706d905fa96eefd78039c471e632f81bc488320b |
Hashes for vl_convert_python-0.11.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8fc675738b9cb016d72e6fd3719ca17db4ae65cf35a6ab857f031c99f66688c5 |
|
MD5 | ab285f05255770fe4b2cccff7729ac56 |
|
BLAKE2b-256 | 2632d66387986debe6a38dbe07bfd7c2ef108a057377ba78dfe1e29ef6a1058e |
Hashes for vl_convert_python-0.11.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d4ab6b248b95a24f12aa6e74e16ef9c802e3fc578ef40763c6e2ad979e9d281 |
|
MD5 | 973836b78a64068ec6a1cdc9a1c2f317 |
|
BLAKE2b-256 | 580d4e354868b72ef2491aba3f4d70071f4324d1f7feb6bdcb887d0919709b65 |
Hashes for vl_convert_python-0.11.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e57b3db34db9092895db8ffac247760969ccca3f0bfa8f995fc2a6a233c3090 |
|
MD5 | 6d4b4615540e011930abf57828a9ea75 |
|
BLAKE2b-256 | 38a554008c2ef18863a66689ad57df90148284d3b5fa51ab7d9fc74adc40bbbc |
Hashes for vl_convert_python-0.11.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fd28b0c307e6d7f413d8b7c7173351486a166472be00bdf38a5438704479d18 |
|
MD5 | db9b47385e0e54036e7dda9be457d6f9 |
|
BLAKE2b-256 | b11183e332d58cd93d7e22708928d748275e32ab794da57c33e5bac3e820fc85 |
Hashes for vl_convert_python-0.11.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d2e325b4f2b45876787d008d5a5f165bc7a7a74a381b59717da7437d79ad727 |
|
MD5 | d10894bb1f8d438c768a445fe5cbf6b9 |
|
BLAKE2b-256 | 04b3567d8a594e00f22514bc6e2bbae7bc43b1ad620463687f1a9b26827fbc3e |
Hashes for vl_convert_python-0.11.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7b78fe157dfffde81569e15077cfc93898b6ecd8f1f6713d132bce71e0578a7 |
|
MD5 | f497879050ffe59bf528aa08ab70f084 |
|
BLAKE2b-256 | a112f4c958cf2b73b89576db4e000ee7acf4d8919311a289dffbb3d784a4b89c |
Hashes for vl_convert_python-0.11.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0210ae8cad50462c71b1814fbaa0dc0389e247bd64467fdb76252407d51776c7 |
|
MD5 | a0f673d8b8c55525f5021441815ad60f |
|
BLAKE2b-256 | daedf6f378d32a2e8cf60cfeee4e12b44ae262361abdac7a98c7006daad1f7eb |
Hashes for vl_convert_python-0.11.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0581f2538cfae270fd46c0c47742a4b78eef85c9377ffd946c14645d7bce00ea |
|
MD5 | 23ddb72a6a003c6cb1127646533304b2 |
|
BLAKE2b-256 | 1e286232c4d7bce6a279c721c4ab6c40970e5e248dc1358ec501263dcbf135da |
Hashes for vl_convert_python-0.11.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df282c8f73e6ae0d33803a572430546376197fde979acb9fae7a4d2b70f3cbaf |
|
MD5 | e092c0fcf6d86b5effd3a164a0619dd5 |
|
BLAKE2b-256 | 66bdfbf134cd77dc61e5a2ee9850bd1ddbd8515487823eb7cadd12fb9d109810 |
Hashes for vl_convert_python-0.11.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ecfb0ebb5a61d9e05f20bc8c72c9798ff66a24a14d008885c6453113521e64a |
|
MD5 | 6068cc09e8cfb9857551ed787302d7b8 |
|
BLAKE2b-256 | 266bc10edacd7c1e4b473d4eb64520a90371ecc5ca0dca369c7d1f677bff5ba3 |
Hashes for vl_convert_python-0.11.0-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e81f154b40f7a5f38d74d7157fc2217cec3f8a860dccdc7e1dfb6eb9381decc |
|
MD5 | a48fb1ee1465e66b76337b1b3aebc53f |
|
BLAKE2b-256 | 9d2d2c959fa63a85d1608a6a8039bc8dfa382e6ef9076ef889b29b94e96604bd |
Hashes for vl_convert_python-0.11.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72eec1fdcd5deef1e051ba66a45365c96b692d3278fa8ca3f1f7cbeca00663e8 |
|
MD5 | 5c082e1ca523d8bfc2fc01bcf5b53422 |
|
BLAKE2b-256 | 129774633e4c64b764cc9cdcee780e53d43cd48b1744c82ef7013f341a790dcf |
Hashes for vl_convert_python-0.11.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 315be928a6dfdbea8c7bcf23abd1cd2b68730021b9e5beedfd05cbdc95ab5baf |
|
MD5 | 365e9fa37f5229dbde29ae2e64f19658 |
|
BLAKE2b-256 | 2c6014c7e6dd5fb7d580ff74494bc105a199a1608c47f921702782506eb3105c |
Hashes for vl_convert_python-0.11.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d931ec9c39476ab6389d72c8ce5a8aba372dd0f5b04ba88675dad2e8872cb370 |
|
MD5 | ffb8d6562a1d3b57de8d79347f64869f |
|
BLAKE2b-256 | 46141da7ab6ec057a462dbe80986dbb7efad462ffcd0f59d6cc7bad21f0f4d68 |
Hashes for vl_convert_python-0.11.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a577b01c834889aeaa184ba01dc34ccbba28b97e4ed94400b137abdb9eff2276 |
|
MD5 | 61427b472934f251fecafd94b40462c5 |
|
BLAKE2b-256 | 1d8f184266df6b9dbea84411abb29e7b81cf5e970717a7e94cd5bbb028002321 |
Hashes for vl_convert_python-0.11.0-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 158d68e4bb54adb0f49670ef1b8a38836506a52daf83852e6deeb9d712a25caf |
|
MD5 | 9aee7f7903bf7a1ff3f353f928b74b50 |
|
BLAKE2b-256 | 487e3b70e27c9e32d67fed2bf896246e8bac862c1effa69dc5c9b4d1c4dea19a |
Hashes for vl_convert_python-0.11.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c7d2bc4ea1a69821d5c5948b1ca9a5128dabd485f379f9270f9bc81dfdebf58 |
|
MD5 | a71d74d523be57f9a84ec38b9ce08032 |
|
BLAKE2b-256 | 20653366a5be7f18f82090063eb1b822a864d56a6e0575146ad5660142190258 |
Hashes for vl_convert_python-0.11.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4afb43b2364ff0a345f4a0980b3b565e7b45d78d2d56cc904520aa1e14b82e67 |
|
MD5 | ec0bfc19ea59582b7c983b42f0eea328 |
|
BLAKE2b-256 | ef08bfec3b8f8eb24e601a50a5739b3ed2847c1e276f09eedce66713abd3f6c4 |
Hashes for vl_convert_python-0.11.0-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ffc858bf6333a322e0ba92321c297c219e420bfa42383f0256dd9cd7651c1c9b |
|
MD5 | c39e3be020f7e2c59f147273a36d5434 |
|
BLAKE2b-256 | 66b6662ec6486df6c85ecacf7fd47de52d588fcd9e9340b0e861d350b5b44a01 |
Hashes for vl_convert_python-0.11.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a0d7a2e2cc4f7d48bd89b8d56da63885c412d9161361a7804430adaceaa67ec |
|
MD5 | d41bd2d5799689ee4d615c99fcc7d7b6 |
|
BLAKE2b-256 | 91d4702f3a34e2dfdce90c8d03730c5dc83bf6ef4fd226ec85db4fa1f6e2eda7 |