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.

Fix: readonly view selects sql language (#10074)

Brook Pattenbf7487fccba8
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 2dfe285ac6..74080ae82f 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,7 +2,11 @@
/* oxlint-disable typescript/no-explicit-any */
import { describe, expect, it } from "vitest";
import type { OutputMessage } from "@/core/kernel/messages";
-import { groupCellsByColumn, shouldHideCode } from "../vertical-layout";
+import {
+ groupCellsByColumn,
+ readonlyDisplay,
+ shouldHideCode,
+} from "../vertical-layout";
describe("groupCellsByColumn", () => {
it("should group cells by column and maintain order", () => {
@@ -171,3 +175,25 @@ 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 6b244c873d..e8a933e89c 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
+++ b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
@@ -10,6 +10,7 @@ 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";
@@ -384,6 +385,8 @@ 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);
return (
<div
@@ -393,11 +396,12 @@ const VerticalCell = memo(
{...cellDomProps(cellId, name)}
>
{cellOutputArea === "above" && outputArea}
- {!hideCode && (
+ {display && (
<div className="tray">
<ReadonlyCode
initiallyHideCode={config.hide_code || kiosk}
- code={code}
+ code={display.code}
+ language={display.language}
/>
</div>
)}
@@ -496,3 +500,20 @@ 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" };
+}