question about pyimport, numpy, and implementation details
See original GitHub issueThanks for creating this package. It looks really neat.
I was particularly intrigued by the pyimport
function you mentioned in the README. Having worked with some other javascript to python translators, I was surprised to see this work. Not only that it even works on something like the os
module! I guess what you are doing is not translating targets of pyimport
into javascript but marking them as untranslated python when you translate javascript to python.
Is that right?
That brings me to the question of numpy
. When I try and use pyimport
with numpy
in the following snippet
import js2py
js2py.eval_js('pyimport numpy;\nconsole.log(numpy.mean([1,2,3,4]));')
it fails and I get an exception.
Digging into the exception a bit suggests that numpy.mean
is trying to look at the type of its input and dispatch on that. But since the type of [1, 2, 3, 4]
is a javascript object, numpy gets confused and dies. Various other attempts (including using EvalJs
to put numpy.array and list into javascript) failed.
Any tips on how one could use js2py
with numpy
, pandas
, etc.?
Thanks.
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (5 by maintainers)
Top GitHub Comments
Hi, thanks, the pyimport is actually quite easy as the Js gets translated to Py and the python modules do not need translation at all, the translated code simply calls python functions directly (with some simple wrapping of arguments and the return value). This simple wrapping is exactly the reason why your np.mean function does not work. As you can see in the conversion table in readme, the Js types are converted to Py types as follows:
Hence the value received by the numpy.mean is actually a JsObjectWrapper, the reason why this is done this way can become clearer after checking the readme file. I have just added a hacky opt-in config to convert js arrays and objects to python lists and dicts implicitly:
Nice, but no datatables or stats 😃
I see what you mean about executing JS by Python. I’m probably thinking in reverse of what makes the most sense. It would probably be better to create an API to the python libraries I want in Python and then call them from node via https://github.com/extrabacon/python-shell or something like it.