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: support hide_code config in read and static html (#520)

Myles Scolnickc1ac5cfb075c
diff --git a/frontend/src/components/data-table/download-actions.tsx b/frontend/src/components/data-table/download-actions.tsx
index 48254f27a4..17f6acbda7 100644
--- a/frontend/src/components/data-table/download-actions.tsx
+++ b/frontend/src/components/data-table/download-actions.tsx
@@ -1,7 +1,6 @@
/* Copyright 2023 Marimo. All rights reserved. */
import React from "react";
import { Button } from "../ui/button";
-import { CaretDownIcon } from "@radix-ui/react-icons";
import {
DropdownMenu,
DropdownMenuContent,
@@ -10,6 +9,7 @@ import {
} from "../ui/dropdown-menu";
import { toast } from "../ui/use-toast";
import { downloadByURL } from "@/utils/download";
+import { ChevronDownIcon } from "lucide-react";
export interface DownloadActionProps {
downloadAs: (req: { format: "csv" | "json" }) => Promise<string>;
@@ -23,7 +23,7 @@ const options = [
export const DownloadAs: React.FC<DownloadActionProps> = (props) => {
const button = (
<Button size="xs" variant="link">
- Download <CaretDownIcon className="w-3 h-3 ml-1" />
+ Download <ChevronDownIcon className="w-3 h-3 ml-1" />
</Button>
);
diff --git a/frontend/src/components/editor/cell/code/cell-editor.tsx b/frontend/src/components/editor/cell/code/cell-editor.tsx
index 0405bfa538..95eda45c94 100644
--- a/frontend/src/components/editor/cell/code/cell-editor.tsx
+++ b/frontend/src/components/editor/cell/code/cell-editor.tsx
@@ -27,8 +27,8 @@ import {
import { derefNotNull } from "@/utils/dereference";
import { LanguageToggle } from "./language-toggle";
import { cn } from "@/utils/cn";
-import { EyeOpenIcon } from "@radix-ui/react-icons";
import { saveCellConfig } from "@/core/network/requests";
+import { HideCodeButton } from "../../code/readonly-python-code";
export interface CellEditorProps
extends Pick<CellRuntimeState, "status">,
@@ -290,11 +290,7 @@ const CellEditorInternal = ({
/>
</div>
)}
- {hidden && (
- <div className="absolute inset-0 z-10" onClick={showCode}>
- <EyeOpenIcon className="hover-action w-5 h-5 text-muted-foreground cursor-pointer absolute left-1/2 top-1/2 transform -translate-x-1/2 -translate-y-1/2 opacity-80 hover:opacity-100" />
- </div>
- )}
+ {hidden && <HideCodeButton onClick={showCode} />}
<div
className={cn("cm", hidden && "opacity-20 h-8 overflow-hidden")}
ref={editorViewParentRef}
diff --git a/frontend/src/components/editor/code/readonly-python-code.tsx b/frontend/src/components/editor/code/readonly-python-code.tsx
index 568d27e071..25c1bd2ac5 100644
--- a/frontend/src/components/editor/code/readonly-python-code.tsx
+++ b/frontend/src/components/editor/code/readonly-python-code.tsx
@@ -1,10 +1,10 @@
/* Copyright 2023 Marimo. All rights reserved. */
-import { memo } from "react";
+import { memo, useState } from "react";
import CodeMirror, {
EditorView,
ReactCodeMirrorProps,
} from "@uiw/react-codemirror";
-import { CopyIcon } from "lucide-react";
+import { CopyIcon, EyeIcon } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Events } from "@/utils/events";
import { toast } from "@/components/ui/use-toast";
@@ -13,18 +13,27 @@ import { cn } from "@/utils/cn";
import { customPythonLanguageSupport } from "@/core/codemirror/language/python";
export const ReadonlyPythonCode = memo(
- (props: { className?: string; code: string } & ReactCodeMirrorProps) => {
+ (
+ props: {
+ className?: string;
+ code: string;
+ initiallyHideCode?: boolean;
+ } & ReactCodeMirrorProps
+ ) => {
const { theme } = useThemeForPlugin();
- const { code, className, ...rest } = props;
+ const { code, className, initiallyHideCode, ...rest } = props;
+ const [hideCode, setHideCode] = useState(initiallyHideCode);
+
return (
<div className={cn("relative hover-actions-parent w-full", className)}>
- <FloatingCopyButton text={code} />
+ {hideCode && <HideCodeButton onClick={() => setHideCode(false)} />}
+ {!hideCode && <FloatingCopyButton text={code} />}
<CodeMirror
{...rest}
- className="cm"
+ className={cn("cm", hideCode && "opacity-20 h-8 overflow-hidden")}
theme={theme === "dark" ? "dark" : "light"}
height="100%"
- editable={true}
+ editable={!hideCode}
extensions={[customPythonLanguageSupport(), EditorView.lineWrapping]}
value={code}
readOnly={true}
@@ -52,3 +61,11 @@ const FloatingCopyButton = (props: { text: string }) => {
</Button>
);
};
+
+export const HideCodeButton = (props: { onClick: () => void }) => {
+ return (
+ <div className="absolute inset-0 z-10" onClick={props.onClick}>
+ <EyeIcon className="hover-action w-5 h-5 text-muted-foreground cursor-pointer absolute left-1/2 top-1/2 transform -translate-x-1/2 -translate-y-1/2 opacity-80 hover:opacity-100" />
+ </div>
+ );
+};
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 6e6567c17f..00bef2fa24 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
+++ b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
@@ -1,6 +1,6 @@
/* Copyright 2023 Marimo. All rights reserved. */
import React, { memo, useRef, useState } from "react";
-import { CellRuntimeState } from "@/core/cells/types";
+import { CellConfig, CellRuntimeState } from "@/core/cells/types";
import { CellId, HTMLCellId } from "@/core/cells/ids";
import { OutputArea } from "@/components/editor/Output";
import { ICellRendererPlugin, ICellRendererProps } from "../types";
@@ -36,6 +36,7 @@ const VerticalLayoutRenderer: React.FC<VerticalLayoutProps> = ({
output={cell.output}
status={cell.status}
code={cell.code}
+ config={cell.config}
stopped={cell.stopped}
showCode={showCode && canShowCode}
errored={cell.errored}
@@ -79,6 +80,7 @@ interface VerticalCellProps
| "runStartTimestamp"
> {
cellId: CellId;
+ config: CellConfig;
code: string;
mode: AppMode;
showCode: boolean;
@@ -91,6 +93,7 @@ const VerticalCell = memo(
status,
stopped,
errored,
+ config,
interrupted,
runStartTimestamp,
code,
@@ -131,7 +134,10 @@ const VerticalCell = memo(
stale={outputStale}
/>
<div className="tray">
- <ReadonlyPythonCode code={code} />
+ <ReadonlyPythonCode
+ initiallyHideCode={config.hide_code}
+ code={code}
+ />
</div>
</div>
);
diff --git a/frontend/src/core/static/__tests__/__snapshots__/download-html.test.ts.snap b/frontend/src/core/static/__tests__/__snapshots__/download-html.test.ts.snap
index ba2538ce10..80e47c940d 100644
--- a/frontend/src/core/static/__tests__/__snapshots__/download-html.test.ts.snap
+++ b/frontend/src/core/static/__tests__/__snapshots__/download-html.test.ts.snap
@@ -78,7 +78,7 @@ exports[`download-html > should construct html correctly 1`] = `
window.__MARIMO_STATIC__.notebookState = {
cellIds: ["1"],
cellData: {
- 1: "JTdCJTIyaWQlMjIlM0ElMjIxJTIyJTJDJTIyY29uZmlnJTIyJTNBJTdCJTdEJTJDJTIybmFtZSUyMiUzQSUyMmNlbGwlMjIlMkMlMjJjb2RlJTIyJTNBJTIyY29kZSUyMiUyQyUyMmVkaXRlZCUyMiUzQWZhbHNlJTJDJTIybGFzdENvZGVSdW4lMjIlM0FudWxsJTJDJTIyc2VyaWFsaXplZEVkaXRvclN0YXRlJTIyJTNBbnVsbCU3RA==",
+ 1: "JTdCJTIyaWQlMjIlM0ElMjIxJTIyJTJDJTIyY29uZmlnJTIyJTNBJTdCJTIyaGlkZV9jb2RlJTIyJTNBdHJ1ZSU3RCUyQyUyMm5hbWUlMjIlM0ElMjJjZWxsJTIyJTJDJTIyY29kZSUyMiUzQSUyMmNvZGUlMjIlMkMlMjJlZGl0ZWQlMjIlM0FmYWxzZSUyQyUyMmxhc3RDb2RlUnVuJTIyJTNBbnVsbCUyQyUyMnNlcmlhbGl6ZWRFZGl0b3JTdGF0ZSUyMiUzQW51bGwlN0Q=",
},
cellRuntime: {
1: "JTdCJTIyb3V0bGluZSUyMiUzQW51bGwlMkMlMjJvdXRwdXQlMjIlM0FudWxsJTJDJTIyY29uc29sZU91dHB1dHMlMjIlM0ElNUIlNUQlMkMlMjJzdGF0dXMlMjIlM0ElMjJpZGxlJTIyJTJDJTIyaW50ZXJydXB0ZWQlMjIlM0FmYWxzZSUyQyUyMmVycm9yZWQlMjIlM0FmYWxzZSUyQyUyMnN0b3BwZWQlMjIlM0FmYWxzZSUyQyUyMnJ1bkVsYXBzZWRUaW1lTXMlMjIlM0FudWxsJTJDJTIycnVuU3RhcnRUaW1lc3RhbXAlMjIlM0FudWxsJTdE",
diff --git a/frontend/src/core/static/__tests__/download-html.test.ts b/frontend/src/core/static/__tests__/download-html.test.ts
index 39d9f82cb3..ec779961ba 100644
--- a/frontend/src/core/static/__tests__/download-html.test.ts
+++ b/frontend/src/core/static/__tests__/download-html.test.ts
@@ -87,6 +87,9 @@ describe("download-html", () => {
id: cellId,
name: "cell",
code: "code",
+ config: {
+ hide_code: true,
+ },
}),
},
cellRuntime: { [cellId]: createCellRuntimeState() },