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.0-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd3f79409cbf46e0616a9a8ed6b918461fa9f8f8592832694aa7268573c56881 |
|
MD5 | 2a4285df446684fd258d47b9cfd19430 |
|
BLAKE2b-256 | 71dd57379378cec4f3645f070e249896ce4230426183ba71fd5c39d31fc1e099 |
Hashes for vl_convert_python-0.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc2b22573e5404ac248e22f88970e631fc058019cdfe436b9cd3ba6c4cbcf96b |
|
MD5 | a233def303ae0fe516c8c36df6ba0b83 |
|
BLAKE2b-256 | 4ae00210fae0a8cdeed922ebedb610681b0b6e29ade0655fee4caf5635f4f03d |
Hashes for vl_convert_python-0.10.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5657ed8c4e51658d47c4de0a59f4f44c3ce3c2a67bbcbfb791fd5089a8e199ff |
|
MD5 | b55a3ca63600620e6401b45c06808db6 |
|
BLAKE2b-256 | 8cab45112345a4b948335668582f6fd4f4fe29daaa2a963fb58ba41ed08902eb |
Hashes for vl_convert_python-0.10.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75ce4ff195874cf62f64ab5d0571de5fc4885f4953747a2680a6a7585cb3cdc7 |
|
MD5 | b652b4a2188211b26b1199762eb09560 |
|
BLAKE2b-256 | 7ec507da740e4a5ace52347b30b44bad54b67ba1f6bf043336f12e0ab683efd5 |
Hashes for vl_convert_python-0.10.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 449c12dbadb100e68b352a4f11a738af9b332d93d03afee63ce897622f40b95b |
|
MD5 | f81b001bc64c5d5614b19787ff4d80f0 |
|
BLAKE2b-256 | eb34da3c628bd4f53d6ba55a5579ef66f1a60c7f93317ea5b504417e9f4771a1 |
Hashes for vl_convert_python-0.10.0-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ebb9ff8ad93a5f94ae5a1e351c0daf8004e3eb14eeadd86586824880eb9e93fb |
|
MD5 | 0de3d04561bfd493d824f41f03207d2d |
|
BLAKE2b-256 | 58ae06d3ffeb02d61dfaaf0acc8bd3d95b47d1960d05dac12e3fd8811103c9b6 |
Hashes for vl_convert_python-0.10.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5208e7bef0baca4b6faeb26dd995760cf0c4720b115f94d3c576360fd67fb656 |
|
MD5 | f3b81b6f5e493f1a698e1859a607d4dc |
|
BLAKE2b-256 | 52f7229029ed85ca8400fb7b439ce0a208e8efa4f2176194ba03f3db99deccbc |
Hashes for vl_convert_python-0.10.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9718ae911f74ba3a31c08471b30ac72eb48c91cedd386b7901a54f3914393ae |
|
MD5 | 49ce376b702d4b53b2dfadb5e600ae9c |
|
BLAKE2b-256 | 20cf9b6ca1d0e22b30c4b55a4841ee2ea2aaf639a9b218b799b05591761465e8 |
Hashes for vl_convert_python-0.10.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 629970c8110b932fd365e553c80d36b9c13016f147ebbca16d9eb17ed40afd4c |
|
MD5 | 2f608aa8b2a3b995dc34ec5222230074 |
|
BLAKE2b-256 | 85e3957dcee897c335791b0907485a39e78b01dae2e68aa2101fa967e79fa3e8 |
Hashes for vl_convert_python-0.10.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3102807b0e65a919c70051ad3b7de79857dccbe221c9157c2c21414863c42d3 |
|
MD5 | bd132225862f2909bce8069bacfc870e |
|
BLAKE2b-256 | 60c5d204a93ae89823474db0ecbd5693cdc7bbe7a20a6ad91c97a118052b0dbe |
Hashes for vl_convert_python-0.10.0-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d456c892369efea119bee360112578e678a13e566ab7e9544425acf9f12a2623 |
|
MD5 | 9f68e9b36cc9be330c20710ad5e8dad6 |
|
BLAKE2b-256 | 868e11ef195f39948316910c034a2e0542009f8f95d00509f578e3b2c7509ca9 |
Hashes for vl_convert_python-0.10.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67821b227821200350ee18485be821b196c40e5cef457e8f841c47b7793613d6 |
|
MD5 | 713a431b0e7f433c6d7842a9ca6a3390 |
|
BLAKE2b-256 | fd7fa7093734041dc1fbc62d8bd175e104b63fbc05cf77b82c9217ab51da04c7 |
Hashes for vl_convert_python-0.10.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7980cbe40380865ac5e99426887f0f7c433316cd0542674e3574ed85d77dce04 |
|
MD5 | 0cfb76c3edb335e2b394929139b2bf3f |
|
BLAKE2b-256 | 181220912baa6164cce1259087d621a74872d1e33c2d8e851af4803cde64e044 |
Hashes for vl_convert_python-0.10.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4e9075a6442958591a215a1b5f037aff33a4c79d7a8f1597a9a550fd494ed46 |
|
MD5 | 3540b836d5b0c6bb5c3c31a0c1ea1d6b |
|
BLAKE2b-256 | 7963b574d19bc36caaf5753866c32d090f98592fd0964d97eed48fe27fd9ae17 |
Hashes for vl_convert_python-0.10.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8d33538c003a6639d135598c671adeae4f68f919b6aae34604fb9f8806f1aa4 |
|
MD5 | 5d275da43a79bd64d946de21dfa8a4c0 |
|
BLAKE2b-256 | acbe473e200a1f36d0cbaf9b33a6d5c18fc045ddd1bad31e6577fdd5ca5a84e9 |
Hashes for vl_convert_python-0.10.0-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ed1f79dcc29bb78915ff836bf6ddc82eb1bbf8df18669d07a581008d910af39 |
|
MD5 | d3625fcd3618348822a88ce810a5e12d |
|
BLAKE2b-256 | 12be3b040b313a399b64c8419c2ac5b4d0b609ff15603b00c6efa1f70f12f824 |
Hashes for vl_convert_python-0.10.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c572d03f2fdc38aa244f5c90753f8a3748d8077dc36f88374602ed663902acbe |
|
MD5 | 3674d72c555227c653b5344d20b391be |
|
BLAKE2b-256 | d77c964c262949b0cf42a3cd31f8499e9a255bcc18799472c39992a4921ec3f2 |
Hashes for vl_convert_python-0.10.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39147b90290f29127bc3c1868066d12397198f250bc35ab59d8e0ab5d979efb3 |
|
MD5 | ff5d9eeb4a130ef28823275543900283 |
|
BLAKE2b-256 | 2b47dc8d126ad1a3850df4d8f087d26407d10ad34113192fc8487e70f21e6c64 |
Hashes for vl_convert_python-0.10.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a55f51bd587d3ee4dc68a0e5876f8258e9779e2d3a3a7335699dbbee9c6e773 |
|
MD5 | 4830424c9e759661ba64d4e10a894365 |
|
BLAKE2b-256 | b0d6b0623a5d13ec6005e3c41e765354262935fbdc270b420cecd99e576a34bb |
Hashes for vl_convert_python-0.10.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79e966074a1304ec7d87159ee06ca40ce6bbdf741a268ae67c89461e9b039372 |
|
MD5 | 1a784b9205cb8ca01004a689eb9e6f5b |
|
BLAKE2b-256 | a8682b6627cf8874de30cbb99b16bd08eb448fae0f4c62123399190f52b5ba8f |
Hashes for vl_convert_python-0.10.0-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c3d2598eebd107786d4a8bf4723c5048e086b542f8e1d8a66aec261f3000143 |
|
MD5 | bd22b4048746d487f2c8e0bfb37ef5bd |
|
BLAKE2b-256 | 6a6fd0ca8a3dc2d5be4d5a12d888c7649f8980f69213ff51f54026a98a40a28c |
Hashes for vl_convert_python-0.10.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7d1efe1c8f4aa3bb0db34f8b04e94295b034b55484407031c1389f2b4af31c1 |
|
MD5 | f06bec4bfceed6f7d133629cbafb3a23 |
|
BLAKE2b-256 | 154414e92b194697c78a30a783b7a22ac4e0a499dcecfe929389155bb447e2c2 |
Hashes for vl_convert_python-0.10.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0319d7cfe5a0c29d40088ec79000f6828fbd8ed338139dc101d85334f35c7422 |
|
MD5 | 9f3f2e8c433ca5726174641233e4e221 |
|
BLAKE2b-256 | 812d59d197d7013017750073d57e48cddb93b3f0858d2da6157dc13a32800e15 |
Hashes for vl_convert_python-0.10.0-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a605c6a3fdbe199025a8918e8bac8f9793e16a321eb11fe03576d891cbde5392 |
|
MD5 | adebd5273276865b92367be92b3a636e |
|
BLAKE2b-256 | d391d28281aaeceb23ccc3388bd8c974220bd1696535f1733f0a5012404166f4 |
Hashes for vl_convert_python-0.10.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 695dd37416d90adee87fd7695ed7f9855114fe0e4f22c0681e123ba2bb76cc81 |
|
MD5 | 7db4163f90780824f399ba1cecc63d5b |
|
BLAKE2b-256 | ce366d8aaf083e904e58f31a3df7295964a88083ebc48da1fd8a7fe31c8b2775 |