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
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:
f.write(vg_spec)
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
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:
f.write(vg_spec)
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.3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aad74f323cb42dc214ffb8b7af9ee5963a4f2049c8147d8d4c4449204be7d9fd |
|
MD5 | d77911fc3d0d68aecd231b4da30bdcbd |
|
BLAKE2b-256 | 1dde99e9b45bf6d6c50070141b43a6d96a570ebd33088166a16731633d411bad |
Hashes for vl_convert_python-0.3.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39e07e262df17460dab7954c10ac7e7a1108e8c0a6ec498cf056da994119048a |
|
MD5 | 3c6676b651b5f4fba9bacf7d9f5abc9a |
|
BLAKE2b-256 | ce4005930e15f03ed491ab48bb5067baa6b7b7c880874c208bbf4431f9cece71 |
Hashes for vl_convert_python-0.3.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | efa1f8e0650ba264c3890ff997309b3429ca1f6d4b325bf254143ed5979ebbad |
|
MD5 | 77447f23c596f128d6be6d29be4bb59e |
|
BLAKE2b-256 | 1cc8b9b67b2512ab45d00c58170a11ad5b413b259b9cbb0433e8956c14b913ce |
Hashes for vl_convert_python-0.3.1-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d80e0c377443b79dba8584fd79f0656fe1dcd697dd620df3807e91ca0b44d61 |
|
MD5 | 4b787d2311cdbf4d5e1faa1da9e1375c |
|
BLAKE2b-256 | 6c23fde2ccd087eb3d48e1c741edc1491a83eadcd3f0fcd62515c70c10af31e8 |
Hashes for vl_convert_python-0.3.1-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2480730ab52c2db272680fb3218954a9e4844b54928b6d529c0efbd580a11268 |
|
MD5 | a163aacb973a6f59340cb4bd4b2d7c91 |
|
BLAKE2b-256 | 0dc35b98e286f5457d820041fa52292fe4103644c3eecbad74e4ee44730174c8 |
Hashes for vl_convert_python-0.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 300293c97e4a51940eeeb8aebc636b1c91157d0bc9753c91de11d6007e4cdc81 |
|
MD5 | 3d13289b60428d7fb7bc9befb418d2ef |
|
BLAKE2b-256 | 3ea150da25e354c217439aa38ff085c8ca1909327b15deff1256e62eaf85032e |
Hashes for vl_convert_python-0.3.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e063a12bcf3e1e5de7b872680cca1a21da5254224f86796cca1f936d1e930369 |
|
MD5 | b7f326f7a60c592729875bdb4f420f37 |
|
BLAKE2b-256 | 74794e719da5ba9fd08eb2b61166c72223c75a40999a6028ad41c04e0bdc899e |
Hashes for vl_convert_python-0.3.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2db6c2d9c3c1d689331e66d250b27bc33898bcf750e8f1e57ab5c0a650a18f0f |
|
MD5 | 907cb41ce763a6528d0816ec32699c77 |
|
BLAKE2b-256 | 0dc4b2c733e3184dcdbca28bf60179ac415c7686aef68642cfc54c13fb61ef80 |
Hashes for vl_convert_python-0.3.1-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e03aeb2d1cb3aa994c5f9d6173358a0d1a871225bd43fea321d83de89ef3b648 |
|
MD5 | c03afad83a90a1672ba4b1f3a1ba0b7f |
|
BLAKE2b-256 | 6e3c26b9475a00e7146e7e377b552f4508920aa18aeb63730b0be07f413596e5 |
Hashes for vl_convert_python-0.3.1-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4dc835b7db10fa8a9790497416aee40c84989f11c3bfac453e730767f2f4c1b9 |
|
MD5 | 70842640f8950f5230a3b7544903f95b |
|
BLAKE2b-256 | 059a4d55c2482959248dac8c8acdc81803dc2e31a994ec832159bff21cfb27e6 |
Hashes for vl_convert_python-0.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fadc78695512efbfea6fc90b4ed21c75107cf0c86f1109b62e3ee0a0d7ea11c2 |
|
MD5 | a926688994eba0678d17004d8647e5ce |
|
BLAKE2b-256 | 28dce15f11ff49ee3d6e5ff149d10014c667853a04cca14411276d5a5e8374d6 |
Hashes for vl_convert_python-0.3.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eefe8ab66885eea28cc3ba7b213007b224d71232228e37fb64384eeb6f285bd2 |
|
MD5 | e98ffceb0ca5ec10d61d6e905bb2d934 |
|
BLAKE2b-256 | 2a4636af35eaec55cc742f158450f7dff23eb28f269cbc2e8ca8e74286dac1ef |
Hashes for vl_convert_python-0.3.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c48abe68015d5ec02f968a4490661d9bd5f27a0d72ab0bcf1bc75a1e3f53539 |
|
MD5 | d6a3ea4daf19520bfee52f315e7829ad |
|
BLAKE2b-256 | bac5444dd6f78a271e5c8eee194c9f165c3cf6b333013b7a5a27dadd48648e40 |
Hashes for vl_convert_python-0.3.1-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce7d6d5ffe6eadb07451e11815126bdf4cc2aa9b84fd33e7e1cc718f4ec63c88 |
|
MD5 | 622ba856f449e76b4429149d00b6a4fa |
|
BLAKE2b-256 | 50d9a4b817b1b1b356edeeb1f24bc02ca4eadd620f99c61e0472aeedc9fd9265 |
Hashes for vl_convert_python-0.3.1-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | adca60615c6757f1c31a54ede43a03e021bfacf40b9cf0f6ebad13288ed6a971 |
|
MD5 | 52272f45fe1861a144de13712262cdae |
|
BLAKE2b-256 | feeb4b1a6a4a64d17331461210b62beb805e463a79325acc1e5151e010936421 |
Hashes for vl_convert_python-0.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5be4ebe511b2da6e6e9e8a6d12a1cd4ec047660786270c8bc6639be17e6bb683 |
|
MD5 | 6f8283d20d7c329df01395c8b886fce7 |
|
BLAKE2b-256 | f6f8d610a77d297ec13810df9b0cac1f6d88390789ca8873973c0928d7ee41de |
Hashes for vl_convert_python-0.3.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0bdf9c9a694fc7f56d4ceadaaa93e0e2920c028aa63ec50e654387f49ddea35 |
|
MD5 | 4d718ef4548321612ad5fab83efcd30f |
|
BLAKE2b-256 | bfbb1dd3d52e0b1aa40ab404e67bf785838a2ff9db5de38b257d8760026f500c |
Hashes for vl_convert_python-0.3.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | add3648d13df3d9f97dbb8ac160b2f0d9e714766f168b221ab061528a0b1ceb4 |
|
MD5 | b7c68d4746bfed25401f5a2dbb42328a |
|
BLAKE2b-256 | 0f11d62a165cf3bdb57bdb8eac85b598e907a637b94f22342b79c7b36f920ef4 |
Hashes for vl_convert_python-0.3.1-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3358f1ae25bbf2d6f9a2bc887b309dc4a92840721c5b638549b0f9331a0adbd3 |
|
MD5 | 397d06b2a66977db59708ec3c0101948 |
|
BLAKE2b-256 | 8de52f8b7fe3351074de71144183eac1aee3b381bda02dc506bb7ee2f5a55d4f |
Hashes for vl_convert_python-0.3.1-cp37-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b5f17251b6da69c54ebeb0b60008d70a7179516bd6f2b01a6eb191cf9584cb0 |
|
MD5 | fedef9b43865cd36674efd9dff103cfd |
|
BLAKE2b-256 | fa69a4a781a40ae1da5a8712d25f9261542c656731e026cca4a439256bb37db7 |
Hashes for vl_convert_python-0.3.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 886cdfa255213bb302a5b979e703a203012c19209a5c7445f366340d1afd0e2c |
|
MD5 | 45f0347db9b600b2d4f22525126d9b2a |
|
BLAKE2b-256 | a67fc15c36a2be0bbd84c8f999672209cdb0fe3d72361d65a0d75ada561ee05c |
Hashes for vl_convert_python-0.3.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3078e6e78e3d10af92378bdb2d5d3238bb85632449c1653402bfe5f5d9e2fec8 |
|
MD5 | f72feb061ab07f912957ab156b0ed178 |
|
BLAKE2b-256 | 61594954b083b7db5d03f608c2df57a0c7d1a2fc782789e4c1085c225fbef5da |
Hashes for vl_convert_python-0.3.1-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b8620518e1cf47b279d2f4707c5807fb323c3509cb3d30c95ff98a738b100be |
|
MD5 | e0f426e63b11e8ecfa92d5f6c7a8c068 |
|
BLAKE2b-256 | 48993427cf596e989a98fc3e79d5bb689efad0a9c81baea092ca9e01fa6fe2d8 |
Hashes for vl_convert_python-0.3.1-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5279940a2e3200bfd5a7941d5fb3f3db2efab100481beaa1bd8c92e6c362231 |
|
MD5 | 1579da53fa648260d2423422186d8b62 |
|
BLAKE2b-256 | 2760917cc99d8a70ddf2fcafc3844a7a5a61224ef7ea35404a09986cf3436989 |