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: hide code logic for html export (#5685)

Myles Scolnick65c9f8a2f10b
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 f7fb8a49c7..b78dcee304 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
@@ -1,7 +1,8 @@
/* Copyright 2024 Marimo. All rights reserved. */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, expect, it } from "vitest";
-import { groupCellsByColumn } from "../vertical-layout";
+import type { OutputMessage } from "@/core/kernel/messages";
+import { groupCellsByColumn, shouldHideCode } from "../vertical-layout";
describe("groupCellsByColumn", () => {
it("should group cells by column and maintain order", () => {
@@ -38,3 +39,135 @@ describe("groupCellsByColumn", () => {
]);
});
});
+
+describe("shouldHideCode", () => {
+ // Helper function to create valid OutputMessage
+ const createOutput = (
+ data: any = "some output",
+ mimetype: OutputMessage["mimetype"] = "text/plain",
+ ): OutputMessage => ({
+ channel: "output",
+ mimetype,
+ data,
+ timestamp: Date.now(),
+ });
+
+ describe("empty/whitespace code", () => {
+ it("should return true for empty string", () => {
+ expect(shouldHideCode("", null)).toBe(true);
+ expect(shouldHideCode("", createOutput())).toBe(true);
+ });
+
+ it("should return true for whitespace-only strings", () => {
+ expect(shouldHideCode(" ", null)).toBe(true);
+ expect(shouldHideCode(" ", createOutput())).toBe(true);
+ expect(shouldHideCode("\n\n", null)).toBe(true);
+ expect(shouldHideCode("\n\n", createOutput())).toBe(true);
+ expect(shouldHideCode(" \n \t ", null)).toBe(true);
+ expect(shouldHideCode(" \n \t ", createOutput())).toBe(true);
+ expect(shouldHideCode("\r\n \r\n", null)).toBe(true);
+ expect(shouldHideCode("\r\n \r\n", createOutput())).toBe(true);
+ });
+ });
+
+ describe("markdown code with output", () => {
+ it("should return true for basic markdown with output", () => {
+ const markdownCode = 'mo.md("# Hello World")';
+ expect(shouldHideCode(markdownCode, createOutput())).toBe(true);
+ });
+
+ it("should return true for multi-line markdown with output", () => {
+ const markdownCode = 'mo.md("""\n# Hello\nThis is content\n""")';
+ expect(shouldHideCode(markdownCode, createOutput())).toBe(true);
+ });
+
+ it("should return true for f-string markdown with output", () => {
+ const markdownCode = 'mo.md(f"# Hello {name}")';
+ expect(shouldHideCode(markdownCode, createOutput())).toBe(true);
+ });
+
+ it("should return true for raw string markdown with output", () => {
+ const markdownCode = 'mo.md(r"# Hello World")';
+ expect(shouldHideCode(markdownCode, createOutput())).toBe(true);
+ });
+
+ it("should return true for rf-string markdown with output", () => {
+ const markdownCode = 'mo.md(rf"# Hello {name}")';
+ expect(shouldHideCode(markdownCode, createOutput())).toBe(true);
+ });
+
+ it("should return true for markdown with various output types", () => {
+ const markdownCode = 'mo.md("# Hello")';
+
+ expect(
+ shouldHideCode(markdownCode, createOutput("text", "text/plain")),
+ ).toBe(true);
+ expect(
+ shouldHideCode(
+ markdownCode,
+ createOutput({ key: "value" }, "application/json"),
+ ),
+ ).toBe(true);
+ expect(
+ shouldHideCode(
+ markdownCode,
+ createOutput("<div>html</div>", "text/html"),
+ ),
+ ).toBe(true);
+ expect(
+ shouldHideCode(markdownCode, createOutput("base64image", "image/png")),
+ ).toBe(true);
+ });
+ });
+
+ describe("markdown code without output", () => {
+ it("should return false for different markdown formats without output", () => {
+ const markdownCodes = [
+ 'mo.md("# Hello")',
+ 'mo.md(f"# Hello {name}")',
+ 'mo.md(r"# Hello")',
+ 'mo.md(rf"# Hello {name}")',
+ 'mo.md("""\n# Multi\nline\n""")',
+ "mo.md('''# Single quotes''')",
+ ];
+
+ markdownCodes.forEach((code) => {
+ expect(shouldHideCode(code, null)).toBe(false);
+ expect(shouldHideCode(code, createOutput(""))).toBe(false);
+ });
+ });
+ });
+
+ describe("non-markdown code", () => {
+ it("should return false for Python code with output", () => {
+ const pythonCode = 'print("Hello World")';
+ expect(shouldHideCode(pythonCode, createOutput())).toBe(false);
+ });
+
+ it("should return false for Python code without output", () => {
+ const pythonCode = 'print("Hello World")';
+ expect(shouldHideCode(pythonCode, null)).toBe(false);
+ expect(shouldHideCode(pythonCode, createOutput(""))).toBe(false);
+ });
+ });
+
+ describe("combination edge cases", () => {
+ it("should prioritize empty code over markdown detection", () => {
+ // Even if we somehow had markdown that was just whitespace, empty code should win
+ expect(shouldHideCode(" ", createOutput())).toBe(true);
+ expect(shouldHideCode("\n\t \n", createOutput())).toBe(true);
+ });
+
+ it("should handle mo.md with only whitespace content", () => {
+ const emptyMarkdown = 'mo.md(" ")';
+ expect(shouldHideCode(emptyMarkdown, createOutput())).toBe(true);
+ expect(shouldHideCode(emptyMarkdown, null)).toBe(false);
+ });
+
+ it("should handle mo.md with newlines only", () => {
+ const newlineMarkdown = 'mo.md("\\n\\n\\n")';
+ expect(shouldHideCode(newlineMarkdown, createOutput())).toBe(true);
+ expect(shouldHideCode(newlineMarkdown, null)).toBe(false);
+ });
+ });
+});
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 49fe3b1369..3f696398fe 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
+++ b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
@@ -25,10 +25,12 @@ import {
} from "@/components/ui/dropdown-menu";
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 { MarkdownLanguageAdapter } from "@/core/codemirror/language/languages/markdown";
import { useResolvedMarimoConfig } from "@/core/config/config";
import { KnownQueryParams } from "@/core/constants";
+import type { OutputMessage } from "@/core/kernel/messages";
import { showCodeInRunModeAtom } from "@/core/meta/state";
import { isErrorMime } from "@/core/mime";
import { type AppMode, kioskModeAtom } from "@/core/mode";
@@ -345,7 +347,8 @@ const VerticalCell = memo(
/>
);
- const isCodeEmpty = code.trim() === "";
+ // Hide the code if it's pure markdown and there's an output, or if the code is empty
+ const hideCode = shouldHideCode(code, output);
return (
<div
@@ -355,8 +358,7 @@ const VerticalCell = memo(
{...cellDomProps(cellId, name)}
>
{cellOutputArea === "above" && outputArea}
- {/* Hide code if it's empty or pure markdown */}
- {!isPureMarkdown && !isCodeEmpty && (
+ {!hideCode && (
<div className="tray">
<ReadonlyCode
initiallyHideCode={config.hide_code || kiosk}
@@ -435,3 +437,15 @@ export function groupCellsByColumn(
// Sort columns by index
return [...cellsByColumn.entries()].sort(([a], [b]) => a - b);
}
+
+/**
+ * Determine if the code should be hidden.
+ *
+ * This is used to hide the code if it's pure markdown and there's an output,
+ * or if the code is empty.
+ */
+export function shouldHideCode(code: string, output: OutputMessage | null) {
+ const isPureMarkdown = new MarkdownLanguageAdapter().isSupported(code);
+ const hasOutput = output !== null && !isOutputEmpty(output);
+ return (isPureMarkdown && hasOutput) || code.trim() === "";
+}