Skip to content

Commit

Permalink
Update xiplot and pyodide (#46)
Browse files Browse the repository at this point in the history
* delay loading of jsonschema

* update xiplot

* improve loading of delayed packages

* update pyodide

* check that delayed packages are can be lazily loaded
  • Loading branch information
Aggrathon authored Sep 16, 2023
1 parent 85d98e4 commit 1b43bf8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions patches/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down Expand Up @@ -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)"
)

Expand Down
13 changes: 13 additions & 0 deletions patches/bundle-dash-app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "(.+)"'
Expand Down
4 changes: 1 addition & 3 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -98,7 +98,6 @@ onmessage = async (event: MessageEvent) => {
* The global Pyodide interpreter interface.
*/
const maybe_pyodide: Promise<PyodideInterface> = loadPyodide({
homedir: "/",
fullStdLib: false,
stdout: postConsoleMessage,
stderr: postConsoleError,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1b43bf8

Please sign in to comment.