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
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:
f.write(vg_spec)
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
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:
f.write(vg_spec)
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.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ced6ffa0c174f132981020be3d437b24a3c5b023ae7a02116d7b5c3da9d34ae1 |
|
MD5 | 734e9b1afd886ea62df47502f8cb45e0 |
|
BLAKE2b-256 | c592402015aaf343d72ec9f9ad427d2484d057a75262681c608df1b19bb260b1 |
Hashes for vl_convert_python-0.3.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bd9bab73199dc3a69f2410cb24acd9c78c1bb17ed04ba0ae3d582d4ec02f66d |
|
MD5 | 007b75fa8e968ea178489c1442a9c59a |
|
BLAKE2b-256 | 651c2b9cf7d6bcaecfb89dc8ec1fdb8316280ae0a5869f2d9976b3836aeba805 |
Hashes for vl_convert_python-0.3.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87ed29dcee18a30bf49c3f038504e45800023ff9a3e4dd7ae7d1707528d33a7e |
|
MD5 | e2134f450001f57797e00740be4e8abe |
|
BLAKE2b-256 | 8327e93c71419925ae45c544093142336fd5ab5ae535cf2a888eeece30d06c66 |
Hashes for vl_convert_python-0.3.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6fb68a6acba1b30ffdccf6f9708ee09a9b2af178ab700e7d637247f8378f9e36 |
|
MD5 | 52a9aa1e5f6f99bb5a6ed91b0e2fe91a |
|
BLAKE2b-256 | 7ee4615a92320dcf3f5ede7ff9f0d9839ad253455ca423faffd87698a3a66aa6 |
Hashes for vl_convert_python-0.3.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 796a9407cd4f0f06841e0017d6cb9a0197b390ac749279937c393e11225b89e1 |
|
MD5 | fa04a9e5f2094d3236b5d8ac4b8e6c2b |
|
BLAKE2b-256 | 8960f56daa71ccd2bb1b3d083051dc7b43c75c901eb0681836f00fee00e19eac |
Hashes for vl_convert_python-0.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6a1d37389f3c1ba3446d82b1d080062d185fa5e698bdcff7a81acfab77b5532 |
|
MD5 | 61d73a0ba048bdca733f397bab703312 |
|
BLAKE2b-256 | dd9009d821c981f5fb85a2d828bc815c81adcd6e2cb36545cb3c5c6e26f4dc46 |
Hashes for vl_convert_python-0.3.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22843e02a924fa6ebb669ebf335ad2071a5e0ccc03b26303eeee580908aab93a |
|
MD5 | c06236ed7cf11262b330f684df5e1b6c |
|
BLAKE2b-256 | 2d6641ebeb73cca05a2f837ac5dbb4d7fbd1573370c010500786e3e52c1c969c |
Hashes for vl_convert_python-0.3.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 822734c1db8f64568f104c117cc05dd0401565eb48aeab2d40e660be1fb8f06d |
|
MD5 | 7947f16611b946eec6bf79632e29e43d |
|
BLAKE2b-256 | f142a54bb3491e649e37b365fa5f94789e35fd781438502519c05ae960a6e7e8 |
Hashes for vl_convert_python-0.3.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c927306ded4b9a34c788f9c2150c5a11bc6e87e637ccf54ffb68a386e786b1e |
|
MD5 | b991b75db05e1a320f07cb2bad1aba5f |
|
BLAKE2b-256 | 774cfbd2a2e345f56b41cbfa753a43b039d212d28711a2fbd889559d0cc6717b |
Hashes for vl_convert_python-0.3.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8609e47131fc53c6cd4655129b0ea0e5eea5a7b4275cc1c1573084b1dc0078a2 |
|
MD5 | 7644b8834ce1ffa48fba54b60a0eee2b |
|
BLAKE2b-256 | 908146609dfae0390cc2878cbc76f54748b5f35558297a56f84854e1a64886e6 |
Hashes for vl_convert_python-0.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 527f327fd6b5da9c8af877e79fe22a78187d5a56a9fa0401882e442de3f6aef5 |
|
MD5 | 3be8b3af18def665dbb1827976fb822d |
|
BLAKE2b-256 | b2c94363aa034a935022aaef7087597bed56d3fa15f25d9c4c4eb4799a5b14a9 |
Hashes for vl_convert_python-0.3.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9cc52f44ea530249249944f774ff260fbe1d2b5d2bf0f6563a85619189f4232 |
|
MD5 | 65bd344e642f33408a64671ed15425c3 |
|
BLAKE2b-256 | e0141cc89743fe06f39ea5364c014e75e291edadd0c5fb8b074a9a42306ccb49 |
Hashes for vl_convert_python-0.3.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f19ef977861270d2d20454a275d4ae1f3c69a14ce9971eab3825aacde2c9ca9 |
|
MD5 | b98468e09e52d81f33a77d98212cb628 |
|
BLAKE2b-256 | ecd30ff24951ab630949d9e99af6be5207f6e55922111fd114ba4129d28209c2 |
Hashes for vl_convert_python-0.3.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c1e4b1bb28323d3972cb20cec4464f8f5fa69421ec89b6f8ffd27fffc403f2b |
|
MD5 | 1a391c95ac2c67587270c3900e536b3b |
|
BLAKE2b-256 | 2e317de0939fa77f73204a3f0692d0fb2a2cb8618aa972d689ac148aef7a8cad |
Hashes for vl_convert_python-0.3.0-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0b780b40b634f347ae8af18d876098b7b26d3ad2ddfc522268b1b6fba1d7ee8 |
|
MD5 | 2e045922fc1fd970b2eac10acbcf9c3a |
|
BLAKE2b-256 | bd2060b94cde681bce0c1c7dad80a9c186d84823d25de14250561bb3ec798d2b |
Hashes for vl_convert_python-0.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b4a6acb472f6a1e35a9a488260cfd8c2c4de7bfca45fea1e04969dc8fba0916 |
|
MD5 | ee0c2373b6b5d4eeb78f0783429f6ff9 |
|
BLAKE2b-256 | a595373e522076fb71206a3c2b3df8cabefaffe5af66a94c1bab9514f234fdf2 |
Hashes for vl_convert_python-0.3.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7219a0e2f3fe13433ca57725b4131efe169d19704d8745586b29cf8f40d8cbab |
|
MD5 | bf2819f4ef60c730630842b28b07f96b |
|
BLAKE2b-256 | cfdb9743b805599c1011edbf23e36e082a29ea2a93186aeb481c2082c360dce2 |
Hashes for vl_convert_python-0.3.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3cb1e6713aad1f50aebf368702a6608b43bd33d85771da36179c94db54c5cdf |
|
MD5 | b496245c7b1a2783dad949d5735271a2 |
|
BLAKE2b-256 | 1d9084a3e41a129e747de1104021d87d06ec84e7eb99b063ec6d71a3c3a317b7 |
Hashes for vl_convert_python-0.3.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f556b836b57300e487b878ace908c11a67e60ca9451ef0c55ab3f48583f5f2e |
|
MD5 | e5ee03d126b7ef776052c22edfd217fa |
|
BLAKE2b-256 | 4b93e22f63569eee00395127c63e46f879a63853cc78e86059afdb858c3db2d8 |
Hashes for vl_convert_python-0.3.0-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba4064e37cee42b2dc15ec55b95b17cb1b0dde562b0d4030f85d82e9a1cfd945 |
|
MD5 | 80263067fb2e67655ef9fa0a72052f46 |
|
BLAKE2b-256 | dc613b8a1f78a60ff6bc5eef9bb9ada13743e339d9340dd9190234c174db928e |
Hashes for vl_convert_python-0.3.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0153724479be0da2aab14c9b318c129ec80c1bcb54837a0ce92a6eb276135415 |
|
MD5 | c38ea8971408777d400b6f1447d1b306 |
|
BLAKE2b-256 | 85c33dd961f8ce9403d6563a31ed00b3be967eb69f69adde5632ce2dd74849e0 |
Hashes for vl_convert_python-0.3.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a013c814f26613773ac15d64370688d74a44db0ab3675c8c54f4a75bcf13c05 |
|
MD5 | db20fd7e3b4d8785a25907fdc87c3f7e |
|
BLAKE2b-256 | 89ef46c27d8088d9cbcdf873f06e0a4bcba523ab93e33a89f762ca17238c8f4c |
Hashes for vl_convert_python-0.3.0-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55fbe4ff3125ef076f28e142973d410e1fdd1b1a1c941fb8c579bee710e6e886 |
|
MD5 | ebdeb7600534eac7cd25f54724a8345e |
|
BLAKE2b-256 | d96946dafc276291041f911bb52c642acdccebe9e281232dd0c5f5c4d8b513c6 |
Hashes for vl_convert_python-0.3.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 533b2182e76c9ddd907ce0c6c4408904504cfaf1994390e624d24e6475d6bb53 |
|
MD5 | 01e41cd14158f1ac50684a1b74b7e53f |
|
BLAKE2b-256 | a09bf036942cc6dd525a31d1e84e124c9cc2f6e9992879db0f588fd127d3f1cb |