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

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

Uploaded Source

Built Distributions

vl_convert_python-0.7.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (22.2 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

vl_convert_python-0.7.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (21.2 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

vl_convert_python-0.7.0-cp311-cp311-macosx_11_0_arm64.whl (19.6 MB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

vl_convert_python-0.7.0-cp311-cp311-macosx_10_7_x86_64.whl (20.8 MB view details)

Uploaded CPython 3.11 macOS 10.7+ x86-64

vl_convert_python-0.7.0-cp310-none-win_amd64.whl (21.2 MB view details)

Uploaded CPython 3.10 Windows x86-64

vl_convert_python-0.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (22.2 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

vl_convert_python-0.7.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (21.2 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

vl_convert_python-0.7.0-cp310-cp310-macosx_11_0_arm64.whl (19.6 MB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

vl_convert_python-0.7.0-cp310-cp310-macosx_10_7_x86_64.whl (20.8 MB view details)

Uploaded CPython 3.10 macOS 10.7+ x86-64

vl_convert_python-0.7.0-cp39-none-win_amd64.whl (21.2 MB view details)

Uploaded CPython 3.9 Windows x86-64

vl_convert_python-0.7.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (22.2 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

vl_convert_python-0.7.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (21.2 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

vl_convert_python-0.7.0-cp39-cp39-macosx_11_0_arm64.whl (19.6 MB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

vl_convert_python-0.7.0-cp39-cp39-macosx_10_7_x86_64.whl (20.8 MB view details)

Uploaded CPython 3.9 macOS 10.7+ x86-64

vl_convert_python-0.7.0-cp38-none-win_amd64.whl (21.2 MB view details)

Uploaded CPython 3.8 Windows x86-64

vl_convert_python-0.7.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (22.2 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

vl_convert_python-0.7.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (21.2 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

vl_convert_python-0.7.0-cp38-cp38-macosx_11_0_arm64.whl (19.6 MB view details)

Uploaded CPython 3.8 macOS 11.0+ ARM64

vl_convert_python-0.7.0-cp38-cp38-macosx_10_7_x86_64.whl (20.8 MB view details)

Uploaded CPython 3.8 macOS 10.7+ x86-64

vl_convert_python-0.7.0-cp37-none-win_amd64.whl (21.2 MB view details)

Uploaded CPython 3.7 Windows x86-64

vl_convert_python-0.7.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (22.2 MB view details)

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

vl_convert_python-0.7.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (21.2 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

vl_convert_python-0.7.0-cp37-cp37m-macosx_11_0_arm64.whl (19.6 MB view details)

Uploaded CPython 3.7m macOS 11.0+ ARM64

vl_convert_python-0.7.0-cp37-cp37m-macosx_10_7_x86_64.whl (20.8 MB view details)

Uploaded CPython 3.7m macOS 10.7+ x86-64

File details

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

File metadata

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

File hashes

Hashes for vl_convert_python-0.7.0.tar.gz
Algorithm Hash digest
SHA256 66f91687317bb9c4b21c1485ab9f51bd122d07ee741e24718432e9cff1226516
MD5 f641e1a30a506055c631bff78448c24c
BLAKE2b-256 722b3d8fe0915a6435f5aca0cacf553c2dbeee2bee9b2b5ad00a00b29a975ec1

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 865941091e795d2922c83480db668f11240cc4ae04845d54c952c0db9a46b83f
MD5 14b58cf57efd8b9286a87e6c52ba46a7
BLAKE2b-256 e39453a15fc1536368f950a8e266e57f16c867b31375368c74733b4b61a8c16c

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0ea90fc30bbde50d23938c245b508b8efc62254b80cfa2441dbc252d241faa17
MD5 406511b725533aeb5be6183d7d988c31
BLAKE2b-256 bbf09ae3a27b59a5bc5768985f97bbc0a822f7a1fff70b2234b2ea3665c0ab36

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7e634bf7e550ca41e28540a77ca0de34afd012134858cc5f9680b1a93cf167f1
MD5 fdb8a345c1d2bb997126e792db8fa138
BLAKE2b-256 8d85e88cfba0b2ee8dbc4dddee8d44e5910a347250c85cdff838eb395ae221b4

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp311-cp311-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 9a1a17412628a5041cbb9205a5a572d32a7a22c8d84ef5fb516aad5b66848ef1
MD5 6b78bb3b13074a478637f7a8a685012f
BLAKE2b-256 752c5ee70f53fe1a063300f922752d4af9f24925322869a7d36cce1de22b3db5

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp310-none-win_amd64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 fae00749e430f085ef91c0134ab983f811adb4f152c43fc9a9e21232f3849733
MD5 1bb72163e2839b19f38006b3539becd7
BLAKE2b-256 816db680e909c36b54db180db55dd19817e0418ae6b2a853e1a81324eaca5020

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5e8fa5a9186be906e102cfbd0aa7d26eb706be4e9d51c56394f3f31e22076f0b
MD5 f2be27720c8431a5cce2ddfeb9adb34c
BLAKE2b-256 e722074c15d00fc9ae805586884385adc2c03d73a94b9e78bb92e512c4a97253

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b31ab411fd6ffd97f7c8f2d6f026fc6126e211a3b72bc563e6069f3a93b7dd6f
MD5 03658ca7273aef9d84aec57100d56c66
BLAKE2b-256 2276583e48ab3890887aa67b94e361ded00156b696ebfdcea5849e0668d810c6

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a97fc6270c7346c62faa406b0e8b2317e547132e3ad17d2b1bfd5c24c94eb108
MD5 e89c762db4cb1b2baeba22729fdc6cbe
BLAKE2b-256 8396d5058ea70e5607c229fbfc7c17695758226ccd2477f913bd7b3bedeaf04f

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp310-cp310-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 ec28362c2e60941bb12f329baf0b1dbb8c6e7505c3cb9f07c033d8dc75ce7bd5
MD5 48c724b4dc1b2c193d099da292d31f05
BLAKE2b-256 1709debbcaa197977140de01142007d83d4d6d461e736a692a1a777e4e697855

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp39-none-win_amd64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 5b6f90aa38b56be3abdfe18c1a02ac1c74de94300c035279fa6677b28b6e953e
MD5 d18438144e175c42a837fb3132d5a947
BLAKE2b-256 d8ecbba305f766da12189dcb6fdf430d86c650e12777080bc0f14877453a16f0

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d264e8d2577278b11c3b8cad5cfd271505840e2ed30d231b99aa4b5db6fa33b6
MD5 010f98cde466a0f36e379781b8c9a27e
BLAKE2b-256 2a2249b9d9a932aedb6c9ddefd1a736d453af78367467d232977c6c4393c9534

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3f49a0b353595d17c0648824bd932be0c0675891b7f3986aeda91e01891ac0c5
MD5 1d0d185d71d9dafc5a24fb440f3d5b97
BLAKE2b-256 cec3bfe5df6f16586764bf9e75ab2a85c8d6dbd9533395a82bd4acbf95823071

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d77c6550d4178f8cbb841ca22a1dfab85bc1487735122fc9f9130bc20072127c
MD5 f82d294aa021bfb6596bd478cd454af3
BLAKE2b-256 f327fcd63fe04959e21701b5b433b128ba4e667ebddcb1022428b6cb271b67d8

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp39-cp39-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 c076c4e3d81b7716f94de8131e6f4022b4a893ed7ac87c0c5cbcf47ec7575faa
MD5 33dcf7cd506ad0607d90983e03442afc
BLAKE2b-256 df37e0bd1cd4b5d2894be751944113b5325469694ca3d85c22242e1fc01b1af4

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp38-none-win_amd64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 abb5d76162a9116cae3712d0e63bbb17348b9cca0da8dbaddcf17174b4b69b51
MD5 e21b20f5e7ac7dc70efb4de10bd91076
BLAKE2b-256 7d570cfcb0c00cc47c0b5b615a6554a8d27236bf8d8b2d68baed48564d575a8b

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e7687e611b0f5603963a3f0b2ed836c12f2b839347e8c774de4641af39978d06
MD5 b60f8d442555f1489606cd3b4bf6b598
BLAKE2b-256 146c98012402a1c814c500d890ff448a62b81115d16b15ddb1fa7c5295460b12

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 b099901f274a5518a20932a9f3654ec3753238dc79bdd8647b7f892103dba528
MD5 128527fa17f5cc4d0f903ab629cc48db
BLAKE2b-256 100f8b825c8885583877d79dbee897b1535b01554b6b8a6fc3672ed601c46219

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 afb3571f946757f2746bfa167a5b619719a3eee458d702aacb68e202ab027307
MD5 93167eada1f9354eb84238c6f529dff7
BLAKE2b-256 55a45b37d9dc70cd83dc8620484e417b14177f2224e2ae5bd296986f2f63824f

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp38-cp38-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 4e172beeac63decd4505c1fc8ad13b0bc67109c8e3af25a0668e0684af517562
MD5 958c324a4788fa679412884bf043df18
BLAKE2b-256 66ee3769c02825c1f8ba293940159b156daa105ca59d548720d40c0186266120

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp37-none-win_amd64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 e2be6fd140027995631562507b4fd9f2252b3ca846c05d1679edf95384e84c7a
MD5 d619de51dc87272a29ab5816ff54ae86
BLAKE2b-256 306448a7c67aeca5b9486276b54148b5d94992b425f448c7243dc8a6bdeb701d

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 071db1a28dfae99742fe4e3a54099e26cc3f6197e508bd5fa58fcfd42fadb1b3
MD5 7b4978af427abd0eea9e0b7d7322007b
BLAKE2b-256 8b382ff29a529bcac9b462168bdfcecee1fcd8761de67fb05f5ea7e865b59f7e

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e489315d6af90d7c22f1054ed96b62abc0543a33a51326f2f65fe56495d45439
MD5 45e5aa3878e14dc2e71bbc5f22f0a5f3
BLAKE2b-256 083427e59df2b226a36e4d0fec8d1419d4362ab0fb7636333e11fbf9ab0fdf38

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp37-cp37m-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp37-cp37m-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bc6ac74ec35e46120108aaa642cd7a3a23962a3e3fe3acb9d34de37c4fe02128
MD5 1d181139aced6e7320e0eb0615b8c1fa
BLAKE2b-256 d6b8a468cd1183e3d02dc015c1cb60d2bd03ad553f9182bf93351f53c2bfd5eb

See more details on using hashes here.

File details

Details for the file vl_convert_python-0.7.0-cp37-cp37m-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for vl_convert_python-0.7.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 130096cd132b31f073d2316308721e1859bbbc765796f1fce63175c00e6c7c40
MD5 495c034bd8d1f9bf573a7b4d92d9fa3d
BLAKE2b-256 94ed7a3e7119708b67e826cc7f3433d9bf5ff9dab9f947e101abec2dc946f9e1

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