Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Unable to save to png format under Windows 10 using node #95

Closed
amsimms opened this issue Mar 29, 2021 · 7 comments
Closed

Unable to save to png format under Windows 10 using node #95

amsimms opened this issue Mar 29, 2021 · 7 comments
Labels
bug Something isn't working

Comments

@amsimms
Copy link

amsimms commented Mar 29, 2021

Despite many attempts, I cannot seem to save charts in "png" format (really any format, but that's what I want) under Anaconda w/Windows 10. I have no issue on a Mac.

The last part of the error is always:

JSONDecodeError: Expecting value: line 2 column 1 (char 2)

A png file is created, but it appears to contain a command rather than its output.

# Test Case
import altair as alt
from altair_saver import save as alt_save
import numpy as np
import pandas as pd
# make simple data frame
test_data = pd.DataFrame.from_dict({ 'x': np.arange(0,10), 'y':np.arange(0,10)})
# make a chart with Altair
test_data_chart = alt.Chart(test_data).mark_line().encode(x=alt.X('x'),y=alt.Y('y'))
# display it (renders fine in notebook, using Chrome)
test_data_chart

# Test Case 1:
test_data_chart.save('testoutput.png')

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-29-2049dff4a23c> in <module>
      1 # Test Case 1:
----> 2 test_data_chart.save('testoutput.png')

C:\ProgramData\Anaconda3\envs\myenvname\lib\site-packages\altair\vegalite\v4\api.py in save(self, fp, format, override_data_transformer, scale_factor, vegalite_version, vega_version, vegaembed_version, **kwargs)
    474         if override_data_transformer:
    475             with data_transformers.disable_max_rows():
--> 476                 result = save(**kwds)
    477         else:
    478             result = save(**kwds)

C:\ProgramData\Anaconda3\envs\myenvname\lib\site-packages\altair\utils\save.py in save(chart, fp, vega_version, vegaembed_version, format, mode, vegalite_version, embed_options, json_kwds, webdriver, scale_factor, **kwargs)
    119             webdriver=webdriver,
    120             scale_factor=scale_factor,
--> 121             **kwargs,
    122         )
    123         if format == "png":

C:\ProgramData\Anaconda3\myenvname\lib\site-packages\altair\utils\mimebundle.py in spec_to_mimebundle(spec, format, mode, vega_version, vegaembed_version, vegalite_version, **kwargs)
     58                 "see http://github.com/altair-viz/altair_saver/".format(fmt=format)
     59             )
---> 60         return altair_saver.render(spec, format, mode=mode, **kwargs)
     61     if format == "html":
     62         html = spec_to_html(

C:\ProgramData\Anaconda3\myenvname\lib\site-packages\altair_saver\_core.py in render(chart, fmts, mode, embed_options, method, **kwargs)
    255         Saver = _select_saver(method, mode=mode, fmt=fmt)
    256         saver = Saver(spec, mode=mode, embed_options=embed_options, **kwargs)
--> 257         mimebundle.update(saver.mimebundle(fmt))
    258 
    259     return mimebundle

C:\ProgramData\Anaconda3\myenvname\lib\site-packages\altair_saver\savers\_saver.py in mimebundle(self, fmts)
     88                 vegalite_version=self._package_versions["vega-lite"],
     89             )
---> 90             bundle[mimetype] = self._serialize(fmt, "mimebundle")
     91         return bundle
     92 

C:\ProgramData\Anaconda3\myenvname\lib\site-packages\altair_saver\savers\_node.py in _serialize(self, fmt, content_type)
    112 
    113         if self._mode == "vega-lite":
--> 114             spec = self._vl2vg(spec)
    115 
    116         if fmt == "vega":

C:\ProgramData\Anaconda3\myenvname\lib\site-packages\altair_saver\savers\_node.py in _vl2vg(self, spec)
     66             [vl2vg], input=vl_json, stderr_filter=self._stderr_filter
     67         )
---> 68         return json.loads(vg_json)
     69 
     70     def _vg2png(self, spec: JSONDict) -> bytes:

C:\ProgramData\Anaconda3\myenvname\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

C:\ProgramData\Anaconda3\envs\myenvname\lib\json\decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

C:\ProgramData\Anaconda3\envs\myenvname\lib\json\decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 2 column 1 (char 2)

If I try specifying mode="vega", it succeeds and produces a file, but it just contains a command not an PNG file:

# Try vega
test_data_chart.save('testoutput.png',mode='vega')

# testout.png contains this text (not a png file):
C:\>node C:/ProgramData/Anaconda3/envs/myenvname\Library\share\vega-cli\node_modules\vega-cli\bin\vg2png ""

FWIW: Python 3.7.10 | packaged by conda-forge | (default, Feb 19 2021, 15:37:01) [MSC v.1916 64 bit (AMD64)] on win32

@jakevdp
Copy link
Member

jakevdp commented Mar 29, 2021

This package has not been tested with Windows (I don't have access to any windows machine on which to test). Any help would be appreciated!

@jakevdp jakevdp added the bug Something isn't working label Mar 29, 2021
@amsimms
Copy link
Author

amsimms commented Mar 29, 2021

Happy to help if I can.

@TravisNeils
Copy link

I was able to solve a very similar issue by installing the required packages globally
npm install -g vega-cli vega-lite canvas
which you have probably already done

When I installed vega itself my issues went away
npm install -g vega

@jasonrwang
Copy link

I was able to solve a very similar issue by installing the required packages globally
npm install -g vega-cli vega-lite canvas
which you have probably already done

When I installed vega itself my issues went away
npm install -g vega

I played around with a few different configurations, but indeed, it seems that installing these four packages globally (in addition to vega-lite-cli and vega-cli existing in conda) is the only one that works. Thanks @TravisNeils.

@mkoohafkan
Copy link

@TravisNeils solution worked for me on Win10 as well. Is this caused by a dependency not being installed correctly on Windows?

@gregorhd
Copy link

gregorhd commented Jul 27, 2021

I'm having the same issue saving Altair charts to any format from within VSCode-Python (VSCode v1.58.2) on Windows 10, neither running a Python script nor a Jupyter-Notebook opened in VSCode. The error message is the same.

JSONDecodeError: Expecting value: line 2 column 1 (char 2)

I'm not familiar with node, I'm afraid. Could you provide pointers on how to save Altair charts using node? I understand the npm install commands should be run from a node.js command prompt. How do I proceed from there to save my charts?

@joelostblom
Copy link
Member

Since Altair 5.2, the functionality of Altair Saver is now available in Altair via the vl-convert package. Most of the functionality has been available since 5.0, and the main addition in 5.2 was PDF export. See the docs on how to save charts for more details

We are going to archive this repo, so I'm closing all the open issues and PRs before doing so. Try out the new options for saving charts mentioned above and if you run into issues, please open an issue directly in the altair or vl-convert repo.

@joelostblom joelostblom closed this as not planned Won't fix, can't repro, duplicate, stale Mar 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants