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

embed_options in mimebundle is NoneType (error when calling chart.save in Altair to a PDF) #159

Closed jowens closed 6 months ago

jowens commented 6 months ago

Having trouble interpreting the error message when calling chart.save to a PDF.

    chart.save(os.path.join(outputdir, title + ".pdf"), engine="vl-convert")
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/altair/vegalite/v5/api.py", line 1214, in save
    save(**kwds)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/altair/utils/save.py", line 200, in save
    perform_save()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/altair/utils/save.py", line 162, in perform_save
    mimebundle = spec_to_mimebundle(
                 ^^^^^^^^^^^^^^^^^^^
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/altair/utils/mimebundle.py", line 77, in spec_to_mimebundle
    embed_options = preprocess_embed_options(embed_options)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/altair/utils/mimebundle.py", line 321, in preprocess_embed_options
    embed_options = embed_options.copy()
                    ^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'copy'

I have import vl_convert as vlc (although no other references to vlc in my code) and I've done pip install vl-convert-python but there's nothing in that trace that indicates it's dropped out of Altair into vl_convert. The Altair instructions are dead simple (https://altair-viz.github.io/user_guide/saving_charts.html) so I've clearly missed something.

Keeping you busy today!

jonmmease commented 6 months ago

Hey @jowens, you don't have to import vl_convert for this to work. This error is in Altair before it gets to invoking vl-convert. Can you open an issue in the altair repo (https://github.com/altair-viz/altair) with a repro and the exact version of altair you're using?

I can't reproduce this with a simple test, and looking at the code in https://github.com/altair-viz/altair/blob/v5.2.0/altair/utils/mimebundle.py, it doesn't look like embed_options should ever be None when passed into preprocess_embed_options.

One quick thing to look at is, what's the result of?

import altair as alt
alt.renderers.options.get("embed_options", {})

Could you include this result in the Altair issue?

jowens commented 6 months ago

Hate to make your life harder, really I do.

print(alt.renderers.options.get("embed_options", {}))
vf.enable()
print(alt.renderers.options.get("embed_options", {}))

prints

{}
None
jowens commented 6 months ago

Oh wait that's vegafusion. That's not you.

jowens commented 6 months ago

I've disabled vf and everything works as expected.

jonmmease commented 6 months ago

Oh, that's really helpful! I'll open a VegaFusion issue to avoid setting this to None.

See https://github.com/vega/vegafusion/issues/474