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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix(anywidget): render legacy named render/initialize exports (#10093)
Myles Scolnicka9c2bcbe1efe
diff --git a/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx b/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsxindex 6a023ae9ee..8803bc7d6a 100644--- a/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx+++ b/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx@@ -140,7 +140,8 @@ const AnyWidgetSlot = (props: IPluginProps<ModelIdRef, Data>) => {return null;}- if (!isAnyWidgetModule(jsModule)) {+ const widget = resolveAnyWidget(jsModule, jsUrl);+ if (!widget) {return (<ErrorBanner error={getInvalidAnyWidgetModuleError(jsModule, jsUrl)} />);@@ -150,7 +151,7 @@ const AnyWidgetSlot = (props: IPluginProps<ModelIdRef, Data>) => {<LoadedSlot// Force remount when the widget module or model changes (cell re-run).key={`${jsHash}:${modelId}`}- widget={jsModule.default}+ widget={widget}modelId={modelId}host={host}/>@@ -200,6 +201,55 @@ function isAnyWidgetModule(mod: any): mod is { default: AnyWidget } {);}+const warnedLegacyNamedExportUrls = new Set<string>();+// Cache the synthesized widget per module namespace so its identity stays+// stable across re-renders (like a default export), avoiding needless+// WidgetBinding re-initialization.+const legacyWidgetCache = new WeakMap<object, AnyWidget>();++/**+ * Resolve the AnyWidget from a loaded module: prefer the AFM-spec default+ * export, otherwise synthesize one from legacy named `render`/`initialize`+ * exports. Returns null if neither is present.+ */+function resolveAnyWidget(mod: any, jsUrl: string): AnyWidget | null {+ if (isAnyWidgetModule(mod)) {+ return mod.default;+ }++ // Only fall back to legacy (pre-AFM) named exports when there is no default+ // export at all; a present-but-invalid default should surface an error+ // rather than be masked.+ if (mod?.default != null) {+ return null;+ }++ const hasNamedRender = typeof mod?.render === "function";+ const hasNamedInitialize = typeof mod?.initialize === "function";+ if (!hasNamedRender && !hasNamedInitialize) {+ return null;+ }++ const cached = legacyWidgetCache.get(mod);+ if (cached) {+ return cached;+ }++ if (!warnedLegacyNamedExportUrls.has(jsUrl)) {+ warnedLegacyNamedExportUrls.add(jsUrl);+ Logger.warn(+ `Anywidget module at ${jsUrl} uses deprecated top-level named ` ++ "exports (`render`/`initialize`). Per the AFM spec, use a default " ++ "export instead: `export default { render }`. " ++ "See https://anywidget.dev/en/afm/",+ );+ }++ const widget: AnyWidget = { render: mod.render, initialize: mod.initialize };+ legacyWidgetCache.set(mod, widget);+ return widget;+}+function getInvalidAnyWidgetModuleError(mod: unknown, jsUrl: string): Error {const afmDocs = "https://anywidget.dev/en/afm/";const hasNamedRender = isRecord(mod) && hasFunctionProperty(mod, "render");@@ -296,5 +346,6 @@ export const visibleForTesting = {LoadedSlot,runAnyWidgetModule,isAnyWidgetModule,+ resolveAnyWidget,getInvalidAnyWidgetModuleError,};diff --git a/frontend/src/plugins/impl/anywidget/__tests__/AnyWidgetPlugin.test.tsx b/frontend/src/plugins/impl/anywidget/__tests__/AnyWidgetPlugin.test.tsxindex 94d3785ff3..428a6722d0 100644--- a/frontend/src/plugins/impl/anywidget/__tests__/AnyWidgetPlugin.test.tsx+++ b/frontend/src/plugins/impl/anywidget/__tests__/AnyWidgetPlugin.test.tsx@@ -9,8 +9,12 @@ import { MODEL_MANAGER, Model } from "../model";import type { WidgetModelId } from "../types";import { BINDING_MANAGER } from "../widget-binding";-const { LoadedSlot, isAnyWidgetModule, getInvalidAnyWidgetModuleError } =- visibleForTesting;+const {+ LoadedSlot,+ isAnyWidgetModule,+ resolveAnyWidget,+ getInvalidAnyWidgetModuleError,+} = visibleForTesting;// Helper to create typed model IDs for testsconst asModelId = (id: string): WidgetModelId => id as WidgetModelId;@@ -170,6 +174,52 @@ describe("isAnyWidgetModule", () => {});});+describe("resolveAnyWidget", () => {+ const jsUrl = "./@file/widget.js";++ it("should return the default export when present", () => {+ const widget = { render: () => undefined };+ expect(resolveAnyWidget({ default: widget }, jsUrl)).toBe(widget);+ });++ it("should return a default factory function", () => {+ const factory = async () => ({ render: () => {} });+ expect(resolveAnyWidget({ default: factory }, jsUrl)).toBe(factory);+ });++ it("should synthesize a widget from a legacy named render export", () => {+ const render = vi.fn();+ const resolved = resolveAnyWidget({ render }, jsUrl);+ expect(resolved).not.toBeNull();+ expect(resolved).toMatchObject({ render });+ });++ it("should synthesize a widget from a legacy named initialize export", () => {+ const initialize = vi.fn();+ const resolved = resolveAnyWidget({ initialize }, jsUrl);+ expect(resolved).not.toBeNull();+ expect(resolved).toMatchObject({ initialize });+ });++ it("should return null for a module with no valid exports", () => {+ expect(resolveAnyWidget({}, jsUrl)).toBeNull();+ expect(resolveAnyWidget({ default: {} }, jsUrl)).toBeNull();+ expect(resolveAnyWidget({ render: "not a function" }, jsUrl)).toBeNull();+ });++ it("should return a stable widget identity across calls for one module", () => {+ const mod = { render: vi.fn() };+ expect(resolveAnyWidget(mod, jsUrl)).toBe(resolveAnyWidget(mod, jsUrl));+ });++ it("should not fall back to named exports when a default export is present", () => {+ // A present-but-invalid default should surface an error, not be masked.+ expect(+ resolveAnyWidget({ default: {}, render: vi.fn() }, jsUrl),+ ).toBeNull();+ });+});+describe("getInvalidAnyWidgetModuleError", () => {const jsUrl = "./@file/widget.js";