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

Skip to first unread message

Christophe Perrod

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

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 ''.

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



Feb 3, 2023, 2:50:13 PM2/3/23

You don't have too.

Have a look at this fiddle:
(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,...
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
> To view this discussion on the web visit

Christophe Perrod

Feb 3, 2023, 3:55:37 PM2/3/23
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.



Christophe Perrod

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

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 :

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)


Reply all
Reply to author
0 new messages