Commit e83894de authored by Michael Droettboom's avatar Michael Droettboom Committed by GitHub

Merge pull request #282 from mdboom/globals-proxy

Add a proxy to get global objects from Python
parents 534bfbaf 2d473282
...@@ -98,6 +98,14 @@ For example, to access the `foo` Python object from Javascript: ...@@ -98,6 +98,14 @@ For example, to access the `foo` Python object from Javascript:
| | | types, a Proxy object to the Python | | | | types, a Proxy object to the Python |
| | | object is returned. | | | | object is returned. |
### pyodide.globals
An object whose attributes are members of the Python global namespace. This is a
more convenient alternative to `pyodide.pyimport`.
For example, to access the `foo` Python object from Javascript:
`pyodide.globals.foo`
### pyodide.repr(obj) ### pyodide.repr(obj)
...@@ -178,7 +186,7 @@ pyodide.runPythonAsync(code, messageCallback) ...@@ -178,7 +186,7 @@ pyodide.runPythonAsync(code, messageCallback)
Returns the pyodide version. Returns the pyodide version.
It can be either the exact release version (e.g. `0.1.0`), or It can be either the exact release version (e.g. `0.1.0`), or
the latest release version followed by the number of commits since, and the latest release version followed by the number of commits since, and
the git hash of the current commit (e.g. `0.1.0-1-bd84646`). the git hash of the current commit (e.g. `0.1.0-1-bd84646`).
...@@ -191,4 +199,3 @@ None ...@@ -191,4 +199,3 @@ None
| name | type | description | | name | type | description |
|-----------|--------|------------------------| |-----------|--------|------------------------|
| *version* | String | Pyodide version string | | *version* | String | Pyodide version string |
...@@ -233,6 +233,7 @@ var languagePluginLoader = new Promise((resolve, reject) => { ...@@ -233,6 +233,7 @@ var languagePluginLoader = new Promise((resolve, reject) => {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Rearrange namespace for public API // Rearrange namespace for public API
let PUBLIC_API = [ let PUBLIC_API = [
'globals',
'loadPackage', 'loadPackage',
'loadedPackages', 'loadedPackages',
'pyimport', 'pyimport',
...@@ -277,6 +278,8 @@ var languagePluginLoader = new Promise((resolve, reject) => { ...@@ -277,6 +278,8 @@ var languagePluginLoader = new Promise((resolve, reject) => {
.then((response) => response.json()) .then((response) => response.json())
.then((json) => { .then((json) => {
fixRecursionLimit(window.pyodide); fixRecursionLimit(window.pyodide);
window.pyodide.globals =
window.pyodide.runPython('import sys\nsys.modules["__main__"]');
window.pyodide = makePublicAPI(window.pyodide, PUBLIC_API); window.pyodide = makePublicAPI(window.pyodide, PUBLIC_API);
window.pyodide._module.packages = json; window.pyodide._module.packages = json;
resolve(); resolve();
......
...@@ -599,3 +599,14 @@ def test_runpythonasync_exception_after_import(selenium_standalone): ...@@ -599,3 +599,14 @@ def test_runpythonasync_exception_after_import(selenium_standalone):
assert "ZeroDivisionError" in str(e) assert "ZeroDivisionError" in str(e)
else: else:
assert False assert False
def test_py(selenium_standalone):
selenium_standalone.run(
"""
def func():
return 42
"""
)
assert selenium_standalone.run_js('return pyodide.globals.func()') == 42
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment