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

Value of global variable could not been gotten through pyodide.globals.get() #137

Open
MRYingLEE opened this issue Aug 31, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@MRYingLEE
Copy link

In a Jupyterlite with slightly customized Pyodide environment, I failed to access value of a global variable.
In UI, I ran the following assignment statement in a cell:

a=1

In some customized code, I ran the following Typescript code for testing:

          const { globals } = this._pyodide;
          const a_before = globals.get("a");
          if (a_before)
            try {
              console.log(`Before run, the value is ${a_before}.`);
            } catch (err: any) {
              console.error(err);
              console.log(`Before run, the value is ${a_before.toJS()}.`);
            }

          await this._pyodide.loadPackagesFromImports(newCode);
          const result = await this._pyodide.runPythonAsync(newCode);

          if (result)
            console.log(`After run, the result is ${result}`);

          const a_after = globals.get("a");
          if (a_after)
            try {
              console.log(`After run, the value is ${a_after}.`);
            } catch (err: any) {
              console.error(err);
              console.log(`After run, the value is ${a_after.toJS()}.`);
            }

It failed to get the value of the global variable. But if the newCode assigned some value to vaiable "a", then the globals.get("a") would succeed afterwards.

It seems the namespace of Pyodide kernel is independent to that of pyodide.runPythonAsync().

Please clarify.
Thanks,

@MRYingLEE MRYingLEE added the bug Something isn't working label Aug 31, 2024
@jtpio
Copy link
Member

jtpio commented Sep 3, 2024

In some customized code, I ran the following Typescript code for testing:

Curious where you run this code?

@MRYingLEE
Copy link
Author

I put my testing code in the function of async execute(content: any, parent: any) , with an if-else branch near

const res = await this._kernel.run(content.code);
.

In future, the code will be run in a Jupyterlab frontend extension. For example, to let AI explain code along with the current values of the used global variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants
@jtpio @MRYingLEE and others