Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

perf: lazy load completion deps (for WASM) (#2604)

Akshay Agrawal0e0221ff580c
diff --git a/development_docs/pyodide.md b/development_docs/pyodide.md
index 69fa11cb7f..333693ccf6 100644
--- a/development_docs/pyodide.md
+++ b/development_docs/pyodide.md
@@ -9,13 +9,11 @@ PYODIDE=true pnpm dev
## Running the frontend against a local backend
-This requires `watchdog` to be installed (`pip install watchdog`).
-
```bash
# build once
-python3 -m build
+hatch build
# server and watch for changes
-python3 pyodide/build_and_serve.py
+uv run pyodide/build_and_serve.py
# in another terminal
cd frontend
PYODIDE=true VITE_MARIMO_VERSION=$(marimo --version) pnpm dev
diff --git a/frontend/src/core/wasm/worker/bootstrap.ts b/frontend/src/core/wasm/worker/bootstrap.ts
index dec04469ed..5bff4870b3 100644
--- a/frontend/src/core/wasm/worker/bootstrap.ts
+++ b/frontend/src/core/wasm/worker/bootstrap.ts
@@ -62,8 +62,7 @@ export class DefaultWasmController implements WasmController {
const span = t.startSpan("loadPyodide");
const pyodide = await loadPyodide({
// Perf: These get loaded while pyodide is being bootstrapped
- // The packages can be found here: https://pyodide.org/en/stable/usage/packages-in-pyodide.html
- packages: ["micropip", "docutils", "Pygments", "jedi", "pyodide-http"],
+ packages: ["micropip"],
// Without this, this fails in Firefox with
// `Could not extract indexURL path from pyodide module`
// This fixes for Firefox and does not break Chrome/others
@@ -213,6 +212,18 @@ export class DefaultWasmController implements WasmController {
code = `import duckdb\n${code}`;
}
+ // Add:
+ // 1. additional dependencies of marimo that are lazily loaded.
+ // 2. pyodide-http, a patch to make basic http requests work in pyodide
+ //
+ // These packages are included with Pyodide, which is why we don't add them
+ // to `foundImports`:
+ // https://pyodide.org/en/stable/usage/packages-in-pyodide.html
+ code = `import docutils\n${code}`;
+ code = `import Pygments\n${code}`;
+ code = `import jedi\n${code}`;
+ code = `import pyodide_http\n${code}`;
+
const imports = [...foundImports];
// Load from pyodide
@@ -235,7 +246,7 @@ export class DefaultWasmController implements WasmController {
import sys
# Filter out builtins
missing = [p for p in ${JSON.stringify(missingPackages)} if p not in sys.modules]
- print("[py] Loading packages from micropip:", missing)
+ print("Loaded from micropip:", missing)
await micropip.install(missing)
`)
.catch((error) => {
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index a8899975d6..2c79e25f72 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -73,7 +73,6 @@ from marimo._plugins.core.web_component import JSONType
from marimo._plugins.ui._core.ui_element import MarimoConvertValueException
from marimo._runtime import dataflow, handlers, marimo_pdb, patches
from marimo._runtime.app_meta import AppMeta
-from marimo._runtime.complete import complete, completion_worker
from marimo._runtime.context import (
ContextNotInitializedError,
ExecutionContext,
@@ -523,6 +522,8 @@ class Kernel:
self, completion_queue: QueueType[CodeCompletionRequest]
) -> None:
"""Must be called after context is initialized"""
+ from marimo._runtime.complete import completion_worker
+
threading.Thread(
target=completion_worker,
args=(
@@ -540,6 +541,8 @@ class Kernel:
def code_completion(
self, request: CodeCompletionRequest, docstrings_limit: int
) -> None:
+ from marimo._runtime.complete import complete
+
complete(
request,
self.graph,
diff --git a/marimo/_utils/rst_to_html.py b/marimo/_utils/rst_to_html.py
index 1dc4272db5..e90eb80f25 100644
--- a/marimo/_utils/rst_to_html.py
+++ b/marimo/_utils/rst_to_html.py
@@ -2,12 +2,12 @@
import contextlib
import io
-from docutils.core import publish_parts # type: ignore[import-untyped]
-
def convert_rst_to_html(rst_content: str) -> str:
"""Convert RST content to HTML."""
+ from docutils.core import publish_parts # type: ignore[import-untyped]
+
# redirect stderr and ignore it to silence error messages
with contextlib.redirect_stderr(io.StringIO()) as _:
parts = publish_parts(
diff --git a/pyodide/build_and_serve.py b/pyodide/build_and_serve.py
index 9c5607d408..f8b4639459 100644
--- a/pyodide/build_and_serve.py
+++ b/pyodide/build_and_serve.py
@@ -1,3 +1,11 @@
+# /// script
+# requires-python = ">=3.12"
+# dependencies = [
+# "watchdog",
+# ]
+# ///
+from __future__ import annotations
+
import http.server
import os
import socketserver
@@ -29,7 +37,7 @@ class WheelBuilderHandler(PatternMatchingEventHandler):
def on_any_event(self, event) -> None:
print(f"Change detected: {event.src_path}")
print("Building wheel...")
- subprocess.run(["python3", "-m", "build"])
+ subprocess.run(["hatch", "build"])
print("Wheel built successfully.")