According to the api docs, pyodide provides a way to get a user defined function or variable through pyodide.globals.x
as an example (where x is defined by the user). I want to be able to get all globals at once but there is nothing in the docs about this. I was wondering if there is perhaps a clever way of doing this?
EDIT:
If you type pyodide.globals
in developer tools there is no reference to x but if you type pyodide.globals.x
(and it is defined) you get the value. How does this work?
pyodide.globals
is a PyProxy object that exposes the Python globals()
dictionary to JavaScript. All attribute access will be proxied to the python dictionary,
see the documentation for more details.
The easiest way to get all globals is to call pyodide.globals.toJs()
which will return a Map object with all the entries. Note that this currently only works with the development version of pyodide or the upcoming 0.17.0 release.