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
add global previewed tables state (#8630)
Shahmir Varqha830edaed51bf
diff --git a/frontend/src/core/cells/ids.ts b/frontend/src/core/cells/ids.tsindex 8beaa13b64..fb6bcb91c6 100644--- a/frontend/src/core/cells/ids.ts+++ b/frontend/src/core/cells/ids.ts@@ -1,6 +1,7 @@/* Copyright 2026 Marimo. All rights reserved. *//* eslint-disable @typescript-eslint/no-redeclare */+import { OBJECT_ID_ATTR } from "@/core/dom/ui-element-constants";import { invariant } from "@/utils/invariant";import type { TypedString } from "../../utils/typed";@@ -110,7 +111,7 @@ export function findCellId(element: HTMLElement): CellId | null {export type UIElementId = `${CellId}-${string}`;export const UIElementId = {parse(element: Element): UIElementId | null {- return element.getAttribute("object-id") as UIElementId | null;+ return element.getAttribute(OBJECT_ID_ATTR) as UIElementId | null;},parseOrThrow(element: Element): UIElementId {const id = UIElementId.parse(element);diff --git a/frontend/src/core/dom/ui-element-constants.ts b/frontend/src/core/dom/ui-element-constants.tsnew file mode 100644index 0000000000..10774af794--- /dev/null+++ b/frontend/src/core/dom/ui-element-constants.ts@@ -0,0 +1,15 @@+/* Copyright 2026 Marimo. All rights reserved. */++/**+ * Stable identifier for a UI element, deterministic across re-executions+ * of the same cell (based on cell ID + creation order).+ * Used to synchronize multiple instances and kernel state.+ */+export const OBJECT_ID_ATTR = "object-id";++/**+ * Random token that changes every time a UI element is constructed+ * (i.e., every cell execution). Used to detect stale elements and+ * force re-renders when a cell re-runs.+ */+export const RANDOM_ID_ATTR = "random-id";diff --git a/frontend/src/core/dom/ui-element.ts b/frontend/src/core/dom/ui-element.tsindex 8c8d099516..fccad48e32 100644--- a/frontend/src/core/dom/ui-element.ts+++ b/frontend/src/core/dom/ui-element.ts@@ -8,6 +8,7 @@ import {MarimoValueInputEvent,type MarimoValueInputEventType,} from "./events";+import { RANDOM_ID_ATTR } from "./ui-element-constants";import { UI_ELEMENT_REGISTRY } from "./uiregistry";import "./ui-element.css";@@ -189,7 +190,7 @@ export function initializeUIElement() {// used like a React key. If the random-id changes, we need to unmount and// remount its child.static get observedAttributes() {- return ["random-id"];+ return [RANDOM_ID_ATTR];}attributeChangedCallback(@@ -199,7 +200,7 @@ export function initializeUIElement() {) {if (this.initialized) {const hasChanged = oldValue !== newValue;- if (name === "random-id" && hasChanged) {+ if (name === RANDOM_ID_ATTR && hasChanged) {// deregister/clean-up this instancethis.disconnectedCallback();// remove and re-add its child to force it to re-render; note thatdiff --git a/frontend/src/core/islands/components/web-components.tsx b/frontend/src/core/islands/components/web-components.tsxindex dee068cec0..e9b8e80eae 100644--- a/frontend/src/core/islands/components/web-components.tsx+++ b/frontend/src/core/islands/components/web-components.tsx@@ -6,6 +6,7 @@ import ReactDOM, { type Root } from "react-dom/client";import { ErrorBoundary } from "@/components/editor/boundary/ErrorBoundary";import { TooltipProvider } from "@/components/ui/tooltip";import { notebookAtom } from "@/core/cells/cells";+import { OBJECT_ID_ATTR } from "@/core/dom/ui-element-constants";import { UI_ELEMENT_REGISTRY } from "@/core/dom/uiregistry";import { LocaleProvider } from "@/core/i18n/locale-provider";import { renderHTML } from "@/plugins/core/RenderHTML";@@ -60,7 +61,7 @@ export class MarimoIslandElement extends HTMLElement {const codeCallback: () => string = optionalEditor? () =>`${UI_ELEMENT_REGISTRY.lookupValue(- optionalEditor.props["object-id"],+ optionalEditor.props[OBJECT_ID_ATTR],)}`: () => code;diff --git a/frontend/src/plugins/impl/DataTablePlugin.tsx b/frontend/src/plugins/impl/DataTablePlugin.tsxindex 5f47ee33bc..e46281d01e 100644--- a/frontend/src/plugins/impl/DataTablePlugin.tsx+++ b/frontend/src/plugins/impl/DataTablePlugin.tsx@@ -60,7 +60,11 @@ import { ContextAwarePanelItem } from "@/components/editor/chrome/panels/contextimport { Alert, AlertTitle } from "@/components/ui/alert";import { Button } from "@/components/ui/button";import { DelayMount } from "@/components/utils/delay-mount";-import { type CellId, findCellId } from "@/core/cells/ids";+import { type CellId, findCellId, UIElementId } from "@/core/cells/ids";+import {+ OBJECT_ID_ATTR,+ RANDOM_ID_ATTR,+} from "@/core/dom/ui-element-constants";import { slotsController } from "@/core/slots/slots";import { store } from "@/core/state/jotai";import { isStaticNotebook } from "@/core/static/static-state";@@ -361,6 +365,7 @@ export const DataTablePlugin = createPlugin<S>("marimo-table")<LazyDataTableComponentisLazy={props.data.lazy}preload={props.data.preload}+ host={props.host}><LoadingDataTableComponent{...props.data}@@ -377,21 +382,66 @@ export const DataTablePlugin = createPlugin<S>("marimo-table"));});+/**+ * Tracks which lazy tables have been previewed across remounts (e.g. tab switches).+ * Keyed by uiElementId (stable across remounts) with randomId as value+ * (changes on cell re-execution, so stale entries are naturally invalidated).+ */+const previewedTables = new Map<UIElementId, string>();++function wasTablePreviewed(+ uiElementId: UIElementId | null | undefined,+ randomId: string | null | undefined,+): boolean {+ return (+ uiElementId != null &&+ randomId != null &&+ previewedTables.get(uiElementId) === randomId+ );+}++function markTablePreviewed(+ uiElementId: UIElementId | null | undefined,+ randomId: string | null | undefined,+): void {+ if (uiElementId != null && randomId != null) {+ previewedTables.set(uiElementId, randomId);+ }+}+const LazyDataTableComponent = ({isLazy: initialIsLazy,children,preload,+ host,}: {isLazy: boolean;children: React.ReactNode;preload: boolean;+ host: HTMLElement;}) => {- const [isLazy, setIsLazy] = useState(initialIsLazy && !preload);+ const parentElement = host.closest(`[${OBJECT_ID_ATTR}]`);+ const uiElementId = parentElement ? UIElementId.parse(parentElement) : null;++ const randomId = host+ .closest(`[${RANDOM_ID_ATTR}]`)+ ?.getAttribute(RANDOM_ID_ATTR);++ const [isLazy, setIsLazy] = useState(+ initialIsLazy && !preload && !wasTablePreviewed(uiElementId, randomId),+ );if (isLazy) {return (<div className="flex h-20 items-center justify-center">- <Button variant="outline" size="xs" onClick={() => setIsLazy(false)}>+ <Button+ variant="outline"+ size="xs"+ onClick={() => {+ markTablePreviewed(uiElementId, randomId);+ setIsLazy(false);+ }}+ ><Table2Icon className="mr-2 h-4 w-4" />Preview data</Button>diff --git a/frontend/src/plugins/impl/FileBrowserPlugin.tsx b/frontend/src/plugins/impl/FileBrowserPlugin.tsxindex 45aa5e55de..87e0b11344 100644--- a/frontend/src/plugins/impl/FileBrowserPlugin.tsx+++ b/frontend/src/plugins/impl/FileBrowserPlugin.tsx@@ -15,6 +15,7 @@ import { Label } from "@/components/ui/label";import { NativeSelect } from "@/components/ui/native-select";import { Table, TableBody, TableCell, TableRow } from "@/components/ui/table";import { toast } from "@/components/ui/use-toast";+import { RANDOM_ID_ATTR } from "@/core/dom/ui-element-constants";import { useAsyncData } from "@/hooks/useAsyncData";import { useInternalStateWithSync } from "@/hooks/useInternalStateWithSync";import { cn } from "@/utils/cn";@@ -150,7 +151,9 @@ export const FileBrowser = ({// HACK: use the random-id of the host element to force a re-render// when the random-id changes, this means the cell was re-rendered- const randomId = host.closest("[random-id]")?.getAttribute("random-id");+ const randomId = host+ .closest(`[${RANDOM_ID_ATTR}]`)+ ?.getAttribute(RANDOM_ID_ATTR);const { data, error, isPending } = useAsyncData(() => {return list_directory({ path: path });diff --git a/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx b/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsxindex 2bf57ea45f..9df7882156 100644--- a/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx+++ b/frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx@@ -4,6 +4,7 @@import type { AnyWidget } from "@anywidget/types";import { useEffect, useRef } from "react";import { z } from "zod";+import { RANDOM_ID_ATTR } from "@/core/dom/ui-element-constants";import { useAsyncData } from "@/hooks/useAsyncData";import type { HTMLElementNotDerivedFromRef } from "@/hooks/useEventListener";import { createPlugin } from "@/plugins/core/builder";@@ -145,7 +146,9 @@ const AnyWidgetSlot = (props: IPluginProps<ModelIdRef, Data>) => {}// Find the closest parent element with an attribute of `random-id`- const randomId = props.host.closest("[random-id]")?.getAttribute("random-id");+ const randomId = props.host+ .closest(`[${RANDOM_ID_ATTR}]`)+ ?.getAttribute(RANDOM_ID_ATTR);const key = randomId ?? jsUrl;return (