Package: vl-convert-python
Overviewvl-convert-pythonis a dependency-free Python package for convertingVega-Litechart specifications into static images (SVG or PNG) orVegachart 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!Installationvl-convert-pythoncan be installed using pip with$ pip install vl-convert-pythonUsageThevl-convert-pythonpackage provides a series of conversion functions under thevl_convertmodule.Convert Vega-Lite to SVG, PNG, and VegaThevegalite_to_svgandvegalite_to_pngfunctions can be used to convert Vega-Lite specifications to static SVG and PNG images respectively. Thevegalite_to_vegafunction can be used to convert a Vega-Lite specification to a Vega specification.importvl_convertasvlcimportjsonvl_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 filesvg_str=vlc.vegalite_to_svg(vl_spec=vl_spec)withopen("chart.svg","wt")asf:f.write(svg_str)# Create PNG image data and then write to a filepng_data=vlc.vegalite_to_png(vl_spec=vl_spec,scale=2)withopen("chart.png","wb")asf:f.write(png_data)# Create low-level Vega representation of chart and write to filevg_spec=vlc.vegalite_to_vega(vl_spec)withopen("chart.vg.json","wt")asf:json.dump(vg_spec,f)Convert Altair Chart to SVG, PNG, and VegaThe Altair visualization library provides a Pythonic API for generating Vega-Lite visualizations. As such,vl-convert-pythoncan be used to convert Altair charts to PNG, SVG, or Vega. Thevegalite_*functions support an optionalvl_versionargument 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.importaltairasaltfromvega_datasetsimportdataimportvl_convertasvlcimportjsonsource=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 filesvg_str=vlc.vegalite_to_svg(chart.to_json(),vl_version="4.17")withopen("altair_chart.svg","wt")asf:f.write(svg_str)# Create PNG image data and then write to a filepng_data=vlc.vegalite_to_png(chart.to_json(),vl_version="4.17",scale=2)withopen("altair_chart.png","wb")asf:f.write(png_data)# Create low-level Vega representation of chart and write to filevg_spec=vlc.vegalite_to_vega(chart.to_json(),vl_version="4.17")withopen("altair_chart.vg.json","wt")asf:json.dump(vg_spec,f)How it worksThis crate usesPyO3to wrap thevl-convert-rsRust crate as a Python library. Thevl-convert-rscrate is a self-contained Rust library for convertingVega-Litevisualization specifications into various formats.  The conversions are performed using the Vega-Lite and Vega JavaScript libraries running in a v8 JavaScript runtime provided by thedeno_runtimecrate.  Font metrics and SVG-to-PNG conversions are provided by theresvgcrate.Of note,vl-convert-pythonis fully self-contained and has no dependency on an external web browser or Node.js runtime.Development setupCreate development conda environment$ conda create -n vl-convert-dev -c conda-forge python=3.10 deno maturin altair pytest black black-jupyter scikit-imageActivate environment and pip install remaining dependencies$ conda activate vl-convert-dev
$ pip install pypdfium2Change to Python package directory$ cd vl-convert-pythonBuild Rust python package with maturin in develop mode$ maturin develop --releaseRun tests$ pytest tests