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.

refactor(frontend): consolidate readonly SQL code display (#10089)

Shahmir Varqhad5dcc65d490c
diff --git a/frontend/src/components/editor/chrome/panels/__tests__/snippet-display.test.ts b/frontend/src/components/editor/chrome/panels/__tests__/snippet-display.test.ts
deleted file mode 100644
index eb8f6b2fb2..0000000000
--- a/frontend/src/components/editor/chrome/panels/__tests__/snippet-display.test.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-/* Copyright 2026 Marimo. All rights reserved. */
-
-import { describe, expect, it } from "vitest";
-import { getSnippetDisplay } from "../snippet-display";
-
-describe("getSnippetDisplay", () => {
- it("shows sql cells as the sql query", () => {
- const { language, value } = getSnippetDisplay(
- 'df = mo.sql("""SELECT * FROM users LIMIT 5""")',
- );
- expect(language).toBe("sql");
- expect(value).toBe("SELECT * FROM users LIMIT 5");
- });
-
- it("keeps plain python cells as python", () => {
- const code = "x = 1 + 2\nprint(x)";
- expect(getSnippetDisplay(code)).toEqual({
- language: "python",
- value: code,
- });
- });
-});
diff --git a/frontend/src/components/editor/chrome/panels/snippet-display.ts b/frontend/src/components/editor/chrome/panels/snippet-display.ts
deleted file mode 100644
index 8702d4e238..0000000000
--- a/frontend/src/components/editor/chrome/panels/snippet-display.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-/* Copyright 2026 Marimo. All rights reserved. */
-
-import { SQLParser } from "@marimo-team/smart-cells";
-
-export type SnippetLanguage = "python" | "sql";
-
-export interface SnippetDisplay {
- language: SnippetLanguage;
- value: string;
-}
-
-const sqlParser = new SQLParser();
-
-/**
- * Decide how a snippet's code should be shown in the panel.
- *
- * SQL cells are stored as python `mo.sql(...)`. Unwrap the inner query and
- * highlight it as SQL, matching how the cell renders once the snippet is
- * inserted. Everything else stays python.
- */
-export function getSnippetDisplay(code: string): SnippetDisplay {
- if (sqlParser.isSupported(code)) {
- const { code: query } = sqlParser.transformIn(code);
- return { language: "sql", value: query };
- }
- return { language: "python", value: code };
-}
diff --git a/frontend/src/components/editor/chrome/panels/snippets-panel.tsx b/frontend/src/components/editor/chrome/panels/snippets-panel.tsx
index adf05ca58d..cb07490c7f 100644
--- a/frontend/src/components/editor/chrome/panels/snippets-panel.tsx
+++ b/frontend/src/components/editor/chrome/panels/snippets-panel.tsx
@@ -31,7 +31,7 @@ import { cn } from "@/utils/cn";
import { HideInKioskMode } from "../../kiosk-mode";
import { ContributeSnippetButton } from "../components/contribute-snippet-button";
import { usePanelOrientation, usePanelSection } from "./panel-context";
-import { getSnippetDisplay } from "./snippet-display";
+import { getReadonlyCodeDisplay } from "@/core/cells/readonly-code-display";
const extensions = [EditorView.lineWrapping];
@@ -188,7 +188,7 @@ const SnippetViewer: React.FC<{ snippet: Snippet; onClose: () => void }> = ({
return null;
}
- const { language, value } = getSnippetDisplay(code);
+ const { language, code: displayCode } = getReadonlyCodeDisplay(code);
return (
<div
@@ -216,7 +216,7 @@ const SnippetViewer: React.FC<{ snippet: Snippet; onClose: () => void }> = ({
language={language}
className="cm border rounded overflow-hidden"
extensions={extensions}
- value={value}
+ value={displayCode}
readOnly={true}
/>
</Suspense>
diff --git a/frontend/src/components/editor/code/readonly-python-code.tsx b/frontend/src/components/editor/code/readonly-python-code.tsx
index 1e798b6a3a..4d13ac5473 100644
--- a/frontend/src/components/editor/code/readonly-python-code.tsx
+++ b/frontend/src/components/editor/code/readonly-python-code.tsx
@@ -1,5 +1,6 @@
/* Copyright 2026 Marimo. All rights reserved. */
+import { markdown } from "@codemirror/lang-markdown";
import { sql } from "@codemirror/lang-sql";
import CodeMirror, {
EditorView,
@@ -11,6 +12,7 @@ import { useAddCodeToNewCell } from "@/components/editor/cell/useAddCell";
import { Button } from "@/components/ui/button";
import { Tooltip } from "@/components/ui/tooltip";
import { toast } from "@/components/ui/use-toast";
+import type { LanguageAdapterType } from "@/core/codemirror/language/types";
import { customPythonLanguageSupport } from "@/core/codemirror/language/languages/python";
import { useTheme } from "@/theme/useTheme";
import { cn } from "@/utils/cn";
@@ -22,6 +24,18 @@ const pythonExtensions = [
EditorView.lineWrapping,
];
const sqlExtensions = [sql(), EditorView.lineWrapping];
+const markdownExtensions = [markdown(), EditorView.lineWrapping];
+
+function readonlyCodeExtensions(language: LanguageAdapterType) {
+ switch (language) {
+ case "sql":
+ return sqlExtensions;
+ case "markdown":
+ return markdownExtensions;
+ default:
+ return pythonExtensions;
+ }
+}
/**
* A readonly code component that can be used to display code in a readonly state.
@@ -42,7 +56,7 @@ export const ReadonlyCode = memo(
showHideCode?: boolean;
showCopyCode?: boolean;
insertNewCell?: boolean;
- language?: "python" | "sql";
+ language?: LanguageAdapterType;
} & ReactCodeMirrorProps,
) => {
const { theme } = useTheme();
@@ -84,7 +98,7 @@ export const ReadonlyCode = memo(
theme={theme === "dark" ? "dark" : "light"}
height="100%"
editable={!hideCode}
- extensions={language === "python" ? pythonExtensions : sqlExtensions}
+ extensions={readonlyCodeExtensions(language)}
value={code}
readOnly={true}
/>
diff --git a/frontend/src/components/editor/renderers/vertical-layout/__tests__/vertical-layout.test.ts b/frontend/src/components/editor/renderers/vertical-layout/__tests__/vertical-layout.test.ts
index 74080ae82f..2dfe285ac6 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/__tests__/vertical-layout.test.ts
+++ b/frontend/src/components/editor/renderers/vertical-layout/__tests__/vertical-layout.test.ts
@@ -2,11 +2,7 @@
/* oxlint-disable typescript/no-explicit-any */
import { describe, expect, it } from "vitest";
import type { OutputMessage } from "@/core/kernel/messages";
-import {
- groupCellsByColumn,
- readonlyDisplay,
- shouldHideCode,
-} from "../vertical-layout";
+import { groupCellsByColumn, shouldHideCode } from "../vertical-layout";
describe("groupCellsByColumn", () => {
it("should group cells by column and maintain order", () => {
@@ -175,25 +171,3 @@ describe("shouldHideCode", () => {
});
});
});
-
-describe("readonlyDisplay", () => {
- it("unwraps SQL cells to their inner query and marks them as sql", () => {
- const result = readonlyDisplay('my_table = mo.sql("""SELECT 1 AS id""")');
- expect(result.language).toBe("sql");
- expect(result.code).toBe("SELECT 1 AS id");
- });
-
- it("leaves plain Python cells untouched", () => {
- const code = "x = 1 + 2";
- const result = readonlyDisplay(code);
- expect(result.language).toBe("python");
- expect(result.code).toBe(code);
- });
-
- it("leaves non-SQL cells (e.g. markdown) unchanged as python", () => {
- const code = 'mo.md("""## Heading""")';
- const result = readonlyDisplay(code);
- expect(result.language).toBe("python");
- expect(result.code).toBe(code);
- });
-});
diff --git a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
index e8a933e89c..a84635072c 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
+++ b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
@@ -10,7 +10,6 @@ import {
Loader2Icon,
MoreHorizontalIcon,
} from "lucide-react";
-import { SQLParser } from "@marimo-team/smart-cells";
import type React from "react";
import { memo, useRef, useState } from "react";
import { z } from "zod";
@@ -30,6 +29,7 @@ import { outputIsLoading, outputIsStale } from "@/core/cells/cell";
import type { CellId } from "@/core/cells/ids";
import { isOutputEmpty } from "@/core/cells/outputs";
import type { CellData, CellRuntimeState } from "@/core/cells/types";
+import { getReadonlyCodeDisplay } from "@/core/cells/readonly-code-display";
import { MarkdownLanguageAdapter } from "@/core/codemirror/language/languages/markdown";
import { useResolvedMarimoConfig } from "@/core/config/config";
import { CSSClasses, KnownQueryParams } from "@/core/constants";
@@ -386,7 +386,7 @@ const VerticalCell = memo(
// Hide the code if it's pure markdown and there's an output, or if the code is empty
const hideCode = shouldHideCode(code, output);
// Only unwrap SQL when the code will actually be rendered.
- const display = hideCode ? null : readonlyDisplay(code);
+ const display = hideCode ? null : getReadonlyCodeDisplay(code);
return (
<div
@@ -500,20 +500,3 @@ export function shouldHideCode(code: string, output: OutputMessage | null) {
const hasOutput = output !== null && !isOutputEmpty(output);
return (isPureMarkdown && hasOutput) || code.trim() === "";
}
-
-const sqlParser = new SQLParser();
-
-/**
- * Unwrap SQL cells to their inner query so read-only views highlight them as
- * SQL instead of showing the raw `mo.sql(...)` wrapper.
- */
-export function readonlyDisplay(code: string): {
- code: string;
- language: "python" | "sql";
-} {
- const trimmed = code.trim();
- if (sqlParser.isSupported(trimmed)) {
- return { code: sqlParser.transformIn(trimmed).code, language: "sql" };
- }
- return { code, language: "python" };
-}
diff --git a/frontend/src/components/slides/slide-cell-view.tsx b/frontend/src/components/slides/slide-cell-view.tsx
index 73348b99a4..753711e085 100644
--- a/frontend/src/components/slides/slide-cell-view.tsx
+++ b/frontend/src/components/slides/slide-cell-view.tsx
@@ -26,7 +26,7 @@ import { connectionAtom } from "@/core/network/connection";
import { useTheme } from "@/theme/useTheme";
import { cn } from "@/utils/cn";
import { ReadonlyCode } from "../editor/code/readonly-python-code";
-import { languageAdapterFromCode } from "@/core/codemirror/language/extension";
+import { getReadonlyCodeDisplay } from "@/core/cells/readonly-code-display";
type RuntimeCell = CellRuntimeState & CellData;
@@ -188,10 +188,7 @@ export const SlideCellReadOnlyView = ({ cell }: { cell: RuntimeCell }) => {
const [userConfig] = useUserConfig();
const cellOutputPosition = userConfig.display.cell_output;
- const language = useMemo(() => {
- const adapter = languageAdapterFromCode(cell.code.trim());
- return adapter.type === "sql" ? "sql" : "python";
- }, [cell.code]);
+ const display = useMemo(() => getReadonlyCodeDisplay(cell.code), [cell.code]);
const output = (
<CellOutputSlide
@@ -203,7 +200,11 @@ export const SlideCellReadOnlyView = ({ cell }: { cell: RuntimeCell }) => {
const editor = (
<div className="marimo-cell">
- <ReadonlyCode code={cell.code} language={language} showHideCode={false} />
+ <ReadonlyCode
+ code={display.code}
+ language={display.language}
+ showHideCode={false}
+ />
</div>
);
diff --git a/frontend/src/core/cells/__tests__/readonly-code-display.test.ts b/frontend/src/core/cells/__tests__/readonly-code-display.test.ts
new file mode 100644
index 0000000000..bbe4715bcd
--- /dev/null
+++ b/frontend/src/core/cells/__tests__/readonly-code-display.test.ts
@@ -0,0 +1,46 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { describe, expect, it } from "vitest";
+import { getReadonlyCodeDisplay } from "../readonly-code-display";
+
+describe("getReadonlyCodeDisplay", () => {
+ it("unwraps SQL cells to their inner query and marks them as sql", () => {
+ const result = getReadonlyCodeDisplay(
+ 'my_table = mo.sql("""SELECT 1 AS id""")',
+ );
+ expect(result.language).toBe("sql");
+ expect(result.code).toBe("SELECT 1 AS id");
+ });
+
+ it("unwraps f-string SQL cells", () => {
+ const result = getReadonlyCodeDisplay(
+ 'my_table = mo.sql(f"""SELECT 1 AS id""")',
+ );
+ expect(result.language).toBe("sql");
+ expect(result.code).toBe("SELECT 1 AS id");
+ });
+
+ it("leaves plain Python cells untouched", () => {
+ const code = "x = 1 + 2\nprint(x)";
+ const result = getReadonlyCodeDisplay(code);
+ expect(result.language).toBe("python");
+ expect(result.code).toBe(code);
+ });
+
+ it("unwraps markdown cells to their inner content", () => {
+ const result = getReadonlyCodeDisplay('mo.md("""## Heading""")');
+ expect(result.language).toBe("markdown");
+ expect(result.code).toBe("## Heading");
+ });
+
+ it("treats empty or whitespace-only code as python", () => {
+ expect(getReadonlyCodeDisplay("")).toEqual({
+ code: "",
+ language: "python",
+ });
+ expect(getReadonlyCodeDisplay(" \n\t ")).toEqual({
+ code: " \n\t ",
+ language: "python",
+ });
+ });
+});
diff --git a/frontend/src/core/cells/readonly-code-display.ts b/frontend/src/core/cells/readonly-code-display.ts
new file mode 100644
index 0000000000..1ef2aee4b5
--- /dev/null
+++ b/frontend/src/core/cells/readonly-code-display.ts
@@ -0,0 +1,35 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { MarkdownParser, SQLParser } from "@marimo-team/smart-cells";
+import type { LanguageAdapterType } from "@/core/codemirror/language/types";
+
+export interface ReadonlyCodeDisplay {
+ code: string;
+ language: LanguageAdapterType;
+}
+
+const markdownParser = new MarkdownParser();
+const sqlParser = new SQLParser();
+
+/**
+ * Unwrap SQL and markdown cells so read-only views show inner content with the
+ * correct syntax highlighting instead of the raw Python wrapper.
+ */
+export function getReadonlyCodeDisplay(code: string): ReadonlyCodeDisplay {
+ const trimmed = code.trim();
+ if (!trimmed) {
+ return { code, language: "python" };
+ }
+
+ if (markdownParser.isSupported(trimmed)) {
+ return {
+ code: markdownParser.transformIn(trimmed).code,
+ language: "markdown",
+ };
+ }
+
+ if (sqlParser.isSupported(trimmed)) {
+ return { code: sqlParser.transformIn(trimmed).code, language: "sql" };
+ }
+ return { code, language: "python" };
+}