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.10.2-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a90c637f536cc549a2641de9f1ee4e563772a45189982dd91ab1a813753e1f2 |
|
MD5 | 1d3e4d5548da073c1151a7cbac25e45d |
|
BLAKE2b-256 | 4a7879ca6242b192a231d69403137f0c9c7c5e23d228d3066b30a093887fe465 |
Hashes for vl_convert_python-0.10.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f18a41d676aace5e319897b759ea2333f168547605a15fc893e402e5fa95fdc |
|
MD5 | 76b9efa5a780f5c4ec8883ff4960e338 |
|
BLAKE2b-256 | f39f9534e775f5220d830d464722cc40e2694e485b9f5820e7f58fffc35b901b |
Hashes for vl_convert_python-0.10.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dfd8997fe03d302b417213c945086fa0c25501a5f81aa69ecda125a3fc8c4536 |
|
MD5 | df0154653487c04185ca4a8f70bed316 |
|
BLAKE2b-256 | cedc804443de302d0918841803f72b5cbce59cadbff225c4656e3ea29e6f1998 |
Hashes for vl_convert_python-0.10.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c8b1a452446d00b6d85d9c6f616501b9a700c7e6a574b8322842db4f3f0a49f |
|
MD5 | 7657cd986099ae6e55a687c82a8e2d9b |
|
BLAKE2b-256 | 21f17d830d303620e908cd79c34a350e8546d389c2ef7a911c12c40695d8c7e2 |
Hashes for vl_convert_python-0.10.2-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3f791c02d167e5c362fc1bee6d4a3575383977bbca713427fc0c8fbd33de0e4 |
|
MD5 | 291b7682946aecd5f5e45d3f5e974c2e |
|
BLAKE2b-256 | c64f6697ab414f327b5689ffa991ce9c0855b73e1ad4b3450bf38dcc8fce2024 |
Hashes for vl_convert_python-0.10.2-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 917ebd8bd904f318d7a295ebe5caff293f4f40814164396ab9df284592ad2218 |
|
MD5 | 595703cc758b0e2902a2cc0ca8252e40 |
|
BLAKE2b-256 | fa013d3fe198351bc2428df069ac70be46bee9b9b2afaa1abfdc7514c4a2cc3f |
Hashes for vl_convert_python-0.10.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb18832077a475413349e2a642d4e055ef50747de92048a3bf7d1aab48468e5c |
|
MD5 | a8628143b9d7e457f58b0120c4559974 |
|
BLAKE2b-256 | 3d2c0752ef7b157e9ec32b7b9be068b4da07180d0c0af0cd48b4eddeac832a8a |
Hashes for vl_convert_python-0.10.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c437a430eede9f3590e97d2a616ef1bf002921deef84750ea598e7b9a5d9688 |
|
MD5 | ab6382b9b646c056f3d8ced6f49e869e |
|
BLAKE2b-256 | 3f0781c94c5af963f773579b004699acf0c750cdb4302b76affcfd3d8af5861c |
Hashes for vl_convert_python-0.10.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06fa5e295f98f2321f3b23a7e8b00877c42e6b73c6f01044e9576848eb3a1242 |
|
MD5 | 93a3d748086ca533a559af018be02133 |
|
BLAKE2b-256 | cf6dd41fed5fdcb41d54f92dab3bafcf639a9ca07982f0d472caf3a1582ec39b |
Hashes for vl_convert_python-0.10.2-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e21c67cb1f838355776ff9ba4357184e7cb8bb5771a265c60773fe30e58be3e3 |
|
MD5 | 52c53071fc47b9ab41e2c38cb5f2f18c |
|
BLAKE2b-256 | 09fecd813b8a5c1b0aa57604591daa77faa7983419537b79628b9244124d1772 |
Hashes for vl_convert_python-0.10.2-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b656141dee30dd54b5af25ce7f92d649b21e2b9700280ef1e8511b274b81267 |
|
MD5 | 757a0ed91cc449bdcc9bb9c215ae82b9 |
|
BLAKE2b-256 | 4d960d21e2b9b1b78320375ba243839dd95f9a64068051927e623dd907dc8446 |
Hashes for vl_convert_python-0.10.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78db93d1fffc443e48cfc22693f34620b112d01f19feee3ad991debc42f81320 |
|
MD5 | 69ab84786fee859a5b108b9e9f0f50d7 |
|
BLAKE2b-256 | 95d954e146159d3f484f15e3b1946d307373e28c991a72694b4a67061eb447de |
Hashes for vl_convert_python-0.10.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b37bc3f262fba645576518ab083795d7620a612689f107366a3ea7957bdbe9e |
|
MD5 | bd8982e30600bf49b7f75dad32a65354 |
|
BLAKE2b-256 | 7313609d50bb984d5027090d468d1c189f7ea11f0fbdd7e70e5b1de289391108 |
Hashes for vl_convert_python-0.10.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ddd755f336d0a655bd0aaedf1f9b44618fc5100e6fa3874273a2f9400464da7 |
|
MD5 | 3938e7352675bd4b3e76a6af48fcb4bf |
|
BLAKE2b-256 | ae8fa02127883cdb6ad25ba10b21f75e3a589a0b2ae84dc51deee8b5870385ce |
Hashes for vl_convert_python-0.10.2-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5b0dcad9e6257be166a8d56ce5bebb6c18a39d6e4f1909d702e93f8fc4d6fdc |
|
MD5 | 8b69b0114184a843bdd3043fdeae928e |
|
BLAKE2b-256 | f7cca7a23004f5c62af2a1f53b2a492205dca8f4738f35c2ba2b791c07fe3e6b |
Hashes for vl_convert_python-0.10.2-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec4af52309429f34eafd29fea735668c1b4aa2f82d2ff45212e296d099a730ab |
|
MD5 | 174bae18d2126e8b42ee2ae3578536ad |
|
BLAKE2b-256 | b215f4da767c272e12cffdb8b27d01b730bcbad13d316ee594a36d37a8cfd54c |
Hashes for vl_convert_python-0.10.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec6dd1260400779d60acd1ee2a84f56fe4b12280d0739c2188ae246fe55c8828 |
|
MD5 | dbd11c7569d708b3ed9041b134acfa0f |
|
BLAKE2b-256 | 5d3e5bfa91497f290ac0c83d7072990cf1c68aefe2ac88a614d2d77d08445713 |
Hashes for vl_convert_python-0.10.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0180c545dc71687a36f44b63fc3c1f7fb3c43afad8142c47b273f3295967c2a5 |
|
MD5 | b606aebd7aeddd89e47fb7da47dbad46 |
|
BLAKE2b-256 | e1dead8fffc05ff41b225aeb70343584afc1f3d85c7ca0d6819c1d56f540a161 |
Hashes for vl_convert_python-0.10.2-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c77fddd0fd177fa77859e04b4d85bdb90c5099a79ccdc27412ad942de11898f |
|
MD5 | 362e949b66ef5edfdab30c10dac1de31 |
|
BLAKE2b-256 | 236dae05054fc21bcf1f1e4962d1959b9693bbba6ef4ab07a0c0b4e60eed64b4 |
Hashes for vl_convert_python-0.10.2-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20bb15d8e583a12aaa32c89b0d2041afed71b2d1d69493f49e6336b5831e20b3 |
|
MD5 | 1cb16b8ce64f40a701fdcd38059422e2 |
|
BLAKE2b-256 | 6070e8b555b53c5a4d52deaa3e3d56b32b8dab44f122707f53503993f5033945 |
Hashes for vl_convert_python-0.10.2-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f4be8c219711c1445f1981d7679f2b2b2de66bb452bf5e52ee0a771f67bfa98 |
|
MD5 | 267a9844269a20787919829cbaf17ff1 |
|
BLAKE2b-256 | 4c32b234429fd4d0432d45506c840003180cba27b211cb84b558b810461f5024 |
Hashes for vl_convert_python-0.10.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce9360160ed57604330c037bdb4f2b7844519453520750ae588fcf9452b9d61a |
|
MD5 | b9613844b6c6a8bb2507d7e741cff33e |
|
BLAKE2b-256 | 680cd50e0ada85f762098fbf7cbf47756c019571158526a24b30e956cb378de2 |
Hashes for vl_convert_python-0.10.2-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac1f2f3f7b001ab7b9acfd59f1e7ee1bee82f3d4a0ff29313d97b82b13b7dd84 |
|
MD5 | f56501c2a8874cc20d1e1564692ab4a9 |
|
BLAKE2b-256 | 16e3f3871abc6283d1f07f4daa169742955ce12bbc2af12f5cdc57fde6522d72 |
Hashes for vl_convert_python-0.10.2-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9cb84bd76a908794c12e7852f4660f9247bca9329f13b0c4738c6c75ec7a68a2 |
|
MD5 | 2c799c14b8241267288bf2d57816956b |
|
BLAKE2b-256 | 23c4a4e48a012b5c9bd708b7a1f96ecc1ada24ca26db6d399b94439432e27291 |
Hashes for vl_convert_python-0.10.2-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 613e8cc7c82b70b4002ceeae056dc49886757f8d4dda8712377049c044d23f0e |
|
MD5 | c34685431be889a52c9f506689ca9c5a |
|
BLAKE2b-256 | 91b6fd742621030aae0d73f506227a68a28c59836d0f25063e5eb4239edf5625 |