Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyodide button demo (SOF-6821) #132

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from
Open

pyodide button demo (SOF-6821) #132

wants to merge 14 commits into from

Conversation

VsevolodX
Copy link
Contributor

No description provided.

@VsevolodX
Copy link
Contributor Author

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)

});
const dict = await this.pyodide.globals.toJs();
console.log("dict:", dict);
console.log("RESULT:", result);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VsevolodX we need result.toJs() to convert python dictionary into JS map. Then we can use .get(key) to extract values. Note result holds only functional return from python code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically this means we can only get the result of one run of code (the return of the last called function to be exact). A bit limiting, but in this particular usage, it's sufficient.

@pranabdas
Copy link
Contributor

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.

@VsevolodX VsevolodX changed the title pyodide button demo pyodide button demo (SOF-6821) Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants