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.13.1.tar.gz (4.0 MB view details)

Uploaded Source

Built Distributions

vl_convert_python-0.13.1-cp37-abi3-win_amd64.whl (24.4 MB view details)

Uploaded CPython 3.7+ Windows x86-64

vl_convert_python-0.13.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (24.4 MB view details)

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

vl_convert_python-0.13.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (23.8 MB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARM64

vl_convert_python-0.13.1-cp37-abi3-macosx_11_0_arm64.whl (22.4 MB view details)

Uploaded CPython 3.7+ macOS 11.0+ ARM64

vl_convert_python-0.13.1-cp37-abi3-macosx_10_7_x86_64.whl (23.5 MB view details)

Uploaded CPython 3.7+ macOS 10.7+ x86-64

File details

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

File metadata

  • Download URL: vl_convert_python-0.13.1.tar.gz
  • Upload date:
  • Size: 4.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.2.3

File hashes

Hashes for vl_convert_python-0.13.1.tar.gz
Algorithm Hash digest
SHA256 d70a608257dd6b5b782d96cccebfe7289992e522e47a8bebb7d928253ca8b396
MD5 8459b1d64e07c0455660b77866739931
BLAKE2b-256 10626e41e11047eecb596db4cdc339675386f087556e2b2d426c73d269358c95

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for vl_convert_python-0.13.1-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4c9fdc3b74b2aafe72c699988e00ee783c0cb57f967182edeaacc26343fdaa92
MD5 ef0e49f50a58a64b230f7b02dc505d0f
BLAKE2b-256 33f33b61d753105b67bbd6ef745f900b57e635001fbc3d5e32d11668fce463a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for vl_convert_python-0.13.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1b8ab9aec2735191eb975a9d2c793ae88840d41ceb8c3780647a4579efd18dda
MD5 1c129d0041f45823d24629b37a93a7d0
BLAKE2b-256 8fb8d2296fa7e79312aa1d8788828ea2fa0269b818e3c74f99d7a06554456624

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for vl_convert_python-0.13.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 af93d9b329e0d0aced439cf1b4f4db89a57c96217b12f8d9a1b3fb821efea809
MD5 a4a91b7f93e6f432cafd25caaee56307
BLAKE2b-256 d20c1b1cef0cbc80cb7917166d71afdd6d63d04be851b9d52f051bec7f471089

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for vl_convert_python-0.13.1-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fdee863e96ebae784a6497a5a8976124febfe4a93d766fa41db973f85d369003
MD5 e09875e4a84ae048f11d14f18016e42b
BLAKE2b-256 012560b8c323f7184cb5537665e405217853cfd4f9501db0c7aada1ae564ef5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for vl_convert_python-0.13.1-cp37-abi3-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 38589661316b9f698a427531cb32dd16eb91dfa1a79326c82ecc75a75d73711f
MD5 7f8fd5baea49d58902cbfcb6f657e0ac
BLAKE2b-256 24da71ea41d14a4f14ed8779105686978f253403664f56293533d3b7bd69b20d

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