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.

enhancement: anywidget dynamic imports in static notebooks (#8110)

Myles Scolnick8a51f2bd381f
diff --git a/frontend/src/core/ai/context/providers/file.ts b/frontend/src/core/ai/context/providers/file.ts
index a6f781aa04..3679bbf32b 100644
--- a/frontend/src/core/ai/context/providers/file.ts
+++ b/frontend/src/core/ai/context/providers/file.ts
@@ -237,7 +237,7 @@ export class FileContextProvider extends AIContextProvider<FileContextItem> {
fileDetails.contents as Base64String,
mimeType,
);
- blob = await deserializeBlob(dataURL);
+ blob = deserializeBlob(dataURL);
} catch {
// Fallback to treating as text
blob = new Blob([fileDetails.contents], { type: mimeType });
diff --git a/frontend/src/core/static/__tests__/files.test.ts b/frontend/src/core/static/__tests__/files.test.ts
index c6bba1fe00..199c6763dd 100644
--- a/frontend/src/core/static/__tests__/files.test.ts
+++ b/frontend/src/core/static/__tests__/files.test.ts
@@ -6,7 +6,7 @@ import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { createLoader } from "@/plugins/impl/vega/vega-loader";
import { Functions } from "@/utils/functions";
import type { DataURLString } from "@/utils/json/base64";
-import { patchFetch, patchVegaLoader } from "../files";
+import { patchFetch, patchVegaLoader, resolveVirtualFileURL } from "../files";
// Start a tiny server to serve virtual files
const server = http.createServer((request, response) => {
@@ -350,6 +350,181 @@ describe("patchVegaLoader - loader.load", () => {
});
});
+describe("resolveVirtualFileURL", () => {
+ // Mock URL.createObjectURL for jsdom environment
+ const mockBlobURLs = new Map<string, Blob>();
+ let blobCounter = 0;
+
+ beforeAll(() => {
+ URL.createObjectURL = vi.fn((blob: Blob) => {
+ const url = `blob:test-${blobCounter++}`;
+ mockBlobURLs.set(url, blob);
+ return url;
+ });
+ URL.revokeObjectURL = vi.fn((url: string) => {
+ mockBlobURLs.delete(url);
+ });
+ });
+
+ afterAll(() => {
+ mockBlobURLs.clear();
+ });
+
+ it("should return a blob URL for virtual files", () => {
+ const virtualFiles = {
+ "/@file/widget.js":
+ "data:text/javascript;base64,ZXhwb3J0IGRlZmF1bHQgeyByZW5kZXI6ICgpID0+IHt9IH0=" as DataURLString,
+ };
+
+ const result = resolveVirtualFileURL("/@file/widget.js", virtualFiles);
+
+ expect(result).toMatch(/^blob:/);
+ });
+
+ it("should return the original URL for non-virtual files", () => {
+ const virtualFiles = {};
+
+ const result = resolveVirtualFileURL(
+ "http://example.com/widget.js",
+ virtualFiles,
+ );
+
+ expect(result).toBe("http://example.com/widget.js");
+ });
+
+ it("should handle various URL formats", () => {
+ const virtualFiles = {
+ "/@file/module.js":
+ "data:text/javascript;base64,Y29uc29sZS5sb2coJ3Rlc3QnKQ==" as DataURLString,
+ };
+
+ const testUrls = [
+ "/@file/module.js",
+ "./@file/module.js",
+ "http://example.com/@file/module.js",
+ ];
+
+ for (const url of testUrls) {
+ const result = resolveVirtualFileURL(url, virtualFiles);
+ expect(result).toMatch(/^blob:/);
+ }
+ });
+
+ it("should create blob URL with correct content", async () => {
+ const jsCode = "export default { render: () => {} }";
+ const base64Code = btoa(jsCode);
+ const virtualFiles = {
+ "/@file/test-module.js":
+ `data:text/javascript;base64,${base64Code}` as DataURLString,
+ };
+
+ const blobUrl = resolveVirtualFileURL(
+ "/@file/test-module.js",
+ virtualFiles,
+ );
+
+ expect(blobUrl).toMatch(/^blob:/);
+ expect(URL.createObjectURL).toHaveBeenCalled();
+
+ // Verify blob content through the mock
+ const blob = mockBlobURLs.get(blobUrl);
+ expect(blob).toBeDefined();
+ const text = await blob!.text();
+ expect(text).toBe(jsCode);
+ });
+
+ it("should handle file:// URLs with @file/ paths", () => {
+ const virtualFiles = {
+ "/@file/local-module.js":
+ "data:text/javascript;base64,ZXhwb3J0IGRlZmF1bHQge30=" as DataURLString,
+ };
+
+ const result = resolveVirtualFileURL(
+ "file:///Users/test/@file/local-module.js",
+ virtualFiles,
+ );
+
+ expect(result).toMatch(/^blob:/);
+ });
+
+ it("should handle different MIME types", async () => {
+ const virtualFiles = {
+ "/@file/script.js":
+ "data:application/javascript;base64,Y29uc3QgeCA9IDE=" as DataURLString,
+ };
+
+ const blobUrl = resolveVirtualFileURL("/@file/script.js", virtualFiles);
+
+ // Should still be a valid blob URL
+ expect(blobUrl).toMatch(/^blob:/);
+
+ // Verify blob content through the mock
+ const blob = mockBlobURLs.get(blobUrl);
+ expect(blob).toBeDefined();
+ const text = await blob!.text();
+ expect(text).toBe("const x = 1");
+ });
+
+ it("should handle blob: base URIs correctly", () => {
+ // Mock document.baseURI to simulate blob: protocol
+ const originalBaseURI = document.baseURI;
+ Object.defineProperty(document, "baseURI", {
+ value: "blob:https://example.com/uuid",
+ configurable: true,
+ });
+
+ const virtualFiles = {
+ "/@file/blob-module.js":
+ "data:text/javascript;base64,ZXhwb3J0IGRlZmF1bHQge30=" as DataURLString,
+ };
+
+ const result = resolveVirtualFileURL("/@file/blob-module.js", virtualFiles);
+
+ expect(result).toMatch(/^blob:/);
+
+ // Restore original baseURI
+ Object.defineProperty(document, "baseURI", {
+ value: originalBaseURI,
+ configurable: true,
+ });
+ });
+
+ it("should handle data URLs with no explicit MIME type", async () => {
+ const virtualFiles = {
+ "/@file/generic.bin": "data:;base64,SGVsbG8gV29ybGQ=" as DataURLString,
+ };
+
+ const blobUrl = resolveVirtualFileURL("/@file/generic.bin", virtualFiles);
+ expect(blobUrl).toMatch(/^blob:/);
+
+ // Verify blob content through the mock
+ const blob = mockBlobURLs.get(blobUrl);
+ expect(blob).toBeDefined();
+ const text = await blob!.text();
+ expect(text).toBe("Hello World");
+ });
+
+ it("should match URLs with prefix paths before /@file/", async () => {
+ const virtualFiles = {
+ "/@file/4263-66-yUGhgQXp.js":
+ "data:application/javascript;base64,ZnVuY3Rpb24gcmVuZGVyKCkge30=" as DataURLString,
+ };
+
+ const blobUrl = resolveVirtualFileURL(
+ "https://molab.marimo.app/preview/@file/4263-66-yUGhgQXp.js",
+ virtualFiles,
+ );
+
+ expect(blobUrl).toMatch(/^blob:/);
+
+ // Verify blob content through the mock
+ const blob = mockBlobURLs.get(blobUrl);
+ expect(blob).toBeDefined();
+ const text = await blob!.text();
+ expect(text).toBe("function render() {}");
+ });
+});
+
describe("maybeGetVirtualFile utility function", () => {
it("should handle URLs without leading dots correctly", async () => {
const virtualFiles = {
@@ -370,6 +545,25 @@ describe("maybeGetVirtualFile utility function", () => {
expect(text2).toBe("test");
});
+ it("should match URLs with prefix paths before /@file/", async () => {
+ const virtualFiles = {
+ "/@file/4263-66-yUGhgQXp.js":
+ "data:application/javascript;base64,ZnVuY3Rpb24gcmVuZGVyKCkge30=" as DataURLString,
+ };
+
+ const unpatch = patchFetch(virtualFiles);
+
+ // Test URL with a prefix path before /@file/
+ const response = await window.fetch(
+ "https://molab.marimo.app/preview/@file/4263-66-yUGhgQXp.js",
+ );
+ const text = await response.text();
+
+ expect(text).toBe("function render() {}");
+
+ unpatch();
+ });
+
it("should handle complex file:// URLs with nested paths", async () => {
const virtualFiles = {
"/@file/nested/data.json":
diff --git a/frontend/src/core/static/files.ts b/frontend/src/core/static/files.ts
index ff1b9dd8c6..9798c31e88 100644
--- a/frontend/src/core/static/files.ts
+++ b/frontend/src/core/static/files.ts
@@ -1,6 +1,8 @@
/* Copyright 2026 Marimo. All rights reserved. */
import type { Loader } from "@/plugins/impl/vega/vega-loader";
+import { deserializeBlob } from "@/utils/blob";
+import type { DataURLString } from "@/utils/json/base64";
import { Logger } from "@/utils/Logger";
import { getStaticVirtualFiles } from "./static-state";
import type { StaticVirtualFiles } from "./types";
@@ -120,6 +122,24 @@ function withoutLeadingDot(path: string): string {
return path.startsWith(".") ? path.slice(1) : path;
}
+/**
+ * Resolve a URL to a blob URL if it's a virtual file, for use with dynamic import().
+ * Unlike fetch, import() can't be patched, so we need to convert data URLs to blob URLs.
+ *
+ * @returns The original URL if not a virtual file, or a blob URL if it is
+ */
+export function resolveVirtualFileURL(
+ url: string,
+ files: StaticVirtualFiles = getStaticVirtualFiles(),
+): string {
+ const vfile = maybeGetVirtualFile(url, files);
+ if (!vfile) {
+ return url;
+ }
+ const blob = deserializeBlob(vfile as DataURLString);
+ return URL.createObjectURL(blob);
+}
+
function maybeGetVirtualFile(
url: string,
files: StaticVirtualFiles,
@@ -130,14 +150,11 @@ function maybeGetVirtualFile(
}
const pathname = new URL(url, base).pathname;
- // If if the URL starts with file://, then using the document.baseURI
- // will not work. In this case, should just chop off from /@file/...
- if (url.startsWith("file://")) {
- const indexOfFile = url.indexOf("/@file/");
- if (indexOfFile !== -1) {
- url = url.slice(indexOfFile);
- }
- }
+ // Extract the /@file/... suffix from the URL or pathname
+ // This handles URLs like https://example.com/prefix/@file/foo.js
+ // or file:///path/to/@file/foo.js
+ const filePathFromUrl = extractFilePath(url);
+ const filePathFromPathname = extractFilePath(pathname);
// Few variations to grab the URL.
// This can happen if a static file was open at file:// or https://
@@ -145,6 +162,19 @@ function maybeGetVirtualFile(
files[url] ||
files[withoutLeadingDot(url)] ||
files[pathname] ||
- files[withoutLeadingDot(pathname)]
+ files[withoutLeadingDot(pathname)] ||
+ (filePathFromUrl && files[filePathFromUrl]) ||
+ (filePathFromPathname && files[filePathFromPathname])
);
}
+
+/**
+ * Extract the /@file/... path from a URL string
+ */
+function extractFilePath(url: string): string | null {
+ const indexOfFile = url.indexOf("/@file/");
+ if (indexOfFile !== -1) {
+ return url.slice(indexOfFile);
+ }
+ return null;
+}
diff --git a/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx b/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx
index 61cbe08e7a..d002699d00 100644
--- a/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx
+++ b/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx
@@ -8,6 +8,8 @@ import useEvent from "react-use-event-hook";
import { z } from "zod";
import { MarimoIncomingMessageEvent } from "@/core/dom/events";
import { asRemoteURL } from "@/core/runtime/config";
+import { resolveVirtualFileURL } from "@/core/static/files";
+import { isStaticNotebook } from "@/core/static/static-state";
import { useAsyncData } from "@/hooks/useAsyncData";
import { useDeepCompareMemoize } from "@/hooks/useDeepCompareMemoize";
import {
@@ -93,7 +95,11 @@ const AnyWidgetSlot = (
error,
refetch,
} = useAsyncData(async () => {
- const url = asRemoteURL(jsUrl).toString();
+ let url = asRemoteURL(jsUrl).toString();
+ // In static notebooks, resolve virtual files to blob URLs for import()
+ if (isStaticNotebook()) {
+ url = resolveVirtualFileURL(url);
+ }
return await import(/* @vite-ignore */ url);
// Re-render on jsHash change (which is a hash of the contents of the file)
// instead of a jsUrl change because URLs may change without the contents
diff --git a/frontend/src/utils/__tests__/blob.test.ts b/frontend/src/utils/__tests__/blob.test.ts
index e3d023c756..02f3928aa9 100644
--- a/frontend/src/utils/__tests__/blob.test.ts
+++ b/frontend/src/utils/__tests__/blob.test.ts
@@ -20,7 +20,7 @@ describe("Blob serialization and deserialization", () => {
test("deserializeBlob should deserialize a base64 string to a Blob", async () => {
const serialized = await serializeBlob(testBlob);
- const deserialized = await deserializeBlob(serialized);
+ const deserialized = deserializeBlob(serialized);
expect(deserialized).toBeDefined();
expect(deserialized.size).toBe(testBlob.size);
expect(deserialized.type).toBe(testBlob.type);
@@ -28,7 +28,7 @@ describe("Blob serialization and deserialization", () => {
test("deserialized Blob should contain the original content", async () => {
const serialized = await serializeBlob(testBlob);
- const deserialized = await deserializeBlob(serialized);
+ const deserialized = deserializeBlob(serialized);
const reader = new FileReader();
// eslint-disable-next-line unicorn/prefer-blob-reading-methods
reader.readAsText(deserialized);
@@ -45,7 +45,7 @@ describe("Blob serialization and deserialization", () => {
type: "image/png",
});
const serialized = await serializeBlob(imageBlob);
- const deserialized = await deserializeBlob(serialized);
+ const deserialized = deserializeBlob(serialized);
expect(deserialized).toBeDefined();
expect(deserialized.size).toBe(imageBlob.size);
expect(deserialized.type).toBe(imageBlob.type);
diff --git a/frontend/src/utils/blob.ts b/frontend/src/utils/blob.ts
index a254973072..5ad021d3af 100644
--- a/frontend/src/utils/blob.ts
+++ b/frontend/src/utils/blob.ts
@@ -14,32 +14,19 @@ export function serializeBlob<T>(blob: Blob): Promise<DataURLString> {
});
}
-export function deserializeBlob(serializedBlob: DataURLString): Promise<Blob> {
- return new Promise((resolve, reject) => {
- try {
- // Extract the base64 data from the data URL
- const [prefix, base64Data] = serializedBlob.split(",", 2);
- const mimeType = /^data:(.+);base64$/.exec(prefix)?.[1];
- // Decode the base64 string
- const binaryString = atob(base64Data);
- // Convert the binary string to an array buffer
- const len = binaryString.length;
- const bytes = new Uint8Array(len);
- for (let i = 0; i < len; i++) {
- bytes[i] = binaryString.charCodeAt(i);
- }
- // Create a new Blob from the array buffer
- const blob = new Blob([bytes], { type: mimeType });
- resolve(blob);
- } catch (error) {
- reject(ensureError(error));
- }
- });
-}
-
-function ensureError(error: unknown): Error {
- if (error instanceof Error) {
- return error;
+export function deserializeBlob(serializedBlob: DataURLString): Blob {
+ // Extract the base64 data from the data URL
+ const [prefix, base64Data] = serializedBlob.split(",", 2);
+ const mimeType = /^data:(.+);base64$/.exec(prefix)?.[1];
+ // Decode the base64 string
+ const binaryString = atob(base64Data);
+ // Convert the binary string to an array buffer
+ const len = binaryString.length;
+ const bytes = new Uint8Array(len);
+ for (let i = 0; i < len; i++) {
+ bytes[i] = binaryString.charCodeAt(i);
}
- return new Error(`${error}`);
+ // Create a new Blob from the array buffer
+ const blob = new Blob([bytes], { type: mimeType });
+ return blob;
}