From 962140981acb91305d23f3a2ccdc0e3ddfd74fc4 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sat, 22 May 2021 21:54:27 -0400 Subject: [PATCH] API Don't expose pyodide as a global variable in loadPyodide (#1597) --- Makefile | 4 +-- conftest.py | 4 ++- docs/development/new-packages.md | 2 +- docs/project/changelog.md | 5 ++++ docs/usage/loading-packages.md | 8 +++--- docs/usage/quickstart.md | 16 ++++++------ docs/usage/webworker.md | 2 +- pyodide-build/pyodide_build/buildpkg.py | 2 +- src/js/load-pyodide.js | 4 +-- src/js/pyodide.js | 33 ++++++++++++------------- src/templates/console.html | 2 +- src/webworker.js | 2 +- 12 files changed, 46 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index 1c35e2d9c..0d5d45fa9 100644 --- a/Makefile +++ b/Makefile @@ -175,7 +175,7 @@ build/test.data: $(CPYTHONLIB) ) ( \ cd build; \ - python $(FILEPACKAGER) test.data --lz4 --preload ../$(CPYTHONLIB)/test@/lib/python$(PYMINOR)/test --js-output=test.js --export-name=globalThis.pyodide._module --exclude __pycache__ \ + python $(FILEPACKAGER) test.data --lz4 --preload ../$(CPYTHONLIB)/test@/lib/python$(PYMINOR)/test --js-output=test.js --export-name=globalThis.__pyodide_module --exclude __pycache__ \ ) $(UGLIFYJS) build/test.js -o build/test.js @@ -188,7 +188,7 @@ build/distutils.data: $(CPYTHONLIB) ) ( \ cd build; \ - python $(FILEPACKAGER) distutils.data --lz4 --preload ../$(CPYTHONLIB)/distutils@/lib/python$(PYMINOR)/distutils --js-output=distutils.js --export-name=pyodide._module --exclude __pycache__ --exclude tests \ + python $(FILEPACKAGER) distutils.data --lz4 --preload ../$(CPYTHONLIB)/distutils@/lib/python$(PYMINOR)/distutils --js-output=distutils.js --export-name=globalThis.__pyodide_module --exclude __pycache__ --exclude tests \ ) $(UGLIFYJS) build/distutils.js -o build/distutils.js diff --git a/conftest.py b/conftest.py index 49566ad8e..39acbe7ad 100644 --- a/conftest.py +++ b/conftest.py @@ -98,7 +98,9 @@ class SeleniumWrapper: self.driver.get(f"http://{server_hostname}:{server_port}/test.html") self.javascript_setup() if load_pyodide: - self.run_js("await loadPyodide({ indexURL : './', fullStdLib: false });") + self.run_js( + "window.pyodide = await loadPyodide({ indexURL : './', fullStdLib: false });" + ) self.save_state() self.script_timeout = script_timeout self.driver.set_script_timeout(script_timeout) diff --git a/docs/development/new-packages.md b/docs/development/new-packages.md index 820a3b846..b668773ea 100644 --- a/docs/development/new-packages.md +++ b/docs/development/new-packages.md @@ -245,7 +245,7 @@ We invoke it as follows: ```sh $ ./file_packager.py PACKAGE_NAME.data \ --js-output=PACKAGE_NAME.js \ - --export-name=pyodide._module \ + --export-name=globalThis.__pyodide_module \ --use-preload-plugins \ --preload /PATH/TO/LIB/@/lib/python3.8/site-packages/PACKAGE_NAME/ \ --exclude "*__pycache__*" \ diff --git a/docs/project/changelog.md b/docs/project/changelog.md index b1c66be24..47bf965e5 100644 --- a/docs/project/changelog.md +++ b/docs/project/changelog.md @@ -12,6 +12,11 @@ substitutions: ## [Unreleased] +- {{ API }} {any}`loadPyodide` no longer automatically stores the API into a + global variable called `pyodide`. To get old behavior, say `globalThis.pyodide + = await loadPyodide({...})`. + {pr}`1597` + ## Standard library - The following standard library modules are now available as standalone packages diff --git a/docs/usage/loading-packages.md b/docs/usage/loading-packages.md index eb02c6661..d77381715 100644 --- a/docs/usage/loading-packages.md +++ b/docs/usage/loading-packages.md @@ -48,8 +48,9 @@ pyodide.loadPackage(['cycler', 'pytz']); {any}`pyodide.loadPackage` returns a `Promise` which resolves when all of the packages are finished loading: ```javascript +let pyodide; async function main(){ - await loadPyodide({ 'indexURL' : '' }); + pyodide = await loadPyodide({ 'indexURL' : '' }); await pyodide.loadPackage('matplotlib'); // matplotlib is now available } @@ -118,9 +119,10 @@ installs from PyPi.