Exabyte-io / materials-designer

A standalone React.js/Redux based web application for the design and visualization of atomistic materials structures. Used at Mat3ra.com and can be deployed in standalone mode.
https://mat3ra-materials-designer.netlify.app/
Other
10 stars 11 forks source link

pyodide button demo (SOF-6821) #132

Open VsevolodX opened 1 year ago

VsevolodX commented 1 year ago

The main problem is that when reading globals from pyodide instance, we get only default ones. Like name, loader etc, but not the ones that we added in python. Although the data is present inside pyodide and can be printed from python to the console (stdout)

pranabdas commented 1 year ago

Looks like globals behave differently when we try to move data both ways. It does work any one way. If we do not pass any globals with runPythonAsync, pyodide.globals.toJs().get(key) does work. But do we really need it to both ways? I guess we have other options:

  1. Pass data from JS to pyodide via globals, and get as functional return the other way
  2. Do not pass any data to pyodide, write functions with required data as args, do not call any functions there. Then access function names in JS via globals and call with args
    pyodide.runPython(`
    def sq(x):
    return x * x
    `);
    const sq = pyodide.globals.toJs().get("sq");
    console.log(sq(5));

Note that whenever there is python dictionary, we need .toJs() to transform into JS map.