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.3-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2532a1e6d2c5b283709b10b78799e91ebebfe1a943e1f0e5208d6782eb332dc6 |
|
MD5 | 69e6993a85ce9300e80a9843e1ba9acc |
|
BLAKE2b-256 | 70eee62b9b306ec69997af32b961fda2ebea89f60c772f3c0b8b683c6aff7f14 |
Hashes for vl_convert_python-0.10.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bcd2b599666bde54d359bf2f77ae409732554aff31288a35e3e60fe1bffb2e18 |
|
MD5 | cfe89c9402ff68f6e8b66f9b3dbd5c1f |
|
BLAKE2b-256 | 8ff453935af6d002c15bbe980626b23608ea87193a2127da86666cb239099da0 |
Hashes for vl_convert_python-0.10.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd92b02e73ffe2b6891ef74658e46053226c3b2080a4a3fb09ef35adf565cb11 |
|
MD5 | 9524c2949588fc21c21295b96b3384d1 |
|
BLAKE2b-256 | b0052db6f2608e99bdf9c6b74d9cd0178132ed370d5165d7d676bf60e61f5f4b |
Hashes for vl_convert_python-0.10.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3052d35e8ebc2d360c78b933af8dd9fe2b29ef522ffe9b2831aad07fbd573919 |
|
MD5 | a10d8d2a7640df1b2047e1130274d38f |
|
BLAKE2b-256 | 9330220648bc4bcca0e44f33ab9d378f416da4a1e996dd3621a15db1de131343 |
Hashes for vl_convert_python-0.10.3-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e6d6792d31d7099fa2d2fb5b0e7f5ec88f54105f1eb198200dbb85ef393ddc9 |
|
MD5 | 55d5847dccb55f40b52f8d23aa3c9247 |
|
BLAKE2b-256 | cc6a2b921ac655918c30e986be8b469128f30b9d6c7aff5805c5a938a4f75208 |
Hashes for vl_convert_python-0.10.3-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb6f46fa876258b79c2beef1316c34f68069cbd4bad00bdd1e883645d0d69bdc |
|
MD5 | 3695e77bc45e4b0af1484ab61289e8eb |
|
BLAKE2b-256 | c0c5feacd75c070cae5a34a9be2607db743d029350912b54d8e6f27ae8357999 |
Hashes for vl_convert_python-0.10.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 523ff6f6d7076c0712d4c65d4bdd033038edc835649aef8d01aaec8a1063306b |
|
MD5 | 9830344beb6b6fee33cd85dc266c730e |
|
BLAKE2b-256 | a9171ecc23ffa54589484f5f1fbf72e0118cb707cecd8fa4c992b148f5184f97 |
Hashes for vl_convert_python-0.10.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fedf080b8b57c04e988cd4dd2cb8493d2706f2c126b40ae47460f82a666dce00 |
|
MD5 | 3ddb5ac7c5aa2434fcf14ab4b12353b7 |
|
BLAKE2b-256 | fa2c63ba0df125579ece23d9b22b840747ff2282a53f13f33e2df7beb7437dbb |
Hashes for vl_convert_python-0.10.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c28095287854c64147e6b60e2c8f235deb732b6462f6fd067b23f604d3319104 |
|
MD5 | ff0a9efd4492b51aafcef9f5efc0016c |
|
BLAKE2b-256 | bd52004cefa7dc53764af503778042af9edbd4863318d3f57d3b5cea7c90b20e |
Hashes for vl_convert_python-0.10.3-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5fbc1995a082273c5fab3580400e0f290587dda302dc10781a49dd54335880b |
|
MD5 | 4b62b4a2dfb293c2bf2f8feed26cc175 |
|
BLAKE2b-256 | df765b94e5669721e3771dae58fdddaa1616e34f14053c79a0dd717d4be8174c |
Hashes for vl_convert_python-0.10.3-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c180dd469de6996031b64de5ba52a58eae2863f39893301565b575e0cba2efa |
|
MD5 | 04960be11fcc60e3f2f0d1f46d548d9a |
|
BLAKE2b-256 | 8a9c5aa5fe8e6a66a0028e8c29fc1085f1c6290f2505f347ef8e9e7ce6e2f21d |
Hashes for vl_convert_python-0.10.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e635d5100a30d92e980b1fb75af282efd4a99db6a809bf81ce04cf6a2d639922 |
|
MD5 | 68754f927b95467cf0dd59f67b317e0a |
|
BLAKE2b-256 | 5035a0331329fa131dfc44fe139cfe9a9b81d3c9545f1ce7745857e26196e7dd |
Hashes for vl_convert_python-0.10.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | abf16b4a46254f35e40335922e361e29b9dcbd55a7b5a8bb8c49fbd933ee3b1f |
|
MD5 | 5bddb782c8fc29242ac90d7438efb725 |
|
BLAKE2b-256 | 054dd3df8c3b4eec3e9bc97ce7115bbc9838b9587b9b7d44b718f0128c6b91fe |
Hashes for vl_convert_python-0.10.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f163c8db68d483926eccc8f787ee4d8483b5ab9188391dceb8ee77ebeeac152 |
|
MD5 | ffdb73efee20899cb091c700ab482026 |
|
BLAKE2b-256 | e2d72828df4d27b82c18ff3ce015812e30b61c4ea1a5025c8965e7c4bf42a21d |
Hashes for vl_convert_python-0.10.3-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be448755bc38a9725140bb3a0dd0f7cf0644c3868f85685ca9df026021e98d17 |
|
MD5 | 35e18e167c0d510da6d50fbc8b4035c2 |
|
BLAKE2b-256 | 07970f4e87665374f4e32e4a6846183aea490f06a519bb9fb9e514125bb0286a |
Hashes for vl_convert_python-0.10.3-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d91782f93e859a4528bb638a732024e41a82736671440faa38826ab91c8ab0d6 |
|
MD5 | ee017d4bc62da11e90e7882200fd4e95 |
|
BLAKE2b-256 | f9e93e52e6f11354e58d48c578aaa05a42157c69dc5b01ea5d58b653d4ed1820 |
Hashes for vl_convert_python-0.10.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63c9294fd3d58031e8b3d37ebfbc0a40d5a09671d93e060730697488db255205 |
|
MD5 | 15a72fe08f1b1dce1e3777abbefac7cf |
|
BLAKE2b-256 | 2c47ec60c3ca7003628bd5bb6eb320e41de518690091fee8e657e6d1f89cf27f |
Hashes for vl_convert_python-0.10.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3426f9c800627098a2e79d103cdb52062110336bdd234997b33954fbd26a701 |
|
MD5 | 43e1b7ed2dfabd8266393f64d6f4b99a |
|
BLAKE2b-256 | e5bffd143b8504699b3866dd920e2ad6b69df7a12dfd8281f26f6a9c63df59b8 |
Hashes for vl_convert_python-0.10.3-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9475f6b6b0b5c559724ef5418bea8889226fa50eff6b6cf55e5a0a29e76ca2d9 |
|
MD5 | f68f14b327e794535c30de5297f05647 |
|
BLAKE2b-256 | e18feaf38e6ef0cd69eab859a0d426930ebdf5b7d3118653f11ca395229a5467 |
Hashes for vl_convert_python-0.10.3-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1ee22e645958b6c0754edbeb9b5631ce2b1f32a80aab48f16722961a4fb9e1d |
|
MD5 | 1b7d6bd63ea57003e089e17f42d425af |
|
BLAKE2b-256 | 784162cac6afbc61fbe47670bacd315313a7197c6c1bd773ad5bdb0ba5b5422c |
Hashes for vl_convert_python-0.10.3-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ef8ba0b082bc34cccfe10fbb0ae4594244940fd8234fd9c71d85af8b8cc7515 |
|
MD5 | 3d2c825371cfc3521212b0c32bb390ea |
|
BLAKE2b-256 | c229bae7b1d084043f4ed7345cdb8c629b92e73b7c9c1cd98792852127f9c7cb |
Hashes for vl_convert_python-0.10.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5af9f00e08aaa0d0e7d53d940fea9ffd45344e7c5da5b0922e2b0307918e0fd7 |
|
MD5 | df78f62a136a2f78277f587b29bbdb9a |
|
BLAKE2b-256 | 8502a0a6d85ae02e3e4aa7427b7e16c3ec03146e5dbfb9293874b1295c387add |
Hashes for vl_convert_python-0.10.3-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f189211b55cb7a0e32ab6d00fe71553a58614c987b0d283002e9fc0bfe5cf251 |
|
MD5 | 6e6482a4c5dbed50058d45e917a20858 |
|
BLAKE2b-256 | fbb17c31a4d2f33df60d2943a7446fff04bd49dcc2af95a9e17034e2095a3499 |
Hashes for vl_convert_python-0.10.3-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee12873f8b66ff62c63f5fd71284a4ab0ee68755dc649e77c0b46dc4ec84a151 |
|
MD5 | e29867a0274f6e936422f867fe193ed8 |
|
BLAKE2b-256 | 10b57fb888eb54ccd8a263f5be096319c104a556051613284ce4b11ce5a47116 |
Hashes for vl_convert_python-0.10.3-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6b4377258fd0634c0e713445b7ba38468f3579aca2ea4f4ab50b5b635a1a4b6 |
|
MD5 | fdd09d0a360fd7c03b81d7a6808e88f6 |
|
BLAKE2b-256 | 17f62526b70ef03f1281ba5ab24e6fd3f7bceec9b3dfca243f2f42798358cce1 |