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.

Add mechanism for injecting custom file stores (#5161)

Pepijn de Vos48b5a908bf0a
diff --git a/frontend/src/core/wasm/bridge.ts b/frontend/src/core/wasm/bridge.ts
index 1065b7fefe..d26c28de4f 100644
--- a/frontend/src/core/wasm/bridge.ts
+++ b/frontend/src/core/wasm/bridge.ts
@@ -137,6 +137,7 @@ export class PyodideBridge implements RunRequests, EditRequests {
// Pass the code to the worker
// If a filename is provided, it will be used to save the file
// If no filename is provided, the file will not be saved
+
const code = await notebookFileStore.readFile();
const fallbackCode = await fallbackFileStore.readFile();
const filename = PyodideRouter.getFilename();
diff --git a/frontend/src/core/wasm/store.ts b/frontend/src/core/wasm/store.ts
index 73b23d8ae1..a381585542 100644
--- a/frontend/src/core/wasm/store.ts
+++ b/frontend/src/core/wasm/store.ts
@@ -88,6 +88,10 @@ const emptyFileStore: FileStore = {
export class CompositeFileStore implements FileStore {
constructor(private stores: FileStore[]) {}
+ insert(index: number, store: FileStore) {
+ this.stores.splice(index, 0, store);
+ }
+
saveFile(contents: string) {
this.stores.forEach((store) => store.saveFile(contents));
}
diff --git a/frontend/src/mount.tsx b/frontend/src/mount.tsx
index 1894f4abba..626328c7ba 100644
--- a/frontend/src/mount.tsx
+++ b/frontend/src/mount.tsx
@@ -33,6 +33,8 @@ import { getMarimoCode } from "@/core/meta/globals";
import type * as api from "@marimo-team/marimo-api";
import { notebookAtom } from "./core/cells/cells";
import { notebookStateFromSession } from "./core/cells/session";
+import type { FileStore } from "./core/wasm/store";
+import { notebookFileStore } from "./core/wasm/store";
let hasMounted = false;
@@ -177,6 +179,11 @@ const mountOptionsSchema = z.object({
.nullish()
.transform((val) => val ?? ""),
+ /**
+ * File stores for persistence
+ */
+ fileStores: z.array(z.custom<FileStore>()).optional(),
+
/**
* Serialized Session["NotebookSessionV1"] snapshot
*/
@@ -219,6 +226,22 @@ function initStore(options: unknown) {
const mode = parsedOptions.data.mode as AppMode;
preloadPage(mode);
+ // Initialize file stores if provided
+ if (
+ parsedOptions.data.fileStores &&
+ parsedOptions.data.fileStores.length > 0
+ ) {
+ Logger.log("🗄️ Initializing file stores via mount...");
+ // Insert file stores at the beginning (highest priority)
+ // Insert in reverse order so first in array gets highest priority
+ for (let i = parsedOptions.data.fileStores.length - 1; i >= 0; i--) {
+ notebookFileStore.insert(0, parsedOptions.data.fileStores[i]);
+ }
+ Logger.log(
+ `🗄️ Injected ${parsedOptions.data.fileStores.length} file store(s) into notebookFileStore`,
+ );
+ }
+
// Files
store.set(filenameAtom, parsedOptions.data.filename);
store.set(codeAtom, parsedOptions.data.code);