Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

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.

Try it out on Binder!
Binder

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

Metadata

Release files for vl-convert-python 1.0.1rc1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vl-convert-python 1.0.1rc1
File Size Uploaded Provenance Actions
vl_convert_python-1.0.1rc1.tar.gz 3.9 MB Without provenance Details for vl_convert_python-1.0.1rc1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for vl-convert-python 1.0.1rc1
File Actions
vl_convert_python-1.0.1rc1-cp37-abi3-win_amd64.whl CPython 3.7 abi3 Windows x86-64 Details for vl_convert_python-1.0.1rc1-cp37-abi3-win_amd64.whl
vl_convert_python-1.0.1rc1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.7 abi3 Linux glibc 2.17+ x86-64 Details for vl_convert_python-1.0.1rc1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
vl_convert_python-1.0.1rc1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.7 abi3 Linux glibc 2.17+ ARM64 Details for vl_convert_python-1.0.1rc1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
vl_convert_python-1.0.1rc1-cp37-abi3-macosx_11_0_arm64.whl CPython 3.7 abi3 macOS 11.0+ ARM64 Details for vl_convert_python-1.0.1rc1-cp37-abi3-macosx_11_0_arm64.whl
vl_convert_python-1.0.1rc1-cp37-abi3-macosx_10_7_x86_64.whl CPython 3.7 abi3 macOS 10.7+ x86-64 Details for vl_convert_python-1.0.1rc1-cp37-abi3-macosx_10_7_x86_64.whl

Total release size: 134.7 MB

Release files / vl_convert_python-1.0.1rc1.tar.gz

Download URL vl_convert_python-1.0.1rc1.tar.gz
Size 3.9 MB
Tags Source
SHA-256 checksum
How to use checksums
824ac71cffa31718c7cad8f307359d1fdce6d215ef3e71f16b57acab6f98cf02
BLAKE2b-256 checksum
How to use checksums
fd5bcf0e29fbc3ba728139757d13250700624a2d9cb4dabb7fb741a9f90ecba9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.3.1

Release files / vl_convert_python-1.0.1rc1-cp37-abi3-win_amd64.whl

Download URL vl_convert_python-1.0.1rc1-cp37-abi3-win_amd64.whl
Size 27.0 MB
Tags CPython 3.7 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
c4c41fbd80afd87d4b5a430c84c86ebe211279b58ff272158e5895531d8131fd
BLAKE2b-256 checksum
How to use checksums
4bff19fd903b38ea1fe11c65ff947d7f1e1fd4fc04aa38a02f643563ac95f1f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.3.1

Release files / vl_convert_python-1.0.1rc1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL vl_convert_python-1.0.1rc1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 27.0 MB
Tags CPython 3.7 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
b860c00be9a3ce40097fd2216180fc0a1e6493c9521f7ebcba446dd77eb60de6
BLAKE2b-256 checksum
How to use checksums
bd6fa3b82755af818105713992a5a4de5eddecd7e60f4e82d29eb47b19e9f300
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.3.1

Release files / vl_convert_python-1.0.1rc1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL vl_convert_python-1.0.1rc1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 26.2 MB
Tags CPython 3.7 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
7196f94a36fb367cf960ca32e27272349f6131e31e96af0e4e32eb2a77914ea7
BLAKE2b-256 checksum
How to use checksums
bf499b31c9c4165d2b320a2356c04f99b897af995cd6dbd388f015d78e9432a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.3.1

Release files / vl_convert_python-1.0.1rc1-cp37-abi3-macosx_11_0_arm64.whl

Download URL vl_convert_python-1.0.1rc1-cp37-abi3-macosx_11_0_arm64.whl
Size 24.7 MB
Tags CPython 3.7 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1a699107b2cc9f85c8fe361250618fe34b6ed064ef9ad4df5cddec40bbe8dbea
BLAKE2b-256 checksum
How to use checksums
99ab8d429ad07403def556016c6223b7e7cf3dff9b77089cdd7f5adf6b2cf73f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / vl_convert_python-1.0.1rc1-cp37-abi3-macosx_10_7_x86_64.whl

Download URL vl_convert_python-1.0.1rc1-cp37-abi3-macosx_10_7_x86_64.whl
Size 25.9 MB
Tags CPython 3.7 abi3 macOS 10.7+ x86-64
SHA-256 checksum
How to use checksums
6b9266fe7300f189c06cdc93d95080edabb1eaaa48c58c1d4d3187f237b70921
BLAKE2b-256 checksum
How to use checksums
84e810baf0945ec355822b4736a18567ca2ac97042d9bd4f22c9811d7e6edf37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.3.1

Security for vl-convert-python 1.0.1rc1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page