From 1b43bf80573fe743a6c6325bd491bd501c8a2df9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Bj=C3=B6rklund?= Date: Sat, 16 Sep 2023 22:26:14 +0300 Subject: [PATCH] Update xiplot and pyodide (#46) * delay loading of jsonschema * update xiplot * improve loading of delayed packages * update pyodide * check that delayed packages are can be lazily loaded --- Makefile | 5 ++++- patches/bootstrap.py | 7 +++++-- patches/bundle-dash-app.py | 13 +++++++++++++ src/worker.ts | 4 +--- xiplot | 2 +- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index d47c3b38..707db77a 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ BUNDLED_PLUGINS := xiplot/plugin_xiplot_filetypes -.PHONY: install_xiplot setup_build build_xiplot build_plugins bundle_plugins build_webdash deploy serve run all clean nuke +.PHONY: install_xiplot update_xiplot setup_build build_xiplot build_plugins bundle_plugins build_webdash deploy serve run all clean nuke all: run @@ -10,6 +10,9 @@ xiplot/.gitignore: git submodule init xiplot && \ git submodule update --depth=1 --remote xiplot +update_xiplot: + git submodule update --depth=1 --remote xiplot + setup_build: rm -rf dist && \ mkdir dist diff --git a/patches/bootstrap.py b/patches/bootstrap.py index e451ef48..dba81728 100644 --- a/patches/bootstrap.py +++ b/patches/bootstrap.py @@ -8,6 +8,7 @@ MOCKED_PACKAGES = "THIS_VALUE_SHOULD_HAVE_BEEN_FILLED_WHEN_BUNDLING_XIPLOT" REQUIRED_PACKAGES = "THIS_VALUE_SHOULD_HAVE_BEEN_FILLED_WHEN_BUNDLING_XIPLOT" +DELAYED_PACKAGES = "THIS_VALUE_SHOULD_HAVE_BEEN_FILLED_WHEN_BUNDLING_XIPLOT" XIPLOT_WHEEL = "THIS_VALUE_SHOULD_HAVE_BEEN_FILLED_WHEN_BUNDLING_XIPLOT" @@ -54,10 +55,12 @@ def setup_dash_app(url_base_pathname): ) # Asynchronously install sklearn (it will be unavailable the first couple of seconds after xiplot has loaded) + packages = "'" + "','".join(DELAYED_PACKAGES.values()) + "'" + modules = "import " + ",".join(DELAYED_PACKAGES.keys()) app._inline_scripts.append( "setTimeout(" - + "async () => await window.web_dash.worker_manager.executeWithAnyResponse(\"micropip.install('scikit-learn')\", {},)" - + '.then(() => window.web_dash.worker_manager.executeWithAnyResponse("import sklearn", {}))' + + f'async () => await window.web_dash.worker_manager.executeWithAnyResponse("micropip.install({packages})", {{}},)' + + f'.then(() => window.web_dash.worker_manager.executeWithAnyResponse("{modules}", {{}}))' + ", 5)" ) diff --git a/patches/bundle-dash-app.py b/patches/bundle-dash-app.py index 62822363..435c6945 100644 --- a/patches/bundle-dash-app.py +++ b/patches/bundle-dash-app.py @@ -155,6 +155,19 @@ def server_error(err): rep = f"REQUIRED_PACKAGES = [{required_packages}]" content = re.sub(reg, rep, content) +# Packages that are loaded after xiplot has started +delayed_packages = {"sklearn": "scikit-learn"} +for package in ["jsonschema"]: + try: + delayed_packages[package] = f"{package}=={version(package)}" + except: + print("Could not find version for", package) +for mod in delayed_packages.keys(): + assert mod not in sys.modules, f"Importing xiplot should not import {mod} " +reg = 'DELAYED_PACKAGES = (".+")' +rep = f"DELAYED_PACKAGES = {str(delayed_packages)}" +content = re.sub(reg, rep, content) + whl = sorted(dist.glob("xiplot-*-py3-none-any.whl")) assert len(whl) > 0, f"Could not find the xiplot wheel in {str(dist)}" reg = 'XIPLOT_WHEEL = "(.+)"' diff --git a/src/worker.ts b/src/worker.ts index eefc7bcd..8f5461af 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -32,7 +32,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -importScripts("https://cdn.jsdelivr.net/pyodide/v0.23.0/full/pyodide.js"); +importScripts("https://cdn.jsdelivr.net/pyodide/v0.24.0/full/pyodide.js"); /** * Handler receiving message events from the WorkerManager. @@ -98,7 +98,6 @@ onmessage = async (event: MessageEvent) => { * The global Pyodide interpreter interface. */ const maybe_pyodide: Promise = loadPyodide({ - homedir: "/", fullStdLib: false, stdout: postConsoleMessage, stderr: postConsoleError, @@ -211,7 +210,6 @@ function postConsoleError(consoleError: string) { declare function loadPyodide(options?: { indexURL?: string; - homedir?: string; fullStdLib?: boolean; stdout?: (msg: string) => void; stderr?: (msg: string) => void; diff --git a/xiplot b/xiplot index 9e093923..08f85ff7 160000 --- a/xiplot +++ b/xiplot @@ -1 +1 @@ -Subproject commit 9e093923da958fa5d2d61cdb41d8a71e160b315d +Subproject commit 08f85ff7177b888e45eb305af4821f25e8f6d7f8