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.6.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7318990fd3201901290994bd82215be5feb62decbf4950f602a9ae1b4de16e93 |
|
MD5 | dc05464b049ab210dfbc717082c6e95e |
|
BLAKE2b-256 | 759af735b165247df505ad979ea4fc3c5a490823940975dfe0fe298aec79938f |
Hashes for vl_convert_python-0.6.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b357872ec3e8a2b6a2185fe1cdb73830f139ca80ee3e90e5395f9ba03c58467c |
|
MD5 | 349d17be4f2adc3f31c644358e110fa4 |
|
BLAKE2b-256 | 5b36f65f8418d7f541e3f2be71f6aaf798cf7822f488477824a8340815eea06c |
Hashes for vl_convert_python-0.6.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 858f79525552cbea160f833eccd70e10a8424e60257573bd471835c5f94ef89b |
|
MD5 | 177c2b538c28069bf12fbedae5fe36b0 |
|
BLAKE2b-256 | 0e9491bbdad4041d047ab39ba131ebd06acbef10b32c32e9a748e5da4183f6c1 |
Hashes for vl_convert_python-0.6.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60bd7283158f60563cbe10901a2a815e9278ddad6b3d943a48f45facdf67778c |
|
MD5 | 446d923ed381ecbc58bfe7a42350c12a |
|
BLAKE2b-256 | 89473534029c0bf0f71850bb11c576239fee8b745d57b92076644a4f99266cea |
Hashes for vl_convert_python-0.6.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d889edae3fd326ba54703170cddd183cdb38bd85309054ef35a662cbd7c8caa4 |
|
MD5 | b5afbe5c88811588f4bae805f61b5453 |
|
BLAKE2b-256 | 8696a4dbfc3ceeb5a7437a11d2b72c24dbe0389f8e0b10af718dc24636b9f795 |
Hashes for vl_convert_python-0.6.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbca24b88d6675ed3999108bee90785dbd2f12dd5c5b8693f82c1f9250e8dab1 |
|
MD5 | ee48ebed8ca9613e24efb20d16926efb |
|
BLAKE2b-256 | 22fbc8eb4203b48c196ce0de5cdb33415e4ebf01e9a22cc0c33d26f27ea8d5fc |
Hashes for vl_convert_python-0.6.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 136c90ac56c9ef70cd643e06d4862e38ae4a925047c9ecf4dac3a34ca1dcde78 |
|
MD5 | 3d7a17d6ef9d5d579ea31d664826d821 |
|
BLAKE2b-256 | 0cf8f56c4add2d3530b54df146bc624b88de8a700324a5857ae40124d04d52bf |
Hashes for vl_convert_python-0.6.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 315bf2c8105afdcabae57b70fdd94a12ab07451d1db9d6b1a7228b088a7585ae |
|
MD5 | 8a15bef303cad34f1e0abfd0de0f0a7b |
|
BLAKE2b-256 | 2eb1d6f0321b04cc5549e08e9ed9c35b0a2ebaf0382af2b8732895cd05228913 |
Hashes for vl_convert_python-0.6.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a174daa001631c803cba6285243e8afb98045d92d029d90ed9564b6f05b7e292 |
|
MD5 | 9c276a96310c2b4360a0fdedf6b47e3a |
|
BLAKE2b-256 | 8a8b30ab64e6b641e9587fa0fa7e39d67a35ecba4f74c9618e897e2e650f8f7e |
Hashes for vl_convert_python-0.6.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a2f18779997c632d27b10b54adbae924e9eb4313e32c1716d1d9d341ac99e4a |
|
MD5 | 19115d011b1f108acf551994a97ed860 |
|
BLAKE2b-256 | 972f9ca31963cc5e040d722725ee524d91c0c70fb190dbc4f4282ed3bd02ad04 |
Hashes for vl_convert_python-0.6.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed6dfa91c69828f62f2b6a6c9c585963f4d73fdb13a85e06bc2dafcd73fc4f5d |
|
MD5 | 1c7a8056bb0b22f5c3b27b44cfde6f66 |
|
BLAKE2b-256 | 348ae987d5dc3cabd303acb07c07391eee9f70fa4ec4577de4c325e54e72acb6 |
Hashes for vl_convert_python-0.6.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2280ec7975f25a8e7e02a9182ccee17f1d5412e8688ca1a5e0037d5e7ef8bcf5 |
|
MD5 | fa2657295e088a29fe59190aef32fb5c |
|
BLAKE2b-256 | 6bdcd302e16b8037d20edeada2aec9bbaa6170d692d457519b2b935a693bd06b |
Hashes for vl_convert_python-0.6.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b46c8a0880c5e9c746f9c0ab6d1a63cc0e380d9c738a592e2260669d39fad26 |
|
MD5 | 1fee32c3f1ffb1eb0ec65ebd9ab1d28f |
|
BLAKE2b-256 | 0b7f733e76a7c68b7b324dbafd012006deaca9cc614b0dac12a3ca07117043a6 |
Hashes for vl_convert_python-0.6.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4da9f91556a4869644685a341051aeee923e038f4997bc63cae51556f8b03b16 |
|
MD5 | eac40cf452ff09fbbf6ea3279f071f4d |
|
BLAKE2b-256 | 14f761149667f5061aa5bd25123a7ffb18386ca7a0d248bcdc007d84501f3707 |
Hashes for vl_convert_python-0.6.0-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f419d66e10b9a510ef0cba6fcb08646ff79e3712641994816d0f501f6fcc7a28 |
|
MD5 | cb0672663f190168af618aea8149eba7 |
|
BLAKE2b-256 | 6d983b65429086b40924bf845a2078ccf49c0205953440c3c9ef8bff8874bdc9 |
Hashes for vl_convert_python-0.6.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 791cf3bd141516085ff870f15a30263283f9c39ec62c9e6c3a998ca4ff17c1c3 |
|
MD5 | f9620393ab376e6a8f1e75f412b33905 |
|
BLAKE2b-256 | d2fb4040349d09137c99a34bd26cfd59caf6406cb89e5dba74c739ed7d913165 |
Hashes for vl_convert_python-0.6.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 358ae5a18fad63d64eb47e3572981d2f0423857931aad94deb0c9b94556d82b8 |
|
MD5 | cb9d6d895e73dd257542962d2b80ebe6 |
|
BLAKE2b-256 | 24d0d0b70475dedbf98245efdb4b622b9af75c9198225b2265f3b83a770a75da |
Hashes for vl_convert_python-0.6.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f2fd4f4cecc37b838309e14080c4294840a4a0b4eb53a6817e758cb74ec8b08 |
|
MD5 | e4b298212c7ad2e8e41485bce176771b |
|
BLAKE2b-256 | 272560fa8445cc74cda93e82d32d09772f50df3cd629c3a15d31fe6c5947c042 |
Hashes for vl_convert_python-0.6.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71e869f226199895940dfc67db7f6d7c916d1553f7fe00a73f101463a31d3954 |
|
MD5 | 0122112e5ed4ab1c87482e532a6f4ceb |
|
BLAKE2b-256 | f21f2f9f7d068ba2a4f2ebd9c05cdf99d18da42af23beef3159921a4119c0df5 |
Hashes for vl_convert_python-0.6.0-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15ed6ae8383ca0156c615997eb94b712582cdd3d083ad370c08d16b9b4b602fe |
|
MD5 | 82f6bdc7a971f21bbe7f0b0dcf02f69b |
|
BLAKE2b-256 | d48bbe55ab54853954e2e0a4e3d3946b6ec63a182353a1fbcc20dddf1f952006 |
Hashes for vl_convert_python-0.6.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 337e1312e513094f6b8a2f040e719cd3ec1fcc7d067aabbcfd1040b50b48fbba |
|
MD5 | 4a6be539b3c0555bb1ff22ee78a68171 |
|
BLAKE2b-256 | 6a7d92f8166905397b96a122b5cb8f2efce1afb59059c95c09140e2299ea3495 |
Hashes for vl_convert_python-0.6.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8430cb2f268582743d026743797aeba9bea93be15a9405589b38678d8c4b81b2 |
|
MD5 | a03bf5deb5f9cc65b96f4df9a2e9d6c9 |
|
BLAKE2b-256 | 61b9fab31b2c6d1c16376e9477ab9e7bd9ad8fb1a9c5efb314b570cf18b8e9ff |
Hashes for vl_convert_python-0.6.0-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40c8a8d2c34840370f79c12f2b5a957b190f09347c7740664cbd7a1cfe224869 |
|
MD5 | 3531df50f3f6a21384230d72b1022914 |
|
BLAKE2b-256 | dec9413f888698e2f5543818bb3a7efd4f4d0b09bae3683aa9f15b13dd0f0967 |
Hashes for vl_convert_python-0.6.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ea126784e2c35465aa7aecc6869dd9ecf1afb2276e8a3cf994b8a395caa66e0 |
|
MD5 | 3f0fe0ebab79197e29e83c98d591ceb3 |
|
BLAKE2b-256 | 34e3b01c127625513b4aa61f6676ac4263d2adcb9234f7442b815650c49cf47b |