Skip to main content

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.

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

$ 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vl_convert_python-0.11.2.tar.gz (3.9 MB view details)

Uploaded Source

Built Distributions

vl_convert_python-0.11.2-cp37-abi3-win_amd64.whl (22.9 MB view details)

Uploaded CPython 3.7+ Windows x86-64

vl_convert_python-0.11.2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (22.9 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

vl_convert_python-0.11.2-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (22.3 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARM64

vl_convert_python-0.11.2-cp37-abi3-macosx_11_0_arm64.whl (21.0 MB view details)

Uploaded CPython 3.7+ macOS 11.0+ ARM64

vl_convert_python-0.11.2-cp37-abi3-macosx_10_7_x86_64.whl (22.1 MB view details)

Uploaded CPython 3.7+ macOS 10.7+ x86-64

File details

Details for the file vl_convert_python-0.11.2.tar.gz.

File metadata

File hashes

Hashes for vl_convert_python-0.11.2.tar.gz
Algorithm Hash digest
SHA256 3e2db75b9236f1f828e38011ae05c7ad6c25fddc42eec70e5bc1cd2e7b7f6bdc
MD5 388eb61b5b15195ce4e2e67a4a536779
BLAKE2b-256 51867ce75d360ca8e7573196b009997cceb571ab254cb9af8b74579f0891b9e3

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.11.2-cp37-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.11.2-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 97740df713fb44a561c78ffb1bc3865f9a276d751866dddad1893d22587d6713
MD5 a150d68e77fb24437e7ae978f977603a
BLAKE2b-256 06f6de2321275198341b8dbcc12b1ef093c9f4847b0a547d6ba5e1c2eac84140

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.11.2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.11.2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1056c57a91aad00661d0394f46ff24b945e5496524e84a587739b7c7025076ce
MD5 adecaf576145ba374373008596c9e05b
BLAKE2b-256 dbd148c2c167187b406cc764af5cde203a37c24b46c06c3287870dba3ddbc3d6

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.11.2-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.11.2-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 4e90d289a11c9f6e8ce4de43c616db39970b7a5b92e8dcdec27aeee600d87bc2
MD5 2aaa515bcdd41d38e90a26fdc7df0074
BLAKE2b-256 c06918af3f2a00e7de381a866232c92e14bf0feef203e48314b6b107d27df97b

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.11.2-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.11.2-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9136510d3ffe9d8f330bec186aeb55f80a14a9fb11b44b435a56cd5f73f829d4
MD5 c1c7178abcdd610be62b6b2d904c605f
BLAKE2b-256 f71c7b6088b4004a7e0d8dee5fa770a6408bea624034cd36a5ba500d4017465b

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.11.2-cp37-abi3-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.11.2-cp37-abi3-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 5d9648c1963ae805373f013ff13edb20caa6bb7f61cbc26409ba48cd9645a3e4
MD5 3b4e25386e4d44acb992a029c2d2028e
BLAKE2b-256 01bf0338d7beb4aa4761c935aafce73c017adbcb4a03dba873a972b58b97a3c0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page