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 and pip install remaining dependencies
$ conda activate vl-convert-dev
$ pip install pypdfium2
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
File details
Details for the file vl_convert_python-1.1.0.tar.gz
.
File metadata
- Download URL: vl_convert_python-1.1.0.tar.gz
- Upload date:
- Size: 4.0 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.3.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc2bf58e019a5b4796b2903af8eb952555a7701603e0752e9f9fe66627af5c2e |
|
MD5 | 1e64172e32df5df4a40ab9fd84411bb7 |
|
BLAKE2b-256 | 9c9510aa327ac9df3fc6ab709cd6cb0deea98aee38ae6567be8c3f942846ba81 |
File details
Details for the file vl_convert_python-1.1.0-cp37-abi3-win_amd64.whl
.
File metadata
- Download URL: vl_convert_python-1.1.0-cp37-abi3-win_amd64.whl
- Upload date:
- Size: 27.1 MB
- Tags: CPython 3.7+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.3.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | df907f8ae6d799f8e26ba40c93f1bf98e5b6dcb31797e3a6acecbcd851251cdd |
|
MD5 | 1cfdd71ec4476caaee3259bc4b9117f6 |
|
BLAKE2b-256 | b12b7199cdb7c889c8d393e9ca787e9a05321904c6a99dc5a80c09849eecc4e0 |
File details
Details for the file vl_convert_python-1.1.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: vl_convert_python-1.1.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 27.0 MB
- Tags: CPython 3.7+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.3.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83b013c44e8e022ce1984d26cd38703544bf595065dca39009e8653e0c4cc377 |
|
MD5 | 377e31154bde581555cb375ef3eb8b41 |
|
BLAKE2b-256 | a5f77c5443f644cbddcd698ae213bb8f3914c806fc89d436ed44cc90acfd1cad |
File details
Details for the file vl_convert_python-1.1.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
.
File metadata
- Download URL: vl_convert_python-1.1.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 26.3 MB
- Tags: CPython 3.7+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.3.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f12983c2d379cdc3f273063befc7cef620a1304991c6c89d08e7b5591b4023a |
|
MD5 | 8f55092414875fa2ecd23e7142e31955 |
|
BLAKE2b-256 | 3c283b3c842746d0dc9719699043a48d8a07189d4d69e0294eb2c8705903c908 |
File details
Details for the file vl_convert_python-1.1.0-cp37-abi3-macosx_11_0_arm64.whl
.
File metadata
- Download URL: vl_convert_python-1.1.0-cp37-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 24.8 MB
- Tags: CPython 3.7+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1b61dd9d6ad171f9b09545a3af77083eebfc91075f3913e92ddf667efe97099 |
|
MD5 | 9f29d90294012e66d6a331ab550a2049 |
|
BLAKE2b-256 | a9a698b785ec3b0b9e9762bf18ae7e8f9be592bbc49fceaf9f79f7c8a58211d9 |
File details
Details for the file vl_convert_python-1.1.0-cp37-abi3-macosx_10_7_x86_64.whl
.
File metadata
- Download URL: vl_convert_python-1.1.0-cp37-abi3-macosx_10_7_x86_64.whl
- Upload date:
- Size: 26.1 MB
- Tags: CPython 3.7+, macOS 10.7+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.3.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e4fee1b0dd19a56935724d77bb471e7fd05eed99330848c1b2547c859532fdd |
|
MD5 | 684b2e2979b5305a62f8ff95df2b192d |
|
BLAKE2b-256 | 86d259008849bd02d2c5bdcd29bef2416a29adb79c615332d2d1aa41b4d2e123 |