Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NameError: name '_node' is not defined in altair/api.py:182 #320

Closed
DSLituiev opened this issue Apr 24, 2017 · 22 comments
Closed

NameError: name '_node' is not defined in altair/api.py:182 #320

DSLituiev opened this issue Apr 24, 2017 · 22 comments
Labels
Milestone

Comments

@DSLituiev
Copy link

DSLituiev commented Apr 24, 2017

I am trying to save a png from altair with chart.savechart(filetype="eps", outfile="myfigure"). I have installed canvas with npm install canvas (I do not have conda). This command results in:

NameError                                 Traceback (most recent call last)
<ipython-input-9-7cc84e0f0694> in <module>()
----> 1 chart.savechart(filetype="eps", outfile="occupancy_vs_epoch")

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/altair/api.py in savechart(self, outfile, filetype)
    180                     f.write(self.to_html())
    181         else:
--> 182             supported = _node.SUPPORTED_FILETYPES + ['json', 'html']
    183             raise ValueError('Cannot save chart of type {0}; supported'
    184                              'extensions are {1}'.format(filetype, supported))

NameError: name '_node' is not defined

It seems _node is not mentioned anywhere else in this file. There is node variable though.

Version: '1.2.0'

@DSLituiev
Copy link
Author

DSLituiev commented Apr 24, 2017

Update: if I replace _node to node in the api.py code and use chart.savechart(filetype="png", outfile="myfigure") instead, I get following:

TypeError                                 Traceback (most recent call last)
<ipython-input-11-e38621ebd49d> in <module>()
----> 1 chart.savechart(filetype="png", outfile="occupancy_vs_epoch")

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/altair/api.py in savechart(self, outfile, filetype)
    166 
    167         if filetype in node.SUPPORTED_FILETYPES:
--> 168             node.savechart(self, outfile, filetype)
    169         elif filetype == 'json':
    170             if hasattr(outfile, 'write'):

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/altair/utils/node.py in savechart(chart, filename, filetype, verbose)
    133 
    134     with ensure_npm_bin_in_path(verbose=verbose):
--> 135         _convert_vegalite_spec(chart.to_dict(),
    136                                'vl2' + filetype,
    137                                outfile=filename,

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/altair/schema/baseobject.py in to_dict(self, data)
    176         """
    177         from ..utils.visitors import ToDict
--> 178         self._finalize()
    179         return ToDict().visit(self, data)
    180 

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/altair/api.py in _finalize(self, **kwargs)
    540         if self.data is not None:
    541             kwargs['data'] = self.data
--> 542         super(LayeredChart, self)._finalize(**kwargs)
    543 
    544     def __iadd__(self, layer):

TypeError: super(type, obj): obj must be an instance or subtype of type

@DSLituiev
Copy link
Author

If I replace a bunch of other similar patterns to super(type(self), self)._finalize(**kwargs), I am getting: vl2png. Again, the canvas is installed.

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 24, 2017

Thanks! fixed here: #321

It doesn't fix your issue, though, it just masks the error saying that "eps" is not a valid file extension.

@DSLituiev
Copy link
Author

DSLituiev commented Apr 24, 2017

The #321 does not seem to fix the issue with super. As a background, I am running a Jupyter notebook with:

%load_ext autoreload
%autoreload 2

This might be related to the issue discussed here:
http://stackoverflow.com/questions/9722343/python-super-behavior-not-dependable

More stable way is to use self.__class__ according to the answers. Or type(self).

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 24, 2017

Yes, it looks like autoreload is causing issues with the object inheritance. I'd try without that.

IMHO this is not an issue with altair's use of super (which is idiomatic), but a bug in autoreload.

@DSLituiev
Copy link
Author

DSLituiev commented Apr 24, 2017

hmm. I really need it. It did not break any other reasonable python package I am using. So I guess, fixing to self.__class__ might be not a bad idea.

In any case, I am still getting following. How can I make sure altair finds canvas?

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-12-e38621ebd49d> in <module>()
----> 1 chart.savechart(filetype="png", outfile="occupancy_vs_epoch")

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/altair/api.py in savechart(self, outfile, filetype)
    166 
    167         if filetype in node.SUPPORTED_FILETYPES:
--> 168             node.savechart(self, outfile, filetype)
    169         elif filetype == 'json':
    170             if hasattr(outfile, 'write'):

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/altair/utils/node.py in savechart(chart, filename, filetype, verbose)
    136                                'vl2' + filetype,
    137                                outfile=filename,
--> 138                                verbose=verbose)

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/altair/utils/node.py in _convert_vegalite_spec(spec, cmd, outfile, verbose)
     73     if verbose:
     74         print('> ' + ' '.join(cmd))
---> 75     p = Popen([cmd], stdout=PIPE, stdin=PIPE, stderr=PIPE)
     76 
     77     input_ = '{0}'.format(json.dumps(spec))

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds)
    948                                 c2pread, c2pwrite,
    949                                 errread, errwrite,
--> 950                                 restore_signals, start_new_session)
    951         except:
    952             # Cleanup if the child failed starting.

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
   1542                             else:
   1543                                 err_msg += ': ' + repr(orig_executable)
-> 1544                     raise child_exception_type(errno_num, err_msg)
   1545                 raise child_exception_type(err_msg)
   1546 

FileNotFoundError: [Errno 2] No such file or directory: 'vl2png'

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 24, 2017

The node integration is currently quite experimental; you can read the documentation here: https://altair-viz.github.io/documentation/displaying.html#saving-figures-as-png-and-svg

You'll need to npm install the correct tools and set up your system paths such that running vl2png on the command line works without an error. Once that's set up correctly, the Altair script that calls this command should work.

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 24, 2017

Regarding super(): there are very good reasons to use this over the (IMHO incorrect) self.__class__ route (e.g. https://rhettinger.wordpress.com/2011/05/26/super-considered-super/). It's unfortunate that autoreload has this bug; if it is important to you to use autoreload with packages that use super(), I'd suggest raising this issue in the context of that project.

@DSLituiev
Copy link
Author

this is very unfortunate that PNG is not supported.
And does not makes altair helpful in writing scientific papers ;((, seems i got caught by the hype too early.
I work in python3 with no conda.

HTML that I save also produces no output: a blank page

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 24, 2017

this is very unfortunate that PNG is not supported.

PNG is supported if you correctly set up the vl2png tool, as I mentioned above. But Altair has no control over your system npm setup at the moment. If you know of a better way to call npm packages from Python, please let us know!

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 24, 2017

Regarding the blank page for HTML: I think you're running into a Vega-Lite version clash. The vega-lite team recently changed the URLs of the JS libraries on their website, and we have not had the chance to release a new Altair version with the updated URLs.

We're getting to that – please be patient – in the meantime you can update the URL manually in the HTML file, and it should work.

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 24, 2017

These are the lines that will need to be updated with the correct URLs: https://github.com/altair-viz/altair/blob/435770277bc20077464b24fd77cdc7a91a98ad25/altair/utils/html.py#L39

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 24, 2017

The updated URLs are

  • https://vega.github.io/vega/releases/v2.6.5/vega.js
  • https://vega.github.io/vega-lite-1/vega-lite.js

Hopefully the Vega/Vega-Lite projects will soon stabilize enough that these sorts of silent changes will not happen in the future.

@DSLituiev
Copy link
Author

that helped! thank you!

As to the node, running node bin returns $(pwd)/.bin dependent on the directory I run it in, so it is still a puzzle to me.

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 24, 2017

Yeah, the node stuff is still a bit of a mystery to me to be honest. The current solution is one that worked on my mac and on TravisCI when I first added it, but I don't think it's totally robust.

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 24, 2017

I just added a PR that makes the urls more configurable in html output: #323

@DSLituiev
Copy link
Author

to get global bin, use npm bin -g. There is also npm root -g. Anyway, I cannot find vl2png in there.

@jakevdp
Copy link
Collaborator

jakevdp commented Apr 24, 2017

Did you install the vega-lite npm package? vl2png comes with it (or at least it previously did).

@DSLituiev
Copy link
Author

DSLituiev commented Apr 24, 2017

Oh, that helps! I got impression from the documentation that I have to look for vl2png in canvas.

Now I am getting a bug (.decode() missing):

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/altair/utils/node.py in _convert_vegalite_spec(spec, cmd, outfile, verbose)
     81     out, err = p.communicate(input=input_)
     82     if p.returncode:
---> 83         combined_output = out+ '\nError:\n' + err
     84         raise CalledProcessError(p.returncode, cmd, output=combined_output)
     85 

TypeError: can't concat bytes to str

After fixing it:

/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/altair/utils/node.py in _convert_vegalite_spec(spec, cmd, outfile, verbose)
     82     if p.returncode:
     83         combined_output = out.decode() + '\nError:\n' + err.decode()
---> 84         raise CalledProcessError(p.returncode, cmd, output=combined_output)
     85 
     86     if outfile is not None:

CalledProcessError: Command 'vl2png' returned non-zero exit status 1

@DSLituiev
Copy link
Author

For some reason CalledProcessError(output=...) is not displayed. Here is the output:

combined_output 
Error:
/usr/local/lib/node_modules/vega/build/vega.js:776
  throw Error(message);
  ^

Error: Missing required "filter" parameter: "expr"
    at Error (native)
    at error (/usr/local/lib/node_modules/vega/build/vega.js:776:9)
    at parseParameter$1 (/usr/local/lib/node_modules/vega/build/vega.js:29370:9)
    at parseParameters (/usr/local/lib/node_modules/vega/build/vega.js:29349:25)
    at parseTransform (/usr/local/lib/node_modules/vega/build/vega.js:29337:35)
    at /usr/local/lib/node_modules/vega/build/vega.js:30295:23
    at Array.forEach (native)
    at parseData$1 (/usr/local/lib/node_modules/vega/build/vega.js:30294:20)
    at /usr/local/lib/node_modules/vega/build/vega.js:30701:5
    at Array.forEach (native)

@DSLituiev
Copy link
Author

the error due to filter happens independent of whether I use transform_data or not

@ellisonbg ellisonbg added this to the Future milestone Sep 27, 2017
@jakevdp
Copy link
Collaborator

jakevdp commented Mar 27, 2018

node is no longer supported in version 2.

@jakevdp jakevdp closed this as completed Mar 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants