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 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


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

Uploaded Source

Built Distributions

vl_convert_python-1.8.0-cp37-abi3-win_amd64.whl (31.3 MB view details)

Uploaded CPython 3.7+Windows x86-64

vl_convert_python-1.8.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (33.0 MB view details)

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

vl_convert_python-1.8.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (31.7 MB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ARM64

vl_convert_python-1.8.0-cp37-abi3-macosx_11_0_arm64.whl (28.8 MB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

vl_convert_python-1.8.0-cp37-abi3-macosx_10_12_x86_64.whl (30.0 MB view details)

Uploaded CPython 3.7+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for vl_convert_python-1.8.0.tar.gz
Algorithm Hash digest
SHA256 ceca613ca5551c55270a15ca48d0f3a7de1e949e0f127310e9b0f6570ea3fbbb
MD5 352968e7fa55f0d8c1238d00a98755e0
BLAKE2b-256 150806945bff9655c5b0520a8d1b2550cd8007e106ebec45a33840035420e0d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for vl_convert_python-1.8.0-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 9f1146b791ed27916f54c45e1d66af53a40eb26e5aaea1892f33eb9a935039ab
MD5 a89583ce59785b32e0eff76468cda517
BLAKE2b-256 f86f29dce05f9167e3a01ab74d79eeadd531bc24cf59e3a7fc3736af476ca431

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for vl_convert_python-1.8.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b51264998e8fcc43dbce801484a950cfe6513cdc4c46b20604ef50989855a617
MD5 95898241629eecf2158d7728a325f49b
BLAKE2b-256 a76b48f6d47a92eaf6f0dd235146307a7eb0d179b78d2faebc53aca3f1e49177

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for vl_convert_python-1.8.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3388e3913287867b3553c10f81ca2d85268216a5a75e7c71b9c1b59887c1977e
MD5 a60dce8e32ed10172a7f718a4d0b637f
BLAKE2b-256 09fa1dd944c9e9898e59e31c385bdce215aca543acc555de20b8bf4dc60ddb89

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for vl_convert_python-1.8.0-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 81f6380019ceadf070a79f85aa624475a6568093f70de0e151a32e91ecbcaacf
MD5 ba8fb236c7919662e0d73a0dcf71c335
BLAKE2b-256 42e2325e6b5895482b2534e7462c012f237c66ffb02fb3af45eec0accab2f8d4

See more details on using hashes here.

File details

Details for the file vl_convert_python-1.8.0-cp37-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for vl_convert_python-1.8.0-cp37-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f663317fc280b07553534195c1e31c4ca882d9c8601430211b078196db5ed227
MD5 1b63ee5c03bcd73e70ac8a4b83ad3ca6
BLAKE2b-256 225a9dca7d8ff56e82c298e9ef381cfc803e262b85b7c59f2515d0e9f81a75b6

See more details on using hashes here.

Supported by

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