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.1-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9a6e980fe3f105fc53465417af285590ba697c54003473a22c0b4093ea83715 |
|
MD5 | 11a3eeecfc7bf374baa80a46253fd1c4 |
|
BLAKE2b-256 | 9677b747b5e28a10c19320bd22abea32ff124452e5e2e554cb644774f81ccb41 |
Hashes for vl_convert_python-0.11.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0ec627037a3fdd73a8720b755ae469e94f017af154314a8702e8f51ebaa9cf6 |
|
MD5 | b1eb56044d67f2c4ca66859d4bd27179 |
|
BLAKE2b-256 | 55d1cc075949d050245273a63d609c1a180d3c6bc4548c7a43aa03c8fa981024 |
Hashes for vl_convert_python-0.11.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04860fa552c55a3d8c30aec154287bf3c5022bba460e83087e23f8a7484fd32a |
|
MD5 | bd5a753f05c651011bee4c3e077c1306 |
|
BLAKE2b-256 | cb40b33e0ae107ff2ceac93217bca7d45382540fd1b894a695cb5aaa6db0ab66 |
Hashes for vl_convert_python-0.11.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5520e934dcdd77e2e8385005f8be112ff2277394ddda2fa5fffae29b22558a1 |
|
MD5 | 047456554317f005494f76c5fbc0fb56 |
|
BLAKE2b-256 | 46fb28dcfb635241df1319955f414650adb3eb48ca2dbbe485e17a6044023cc3 |
Hashes for vl_convert_python-0.11.1-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 693baa28fc0c2306efdd9f357d46b86d1e87301fd34742a59e9fc9ca1ddce530 |
|
MD5 | 9915bbefe1815c7eb68acce6ce11d38e |
|
BLAKE2b-256 | 73b45ac7ca1fb792938d4dd4d880c11900e08165b04301b5a17c139104dac93b |
Hashes for vl_convert_python-0.11.1-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4856a0b8a15963666ebc0be4515615ff5e7d5515e45a40b4d810a2f388979ed |
|
MD5 | a1394b3caf61bbd58e8396f97882d8e7 |
|
BLAKE2b-256 | b37e9edbee3d9790f940c7addc8c8cbf382b24c67906e2a2626adb0ee6893007 |
Hashes for vl_convert_python-0.11.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3fc041a6a2ed67e319e8dd66a05fc8822e163dcf4b9afca761c97e10260a4df2 |
|
MD5 | 87badccfbc90d4ad91035c4d6cd69834 |
|
BLAKE2b-256 | d7aae670a966c33b2bf25169f58ec31982fbcd2005e40df79ca330a90caacb93 |
Hashes for vl_convert_python-0.11.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c458ce6b8a26b1b7812f503f6310341070834ac1c4a610db69c192010fcc01d1 |
|
MD5 | b4039f1dddb5bd1d6113b8256a2e1424 |
|
BLAKE2b-256 | 5a8c0d9ebfa7f9b86df0bff76705731678967d09bf8e2ece544da3d65cbacc2a |
Hashes for vl_convert_python-0.11.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f758f8d743dcc50b016beb0edb496cf85a2e214e42d19752ba7210d6cfd81c5f |
|
MD5 | 33b4a9d792b6ea5002d6e1b4ffa6a32b |
|
BLAKE2b-256 | 72c7e878f0cfba03a84a9a73f1e5c71e7a0634369936db002499fd3459a51cff |
Hashes for vl_convert_python-0.11.1-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 509b5355c88fbe79a9a2458e2ec05edf8bd330ab83a53584bd570c23ba475b24 |
|
MD5 | 1def5e10bd3dd359dcbbc2eb872f87f8 |
|
BLAKE2b-256 | 20096a67d8a9b41403ff889804f2387e6a4729501f462b41b9e34d24b82cb1f0 |
Hashes for vl_convert_python-0.11.1-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d0597eba5d78bad94a86ab5c8f9454f83c237c7a8439b09501631922d70e2e5 |
|
MD5 | 5b52cef96464f8dfbf1e3be0b6e596b2 |
|
BLAKE2b-256 | b13fe061763e94aadbceb1b440f85f941d542e1d8064188cab99441393acc169 |
Hashes for vl_convert_python-0.11.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc6348eea2a6b410f1d06d73d398c71f08302fd6d2b9bb3456adceb51a87315a |
|
MD5 | 8418264a93c026a6557e9bfaa8652aa1 |
|
BLAKE2b-256 | 2e62562afdaf6e082e58cf97723bbb2f55ba0a8802ddb650a193ef2a64f2bbe0 |
Hashes for vl_convert_python-0.11.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db5f977ca4b8d2969faa97038a8f5e5c1818d5bdaf736310ed2ad800177b9229 |
|
MD5 | 530a6cf68f2c871204b63e90ce4711bd |
|
BLAKE2b-256 | 0400aa4bf691350c40f816584798a69d84efa35fbf4b758ed1bdb1c4913ff0f6 |
Hashes for vl_convert_python-0.11.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61a14c7d3fe1c44921e9fa53848a21e2091c5292b484b936fb182b0977f3f472 |
|
MD5 | edb33d693a242a77ca60c8731eed5f60 |
|
BLAKE2b-256 | 9dac53677ca07acc29d7da9d37f8b7babdca6d93a0ea7c57f230c4eee09c2c57 |
Hashes for vl_convert_python-0.11.1-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca4ccc00edb7c265d8f1752bf8edb8da8ada5e81b4df94a700cb135b1dd690fd |
|
MD5 | 6c59a62a7d4002f20a0d33eec4c1be92 |
|
BLAKE2b-256 | e102d8e51c34333184490989303b7a35e0cf30fc6b7848c52dba4e805d398447 |
Hashes for vl_convert_python-0.11.1-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1655250fcfca76f1859431a43ec6724e3243ec5cc92aa7f9ab50a86b237b425a |
|
MD5 | f4cdad56da3374ac8c8ab782c460695b |
|
BLAKE2b-256 | 1688f5c0b681e4058f2fd711af1528d3e54dab0ef47d2ce55288caafe7838d8e |
Hashes for vl_convert_python-0.11.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8dd4d28153db87eb47d7c9ddd127517f22f950a3a13bc52e544392659944bca0 |
|
MD5 | cc236de5d4925a5e1d785ca1c510ea67 |
|
BLAKE2b-256 | 4251f85342a5ec8fa64a01f80039d7905f7d96f6916bd567598e46aa1ef1c833 |
Hashes for vl_convert_python-0.11.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1bb995f2cee8a39a18d2198740d36261dbbeaed5ab46e7e969dd0368b26a8be |
|
MD5 | 23695f5c0861360a64e91f7162b2350a |
|
BLAKE2b-256 | 2878cfe20acd04881ece91c7b9b78af94f60be03edda2e278ca35448aa9a503c |
Hashes for vl_convert_python-0.11.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb6149f03a4bed6246a38216099a6d39e7a1e488861e43fa5f538c7f65f4e44a |
|
MD5 | 82a4cc60f5f9fd1e3b5a534df30e010c |
|
BLAKE2b-256 | 97242968995863613410e69df61dffcac8de5ac374184f76296a57ca092e0003 |
Hashes for vl_convert_python-0.11.1-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6166a46fac0214a5361b59edb4e4282c7d525bb2a1449aaa6fcd5e35c5038e9c |
|
MD5 | 398e287872417dc5312575fe32179378 |
|
BLAKE2b-256 | f9e13bbf5c300a21e4665b3308db1157b3a8479fdacabee7b37c5b75b929b45d |
Hashes for vl_convert_python-0.11.1-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f3feeceb22b96616a442e3c28468dd567479f8aba39c1e6f48c6737d60059aa |
|
MD5 | 69bf92bb6cb37a7e2659161c2a9e9df3 |
|
BLAKE2b-256 | d001ce669e231e08ca55f896837ecb98d2a42e652bec703acd0ddb70759d76ca |
Hashes for vl_convert_python-0.11.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31b10d56d81dff5c852c926f0dbf2d4c7d712b3d21600d04137a633f736f3963 |
|
MD5 | 0efae1d87cf2795ca869a8ecc2a868b9 |
|
BLAKE2b-256 | e8ab1f4a60394eff5a5446c11ac28961935446a79fac1e66da85f54249abac6a |
Hashes for vl_convert_python-0.11.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6fb43b9c744ab7ad81656539df795362eb18ea4a6fe216f4d63f9a320b05f26 |
|
MD5 | cdd32d11a1db0f38d45b1daa19c3fe77 |
|
BLAKE2b-256 | fc09437c0739dacc8ff0c377aaad5593295af28e81000313315bc065e06d5266 |
Hashes for vl_convert_python-0.11.1-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12810638a7e1381de3e67683cc8370b9b80ed6b3d63e421ac18ded68de50523b |
|
MD5 | 1a3d33b4e75da44a760a43841b1407af |
|
BLAKE2b-256 | efb445a31e856847d4ef792d5de86b587ee382bc882b533fc9fb7edc9fe930af |
Hashes for vl_convert_python-0.11.1-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e7edc8ff385d18b8fbb881c6a132223bdbe2c095b8918b1c468d8c704fbb194 |
|
MD5 | c54cc0fac528e1178abe3a887fd8158d |
|
BLAKE2b-256 | e84ff9e9a8ed390591d08670298147ae3c8b6fb34b5a247ce8a93c6e61ac2b66 |