vega / vl-convert

Utilities for converting Vega-Lite specs from the command line and Python
BSD 3-Clause "New" or "Revised" License
100 stars 12 forks source link

Using vlconvert as a renderer? #84

Closed joelostblom closed 1 year ago

joelostblom commented 1 year ago

I am currently trying to produce pdf latex output via jupyter book and running into multiple issues with getting Altair charts to show up since there is no javascript front end. With altair saver I could use some like alt.renderers.enable('altair_saver', fmts=['png'], embed_options={'scaleFactor': '4'}) (from https://github.com/altair-viz/altair_saver/issues/75#issuecomment-1031765087), would it be possible to use vlconvert in a similar way?

I naively tried this which didn't work:

alt.renderers.register('vlc_png', vlc.vegalite_to_png)
alt.renderers.enable('vlc_png')

source = pd.DataFrame({
    'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
    'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})
alt.Chart(source).mark_bar().encode(
    x='a',
    y='b'
)
/home/joel/miniconda3/envs/alt5/lib/python3.11/site-packages/IPython/core/formatters.py:367: FormatterWarning: text/plain formatter returned invalid type <class 'bytes'> (expected <class 'dict'>) for object: alt.Chart(...)
  warnings.warn(
jonmmease commented 1 year ago

I think you can do this by enabling the regular Altair "png" renderer:

import altair as alt
alt.renderers.enable("png", scale_factor=2)

(at least this is currently working for me locally on altair master)

joelostblom commented 1 year ago

Ah yes of course, I mixed up the png renderer with the mimebundle renderer and didn't realize it actually uses vl-convert under the hood, thank you!