Altair saver ValueError: Unsupported format: 'png'

Viewed 5507

I keep getting an error using Altair saver when I am trying to save a chart to a PNG in Jupyter Notebook. ValueError: Unsupported format: 'png'

I know from here that I need to set the renderer enable and from here that there is a typo in the README, so I have that correct in the 5th line of code.

Running the following:
Windows 10
conda 4.8.2
Python 3.8.3
altair 4.1.0 py_1 conda-forge
altair_saver 0.1.0 py_0 conda-forge
vega 3.4.0 py38h32f6830_0 conda-forge
selenium 3.141.0 py38h9de7a3e_1001 conda-forge

import pandas as pd
import altair as alt
from altair_saver import save
alt.renderers.enable('default'); # if in jupyter, ; to suppress output
alt.renderers.enable('altair_saver', fmts=['vega-lite', 'png']);

mytaskbars = pd.DataFrame([
    {"task": "Task1a", "start": '2020-06-01', "end": '2020-09-30', "color": 'royalblue'},
    {"task": "Task1b", "start": '2020-06-01', "end": '2021-03-31', "color": 'deepskyblue'},
    {"task": "Task2", "start": '2020-06-01', "end": '2021-03-31', "color": 'red'},
    ])
    
mytaskbars["start"] = pd.to_datetime(mytaskbars["start"])
mytaskbars["end"] = pd.to_datetime(mytaskbars["end"])
    
    
chart = alt.Chart(mytaskbars).mark_bar(opacity=0.7).encode(
    x=alt.X('start', axis=alt.Axis(title='Date', labelAngle=-45, format = ("%b %Y"))),
     x2 = 'end',
    y=alt.Y('task', axis=alt.Axis(title=None)),
    color = alt.Color('color:N', scale = None)
    )
    
save(chart, "chart_202006.png")
chart

I also tried chart.save('chart_202006.png') as indicated here, same error. I think the error is somehow associated with the renderers line, even though the error is thrown later.

Here is the full traceback, as requested in the comments:

ValueError                                Traceback (most recent call last)
<ipython-input-3-13a284c2aca9> in <module>
     19     )
     20 
---> 21 save(chart, "chart_202006.png")
     22 chart

~\anaconda3\envs\geospat_env\lib\site-packages\altair_saver\_core.py in save(chart, fp, fmt, mode, method, **kwargs)
     60     """
     61     if method is None:
---> 62         Saver = _get_saver_for_format(fmt=fmt, fp=fp)
     63     elif isinstance(method, type):
     64         Saver = method

~\anaconda3\envs\geospat_env\lib\site-packages\altair_saver\_core.py in _get_saver_for_format(fmt, fp)
     28         if fmt in s.valid_formats and s.enabled():
     29             return s
---> 30     raise ValueError(f"Unsupported format: {fmt!r}")
     31 
     32 

ValueError: Unsupported format: 'png'
2 Answers

Anyone looking for a quick copy-paste solution:

$ pip install  altair_saver
$ npm install vega-lite vega-cli canvas

To save charts to PNG, you need more than just the altair_saver package. You also need either selenium or node dependencies, as outlined in altair_saver's installation section.

Follow those instructions and install the required dependencies, and it will resolve your error.

Related