How to modify a list of JS name functions after loading ?

19 views
Skip to first unread message

Christophe Perrod

unread,
Feb 3, 2023, 10:54:48 AM2/3/23
to brython
Hi,

I have a very specific problem...

I have a script that import a JS library ( numjs ) with the 'load()' function.

The JS brings a 'nj' objet in Python namespace so we can now reference the object's properties with 'window.property'.

For convenience, I obviously translates this name to a simple 'property' with the following assignment :

array = window.nj.array   # by example...

It works a neat...but the nj JS object owns 65 different properties...

Should I write 65 successive assignments, or do you think there is a more convenient way to proceed ? I tried naively to 'inject' the new names in the globals() dictionary but no success...

Many thanks in advance

Chris

Kiko

unread,
Feb 3, 2023, 2:50:13 PM2/3/23
to bry...@googlegroups.com
Hi,

You don't have too.

Have a look at this fiddle: https://jsfiddle.net/6uvLxhj2/2/
(and check the output in the console in the jsfiddle site (botton right).

Once you have done

> np = window.nj # this would be pretty similar to 'import numpy as np'

you have all njs names in np var.

OTOH, take care with this lib. It is not updated and functionality is
far from what you have using numpy.
If you want to do number crunching using JS have a look to
alternatives like arquero, data-forge,...
https://github.com/uwdata/arquero
https://github.com/data-forge/data-forge-ts
https://github.com/javascriptdata/danfojs
To me is unpleasant to do this kind of thing in JS world but arquero
was not a so bad experience.


> Many thanks in advance
>
> Chris
>
> --
> You received this message because you are subscribed to the Google Groups
> "brython" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to brython+u...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/brython/6e4ce5ee-321c-47c1-9f99-4ea2e5a35532n%40googlegroups.com.
>

Christophe Perrod

unread,
Feb 3, 2023, 3:55:37 PM2/3/23
to bry...@googlegroups.com
Thanks Kiko for your response.

But I forgot to put the thing in context : the goal of my script is to exec() Python scripts written by user in editors embedded in the page.
The first instruction would be of course the import statement, but I can't predict the alias the user would ( eventually ) choose, even 'np' would be of course the most serious candidate...

The translation or JS object's properties bypasses this problem, but a kind of solution like yours would be of course by far preferable.

Thanks you for your advice on other numpy-like librairies. My tests with numjs are only a proof of concept for now, but I'll give a look at arquero.

Regards

Chris

Christophe Perrod

unread,
Feb 4, 2023, 6:57:19 AM2/4/23
to brython
Hi,

Ok, I've done the trick. The key is to retrieve the JS functions names list, then mapping the function reference ( using getattr() ) with the name in Python global namespace :

<code>
nj = window.nj

prop = window.Object.getOwnPropertyNames(window.nj)

for p in prop:

     if p not in globals().keys():   # to avoid collision

             globals()[p] = getattr(nj, p)

</code>

Chris
Reply all
Reply to author
Forward
0 new messages