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.9.0-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 202616d7adba3b25a560eea655be1c37a62d00087eb9bb37c22176e1ac17bc85 |
|
MD5 | 58dd64172fc9c45220821b14bfb2a94c |
|
BLAKE2b-256 | 7013edb09804f85b1fe6bfa7d5ed082edad8a04eb995ecc4b0455f8503706e40 |
Hashes for vl_convert_python-0.9.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 291ba4cb39274c9a8dab66dbaff16cdbd2189953677e583549a9739fb636f099 |
|
MD5 | 7784aeccd048f0fd723a46840c4af4e2 |
|
BLAKE2b-256 | 8fef06d5c115cb69252344cef1e996a3581508aeb39ce14219cab6b0a2478d99 |
Hashes for vl_convert_python-0.9.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e214bce6de81ea1760ed1a86d53fe092b8e0dd12241c945da56a07c464c31a1 |
|
MD5 | 3bb258acdd196a90f2f447a5df6b979c |
|
BLAKE2b-256 | cfa16767e7ff7810085f0a7c303d0f889dff1e450a4b5c7d0bd752b83b97f051 |
Hashes for vl_convert_python-0.9.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cca1c76d2c2d95146fb644b9e7a596a8367598666b08fd9ea246c4bd8a935f6 |
|
MD5 | 4ec8f82dd7a0d6038751c6571e63e93f |
|
BLAKE2b-256 | faa4cbb6b165b694f00673f3f506809fe4988b33d96822a230f36adeb6d0dde9 |
Hashes for vl_convert_python-0.9.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eec45449f5da43112f1409130130a1d8a0138716c997267164ccc26389806d44 |
|
MD5 | a933be192bc8f64e301f3546804fd74e |
|
BLAKE2b-256 | 7a9107d373f4e3c333a86973a5df3d7658c7e2fe2603d947c2fc8839f9a1cda2 |
Hashes for vl_convert_python-0.9.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ce3587240a155c9a08948f57893d6b905c619fdbcac02c63b6d24a52ccef3e8 |
|
MD5 | 752736a6543b1f91e6be20660be56cc8 |
|
BLAKE2b-256 | a556ea306f3d290253f6aadf44678ce98cddd2baacd7bfdeb3541d9028e6c4ce |
Hashes for vl_convert_python-0.9.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ca51acbf64eb818b62b4cac9dc06cbefabdc5fd21ba64482841eeb3de902d4d |
|
MD5 | 8b858661e8ea4df80fa8ba50033bbea3 |
|
BLAKE2b-256 | 8939a67c6bbb080891a1ebda8cace911115760bab85fd0cee5e5be718edf3236 |
Hashes for vl_convert_python-0.9.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e7bff4533e9bc261a55c1cf5556b155a4db6a3906ed020d9f8f26a2299d37a0 |
|
MD5 | 8040e00cb22b80134aded1976532fe5d |
|
BLAKE2b-256 | a6ac595e5547b3dce4fc1a61220fb622a376b831a55a8672274f29baef006ba8 |
Hashes for vl_convert_python-0.9.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 738b8cf96a8e1e997f084df4a9545daf8e8cfe82b5b76c30028e3da1c1e1f50d |
|
MD5 | 280ea4029e09c21556bb93c6ef5cc836 |
|
BLAKE2b-256 | 08478ea35139b66285816208a41a0e224152570215f89947a09e9044cba27b62 |
Hashes for vl_convert_python-0.9.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ceadefa19d9ec46cc13ee8f798ecd3d506031bbc927b80b5b9405ec156824a1 |
|
MD5 | 5f692a062af3bec48a639a56b525a9d8 |
|
BLAKE2b-256 | 0dd24bfb119e196f66db3b2361c327278500d4dadaea3ac9c167521959c0acb6 |
Hashes for vl_convert_python-0.9.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 791b943462b08a4b6825e72a10e6f29e0dbdb657ecb65130b0bff38c984ded59 |
|
MD5 | d81da94fb6ce12f46c06cdb16a82c8bd |
|
BLAKE2b-256 | 30cec574060c7cd45c64ea46850e7250f38cccf4e7a9115799d47d9508b46dd6 |
Hashes for vl_convert_python-0.9.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be040db8b72095fea0d75b6896ed4e6d5e4e3a387030b03342924b2fc5e39cac |
|
MD5 | 7273709e86b2031466ba78fb36ad8bf6 |
|
BLAKE2b-256 | e59fa1ec619c05c101e76901f995437d993a0dc44d3763c026dcbfad51dc90fd |
Hashes for vl_convert_python-0.9.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a5052bea745c63e70ac9b3014cc78f9e1d5025a7c1372edd07b3d018a5dc71e6 |
|
MD5 | 4cf05271d3e562dbc40f3231ea62c326 |
|
BLAKE2b-256 | 1f5465d7c7fd8b7b6d06ebc9ab0f8d8ffc7d587b8b773b3adca66cb387358a74 |
Hashes for vl_convert_python-0.9.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8984c3b24319b28f03138c096215fd27a13cc75806d010c7443a3fa1e3678ff3 |
|
MD5 | 11b23b97e6006bf68a89d4958141e1c7 |
|
BLAKE2b-256 | ed2d5978f61e3b88ea835a5b96c8f43f9efcd13c49b368c5538660e21873153a |
Hashes for vl_convert_python-0.9.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e40df5cfab374853a3915c5e5244b80308471eb380ebcf82ea1b6072816d822b |
|
MD5 | 389e3ab2d9bb1bb996bd341f7be0cf99 |
|
BLAKE2b-256 | 8df1fdef773d324bff09f7dcf16ea20f45a541936168ce51f023ac9baccbda3a |
Hashes for vl_convert_python-0.9.0-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ce2a871cb634f39450bbc4e97d0ea2b36764ce24540452a23f27013127eabce |
|
MD5 | 7a78ec02eae891da19e5097cc2458ff4 |
|
BLAKE2b-256 | 73631c9b4dcb49ab4efef5f1edaaabe504f5bc550c46552da7f85955833ce019 |
Hashes for vl_convert_python-0.9.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d283f4f01c3603d94ae1409326c475e7490f80574763032b3ca14d14b8d793d |
|
MD5 | 7aa8dba92accb332a15cd92875d7ef26 |
|
BLAKE2b-256 | da7e2538bc23d8db69b53eb3bb2f90e5e42de25aadfba0bfabd77d0e4bc5d15b |
Hashes for vl_convert_python-0.9.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27fa9bf7b3454d68f3f867c99c43c59b821a0d596b4698fa676bb063b1c952e9 |
|
MD5 | 2607932a1134c914fff888e7528b26f8 |
|
BLAKE2b-256 | 3b31537075441e730445f092f149be60320f9db09098623059395785cf176998 |
Hashes for vl_convert_python-0.9.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 105f0a33dc5c5ef5ad9e9a07be79efa2b999c56af2a0ecdacb823eec84634686 |
|
MD5 | c6175485b94558b0f729ab32f27efef8 |
|
BLAKE2b-256 | 31e7eff623aa2bb230d355b8b3ced3230e927ce125bab4eacf484929a39368e0 |
Hashes for vl_convert_python-0.9.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee36c1be868effc6c9c6579ca46191a3607556f9857072a8b68867799cf62245 |
|
MD5 | 10e17c5622855832fe33a53b16d85d7b |
|
BLAKE2b-256 | b0a7d524c4afb7a2494632e72f8868c94b75357363fcae6122124e083486738e |
Hashes for vl_convert_python-0.9.0-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0e342c7f64bc0ca0b907518bb4d578d6722e0c7abcec57008377512570b178e2 |
|
MD5 | 87e2a859665e8514d647c120b795d726 |
|
BLAKE2b-256 | 0ad4788d5cd4d054778a05b834bc378ca018f948e5e119a39fe088fcab37481a |
Hashes for vl_convert_python-0.9.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd07fdf8030fe438eb310947945f1db0ffe94492786e83393b685db90cddef8b |
|
MD5 | 9af54fa940ebcf0de33dec080f1055ed |
|
BLAKE2b-256 | e98b907771e7ae006a79524433c3a923d8afd27d340532cc91195fea647b0fab |
Hashes for vl_convert_python-0.9.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 817ffee96c78e891fb2bab5494b6647eef41526fce266e64725f20cf16c285c9 |
|
MD5 | 2ac44927bd52a2bac71481af8f5ea5b0 |
|
BLAKE2b-256 | d0cfcbe14c48f8893b48a37957da1979c713b522e9410fcdcbfa6645e8a57449 |
Hashes for vl_convert_python-0.9.0-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d72f6bf035842226b8a4c72966d6a85183c6ad8a0b7c812ae08b8fb59b27b8e |
|
MD5 | 125041bea281cceb2d9e12c7ac0c5d9e |
|
BLAKE2b-256 | 035dd1e73bbc5f47150c38b8bc486a2aa6b90724c7cdc741dff5d9f4a0fb3af7 |
Hashes for vl_convert_python-0.9.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 578525e2f86796a46f84a160655e3ebb092f0b770802986cd94eae8f1da91a6b |
|
MD5 | 2d79b5498da364a68e90b1a30d69cd70 |
|
BLAKE2b-256 | 9345964713b75928d9b1e1774859747d2b1064d52821b9e823d09384a365ca29 |