jupyterlite / jupyterlite/pyodide-kernel
Value of global variable could not been gotten through pyodide.globals.get()
- Dominant language
- Python
- Stars
- 90
- Forks
- 47
- Avg merge
- 3d 13h
- Merged PRs (30d)
- 11
Description
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:
```python
a=1
```
In some customized code, I ran the following Typescript code for testing:
```Typescript
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,
Contributor guide
Assessment
This issue has not been assessed yet.