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

Not able to save anything #114

Closed
aborruso opened this issue Dec 12, 2022 · 6 comments
Closed

Not able to save anything #114

aborruso opened this issue Dec 12, 2022 · 6 comments

Comments

@aborruso
Copy link

Thank you for the development of this!

I have this sample code:

import pandas as pd
import altair as alt
from altair_saver import save

df = pd.read_csv("tmp.csv",keep_default_na=False)

df['year'] = pd.to_datetime(df['year'], format='%Y')

chart=alt.Chart(df).mark_area().encode(
    alt.X('year:T', timeUnit = 'year',title='year',axis=alt.Axis(tickCount='year')),
    alt.Y('v:Q',axis=alt.Axis(format='%'),title='percentage'),
    color='i:N'
)

save(chart, "chart.png")

If I run it, I have

Traceback (most recent call last):
  File "/mnt/c/Users/aborr/Documents/GitHub/f-dss_report/blackboard/p.py", line 15, in <module>
    save(chart, "chart.png")
  File "/home/aborruso/.local/lib/python3.9/site-packages/altair_saver/_core.py", line 169, in save
    Saver = _select_saver(method, mode=mode, fmt=fmt, fp=fp)
  File "/home/aborruso/.local/lib/python3.9/site-packages/altair_saver/_core.py", line 67, in _select_saver
    if s.enabled() and fmt in s.valid_formats[mode]:
  File "/home/aborruso/.local/lib/python3.9/site-packages/altair_saver/savers/_node.py", line 103, in enabled
    return bool(exec_path("vl2vg") and exec_path("vg2png"))
  File "/home/aborruso/.local/lib/python3.9/site-packages/altair_saver/savers/_node.py", line 29, in exec_path
    for path in [None, npm_bin(global_=True), npm_bin(global_=False)]:
  File "/home/aborruso/.local/lib/python3.9/site-packages/altair_saver/savers/_node.py", line 24, in npm_bin
    return check_output_with_stderr(cmd).decode().strip()
  File "/home/aborruso/.local/lib/python3.9/site-packages/altair_saver/_utils.py", line 198, in check_output_with_stderr
    ps = subprocess.run(
  File "/usr/lib/python3.9/subprocess.py", line 528, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/home/aborruso/.nvm/versions/node/v16.18.1/bin/npm', 'bin', '--global']' returned non-zero exit status 1.

I have no idea on how to solve it

@aborruso
Copy link
Author

Running this notebook I have the below error.

@jakevdp do you have some tips on how to solve it?

CalledProcessError                        Traceback (most recent call last)
Cell In [5], line 3
      1 from altair_saver import save
      2 for fmt in ['json', 'vg.json', 'html', 'png', 'svg', 'pdf']:
----> 3   save(chart, f'chart.{fmt}')
      4 get_ipython().system('ls -lh chart.*')

File ~/.local/lib/python3.9/site-packages/altair_saver/_core.py:169, in save(chart, fp, fmt, mode, embed_options, method, suppress_data_warning, **kwargs)
    166 if embed_options is None:
    167     embed_options = alt.renderers.options.get("embed_options", None)
--> 169 Saver = _select_saver(method, mode=mode, fmt=fmt, fp=fp)
    170 saver = Saver(spec, mode=mode, embed_options=embed_options, **kwargs)
    172 return saver.save(fp=fp, fmt=fmt)

File ~/.local/lib/python3.9/site-packages/altair_saver/_core.py:67, in _select_saver(method, mode, fmt, fp)
     65     fmt = extract_format(fp)
     66 for s in _SAVER_METHODS.values():
---> 67     if s.enabled() and fmt in s.valid_formats[mode]:
     68         return s
     69 raise ValueError(f"No enabled saver found that supports format={fmt!r}")

File ~/.local/lib/python3.9/site-packages/altair_saver/savers/_node.py:103, in NodeSaver.enabled(cls)
    100 @classmethod
    101 def enabled(cls) -> bool:
    102     try:
--> 103         return bool(exec_path("vl2vg") and exec_path("vg2png"))
    104     except ExecutableNotFound:
    105         return False

File ~/.local/lib/python3.9/site-packages/altair_saver/savers/_node.py:29, in exec_path(name)
     27 @functools.lru_cache(16)
     28 def exec_path(name: str) -> str:
---> 29     for path in [None, npm_bin(global_=True), npm_bin(global_=False)]:
     30         exc = shutil.which(name, path=path)
     31         if exc:

File ~/.local/lib/python3.9/site-packages/altair_saver/savers/_node.py:24, in npm_bin(global_)
     22 if global_:
     23     cmd.append("--global")
---> 24 return check_output_with_stderr(cmd).decode().strip()

File ~/.local/lib/python3.9/site-packages/altair_saver/_utils.py:198, in check_output_with_stderr(cmd, shell, input, stderr_filter)
    176 """Run a command in a subprocess, printing stderr to sys.stderr.
    177 
    178 This function exists because normally, stderr from subprocess in the notebook
   (...)
    195 subprocess.CalledProcessError : if the called process returns a non-zero exit code.
    196 """
    197 try:
--> 198     ps = subprocess.run(
    199         cmd,
    200         shell=shell,
    201         input=input,
    202         check=True,
    203         stdout=subprocess.PIPE,
    204         stderr=subprocess.PIPE,
    205     )
    206 except subprocess.CalledProcessError as err:
    207     stderr = err.stderr

File /usr/lib/python3.9/subprocess.py:528, in run(input, capture_output, timeout, check, *popenargs, **kwargs)
    526     retcode = process.poll()
    527     if check and retcode:
--> 528         raise CalledProcessError(retcode, process.args,
    529                                  output=stdout, stderr=stderr)
    530 return CompletedProcess(process.args, retcode, stdout, stderr)

CalledProcessError: Command '['/home/aborruso/.nvm/versions/node/v16.18.1/bin/npm', 'bin', '--global']' returned non-zero exit status 1.

@joelostblom
Copy link
Member

Until Altair is updated to the next version, you can try the method suggested here https://stackoverflow.com/a/74602283/2166823

@aborruso
Copy link
Author

Until Altair is updated to the next version, you can try the method suggested here https://stackoverflow.com/a/74602283/2166823

I have this error

Traceback (most recent call last):
  File "/mnt/c/Users/aborr/Desktop/trash/pp.py", line 14, in <module>
    save_chart(my_chart, 'my-chart.png', 2)
NameError: name 'save_chart' is not defined

Thank you

@aborruso
Copy link
Author

I have installed selenium==4.2.0 , and in this way it works

import altair as alt
from vega_datasets import data

chart = alt.Chart(data.cars.url).mark_point().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color='Origin:N'
)

chart.save('chart.png')

@stefaneidelloth
Copy link

stefaneidelloth commented Feb 15, 2024

Due to restrictions with altair_viewer I use altair==4.2.2.
Installing selenium==4.2.0 did not solve the issue for me.
I have python version 3.11.6 and node version 20.9.0.

...npm.CMD', 'bin', '--global']' returned non-zero exit status 1.

Related: vega/altair#2866

@jonmmease
Copy link

For future readers, altair_saver has been replaced by vl-convert in Altair 5. See https://altair-viz.github.io/user_guide/saving_charts.html#png-svg-and-pdf-format.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants