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.1-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c75b9a1f7343562307f82adf9c7141b599e775550c42b087569ae839868ccf23 |
|
MD5 | 9a43d50e32a870bccaaaad287e7f4a43 |
|
BLAKE2b-256 | 65ca15beba8590a0b1f1e1f3745331e003fd7dea5f07da851e0f6c4bd9d49781 |
Hashes for vl_convert_python-0.10.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee1396e014dc3a5d1a7c11cf817f0415bb1cb94e0ebde4e410b4a5f3818a9285 |
|
MD5 | 523a5cd34bf835c023ac9bac7be9be33 |
|
BLAKE2b-256 | ba3648c224144c5254aa977d97ef2dcc520ab6537ac0f3ce44aa7a95690b30fc |
Hashes for vl_convert_python-0.10.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ae3be0f445217f3e6a5bd74fb918e51fa522a6c27a4815500484e210803de44 |
|
MD5 | c40ecb061ef907221dec7d755ee6b9bf |
|
BLAKE2b-256 | 533e8ee323ab843880fdad8c966cefa4d1f30cb1c9cb763608c455c18a9f869a |
Hashes for vl_convert_python-0.10.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35b4ec3e82251ddb1fd5f35a8398c22ef2bd9d8bd2958cf24b7fafb29ba1e1ff |
|
MD5 | 10b87928751efc12b2f523d8359ebdec |
|
BLAKE2b-256 | 41b27a0e4a44ce52343e9b9b304692ca1d4e1ef215e6a3bf7b1f6b38aa6fc98c |
Hashes for vl_convert_python-0.10.1-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 491261a7609cc2d08fae83e22fa727458b968d2f5fffb32aacff8d26889147ce |
|
MD5 | 953b915ee9e6f8a40a783bb0aa080cdd |
|
BLAKE2b-256 | 50454ff1e478cc117b6142fa9e2ee1e20ccfebbdfb6bb03a775f826f21ab4f5f |
Hashes for vl_convert_python-0.10.1-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bc75121a636dd5e7d6ee70dad2c9bc8ac0ea2be528e6870e49dfbe9b1febda4 |
|
MD5 | 3003a9a9bea431a008432caa697882f0 |
|
BLAKE2b-256 | 68ffa3eb54ab410c0da46344b0a6ea0079f775c6a2c96849d4d1e0f829b7bdf1 |
Hashes for vl_convert_python-0.10.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92c5b1f18a6283c969f1fb27e3934881f6c31257870b8ec8c944002b0155ea5c |
|
MD5 | 42366ee755b6f39d364d53a73488d639 |
|
BLAKE2b-256 | 7c7b7402959587227d38ec6d2070d919ec4b66b1951ad7ae69b0c9d15e049fac |
Hashes for vl_convert_python-0.10.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41d654d04f25bd11c0f1d4105d7258f1e4e9920bf61d8d5f3c84050a76f1107e |
|
MD5 | 9790ed5adf2f3600d1464503fbae7982 |
|
BLAKE2b-256 | d48e56146ccd166a5f120c91b4c28aca5fbafe087cbcc2f338ad74141be4c44f |
Hashes for vl_convert_python-0.10.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b9ad77817ea1980a4ff9b6c6cf1338cfa6d5744decd0308b1376a3a17f8e598 |
|
MD5 | e6aa2785669a612a09095b21d4956db8 |
|
BLAKE2b-256 | a320af2a1d851cc110776ddec4805bcd23b7377c311c5932a0097203bdd5c56a |
Hashes for vl_convert_python-0.10.1-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbfced8dd245192d439b13a5d7fe4ad3fbbcf10dcbe817723d80020f1b05a8ec |
|
MD5 | 1a3e27224f00f274228dc5f49b297ed0 |
|
BLAKE2b-256 | 81e9610090f4589a20e973ff0538a3a2d2c62caad95e82c075e97326efc2e0c0 |
Hashes for vl_convert_python-0.10.1-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7551fc5588c6881d653a1fb3b7d9aacec70363f443a3bf29c1b0eb28529b376a |
|
MD5 | 6fa3a9d4692e33bef93400876ca00bae |
|
BLAKE2b-256 | 4f9513575b07dec729eb96e0c6cc815fcdf91a05e69ddabac6b47aadbb2397b2 |
Hashes for vl_convert_python-0.10.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6b60b8d65d29c8df08a7f0bb88f31767c25dc4be7efc358a1402a48a6ce56d0 |
|
MD5 | f31b01592a58108aa49c96ff790e972c |
|
BLAKE2b-256 | ae265e5aef1ef5964ae96ee78026bf68d992f3fe2ac19c1d788894095a8054d1 |
Hashes for vl_convert_python-0.10.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25b70e90597b4efe0a6918f9d98df584e0ad0f2d2bffae0cd3e2c6faa52b58a7 |
|
MD5 | cd7b5e1081ea8f9e3a1d442f521420b9 |
|
BLAKE2b-256 | 85afd706164f5d19075c13f6b8882e8861ed84450003b37b8d1b03fa99fe6d39 |
Hashes for vl_convert_python-0.10.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 609617f4f498424451f78cc7d7d885d772eb1050f7d527c229ad0c5c2f9093d0 |
|
MD5 | 2a4420928448a1b81103ebca0b867bd5 |
|
BLAKE2b-256 | 2dc9114e7e567c2f975eb3fbbeee4eae1cb1fae9b115733d9b6df8f31f4d57f5 |
Hashes for vl_convert_python-0.10.1-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6493a512f14522cb25d7e290e5fa2fad7278bc7abaea7fd6d3f6431a269e92b9 |
|
MD5 | db5529491ff750fe117fe1d57dccdf0c |
|
BLAKE2b-256 | be961005f2c266090a6a7284ab4bb91ad14944de3a36575400d58cf8fb7c69ac |
Hashes for vl_convert_python-0.10.1-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d61c1b86b2f0675417c194f993e90a128005ffc463040dafb7bbaa28a180ca9 |
|
MD5 | 9a941f069f0c921f0db257c20aeb55a1 |
|
BLAKE2b-256 | b97abf55d990d18b1074bad5f85e91c0571129387970af42a5c4e89ad889227a |
Hashes for vl_convert_python-0.10.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7d7c70d23989624d60f698d07c4646e7ef2373035541dff3efd64aceda5f4b9 |
|
MD5 | 94a10f80339e0dc160dffb29ee36cacd |
|
BLAKE2b-256 | 1a4109a158280eaca9158cf5c42f6b9b45e53a1be67a1f26186a0298a7b2e3ac |
Hashes for vl_convert_python-0.10.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cea711e41eb5cf4b3593e086f3f79e220968056301ebda763ba57b2d288b743 |
|
MD5 | b7b2f5a7f4ffc15fe6b13161cb92f42a |
|
BLAKE2b-256 | a38db563fefcef45d514bd018f769216480dd6970a8d1bd24c01e266b2849364 |
Hashes for vl_convert_python-0.10.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b93768d001436c5358069b43123fad32e1da675478c0eb161004c4d8f3ac983 |
|
MD5 | 561e4594297445ea64d13ad3618438ea |
|
BLAKE2b-256 | 927fd82d9eab951f4d7d7161b578ca01ce279679e0429a6b2dd01fc978dc1351 |
Hashes for vl_convert_python-0.10.1-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 420c2ca9e55f78fad30b88783a41b3e084d3891e6f375827373e359b1dbdadfe |
|
MD5 | 2d5c07ac87d13467a1f69a45371a86c6 |
|
BLAKE2b-256 | 90df0412f74654810189238ca8e861fa2040e990efa84479e945d1afaf025480 |
Hashes for vl_convert_python-0.10.1-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f468e08a9989f85c57ef071d604fcfae02152308a1e50ca760de8ffddae774b |
|
MD5 | 3355187ec4c9eb4273bee28f005cac90 |
|
BLAKE2b-256 | 0d4e0bb37ea3af2124edae02b28d9fb1ff69fab401ab768cca13281a2686fd27 |
Hashes for vl_convert_python-0.10.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c05c1e46b131e29b83c13ad79c1448c8145c85fa40cf3675265252917ccc1a0f |
|
MD5 | dcd9165f29692b643fdc8083f7df34f8 |
|
BLAKE2b-256 | 4a06709d6aa8d0dd0f68eeec070f9e53a3574a6a6ac3bbd1c92d79a9adbc903e |
Hashes for vl_convert_python-0.10.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55f91881e45d4fe5b02630de374befe668d3a4116397172a07f573a4f92023cb |
|
MD5 | 2a97a6b347fca6068f6d8934638cf2c7 |
|
BLAKE2b-256 | 110811c33ee5cb8ea84f526ea5fd2eb1f89b00d8031043bc7d2b7b23a187cd1f |
Hashes for vl_convert_python-0.10.1-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f728f86df79fb2de37964e430b6542030709592c114ffb66d6132b1d765b089 |
|
MD5 | 4a6c1defe02da8d593374f7c3718d1c9 |
|
BLAKE2b-256 | 7503dc63931336d01d5c09a4cb7068c9ebb8bdbc578c60a4efa5064f268d40fb |
Hashes for vl_convert_python-0.10.1-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e133827971eec62ad21cd48c006fb7130973715c03a9b384eedf4d292d158ab7 |
|
MD5 | fe6bf5f2b4358130e10c82bf7331b8df |
|
BLAKE2b-256 | 3e3a3c5022c3d0dbe0954944586eefbe5b2d535749bfaff456a850d8ff2b75d8 |