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.

improvement: option to show code in static html (#440)

Myles Scolnickf56cf20f69cf
diff --git a/frontend/e2e-tests/helper.ts b/frontend/e2e-tests/helper.ts
index 1b78631cc8..7e9f8ec62f 100644
--- a/frontend/e2e-tests/helper.ts
+++ b/frontend/e2e-tests/helper.ts
@@ -112,6 +112,14 @@ export async function exportAsHTMLAndTakeScreenshot(page: Page) {
waitUntil: "networkidle",
});
await takeScreenshot(exportPage, path);
+
+ // Toggle code
+ await exportPage.getByTestId("show-code").click();
+ // wait 100ms for the code to be shown
+ await exportPage.waitForTimeout(100);
+
+ // Take screenshot of code
+ await takeScreenshot(exportPage, `code-${path}`);
}
export async function exportAsPNG(page: Page) {
diff --git a/frontend/postcss.config.js b/frontend/postcss.config.js
index 04810b5241..94776a98f5 100644
--- a/frontend/postcss.config.js
+++ b/frontend/postcss.config.js
@@ -1,7 +1,9 @@
+/* Copyright 2023 Marimo. All rights reserved. */
module.exports = {
plugins: {
+ "tailwindcss/nesting": {},
tailwindcss: {},
- autoprefixer: {},
...(process.env.NODE_ENV === "production" ? { cssnano: {} } : {}),
+ autoprefixer: {},
},
};
diff --git a/frontend/src/components/editor/Cell.tsx b/frontend/src/components/editor/Cell.tsx
index 8b9378b047..36d8833afd 100644
--- a/frontend/src/components/editor/Cell.tsx
+++ b/frontend/src/components/editor/Cell.tsx
@@ -251,6 +251,7 @@ const CellComponent = (
const className = clsx("Cell", "hover-actions-parent", {
published: !editing,
+ interactive: editing,
"needs-run": needsRun,
"has-error": errored,
stopped: stopped,
diff --git a/frontend/src/components/editor/code/readonly-python-code.tsx b/frontend/src/components/editor/code/readonly-python-code.tsx
new file mode 100644
index 0000000000..f730173654
--- /dev/null
+++ b/frontend/src/components/editor/code/readonly-python-code.tsx
@@ -0,0 +1,54 @@
+/* Copyright 2023 Marimo. All rights reserved. */
+import { memo } from "react";
+import { python } from "@codemirror/lang-python";
+import CodeMirror, {
+ EditorView,
+ ReactCodeMirrorProps,
+} from "@uiw/react-codemirror";
+import { CopyIcon } from "lucide-react";
+import { Button } from "@/components/ui/button";
+import { Events } from "@/utils/events";
+import { toast } from "@/components/ui/use-toast";
+import { useThemeForPlugin } from "@/theme/useTheme";
+import { cn } from "@/utils/cn";
+
+export const ReadonlyPythonCode = memo(
+ (props: { className?: string; code: string } & ReactCodeMirrorProps) => {
+ const { theme } = useThemeForPlugin();
+ const { code, className, ...rest } = props;
+ return (
+ <div className={cn("relative hover-actions-parent w-full", className)}>
+ <FloatingCopyButton text={code} />
+ <CodeMirror
+ {...rest}
+ className="cm"
+ theme={theme === "dark" ? "dark" : "light"}
+ height="100%"
+ editable={true}
+ extensions={[python(), EditorView.lineWrapping]}
+ value={code}
+ readOnly={true}
+ />
+ </div>
+ );
+ }
+);
+ReadonlyPythonCode.displayName = "ReadonlyPythonCode";
+
+const FloatingCopyButton = (props: { text: string }) => {
+ const copy = Events.stopPropagation(() => {
+ navigator.clipboard.writeText(props.text);
+ toast({ title: "Copied to clipboard" });
+ });
+
+ return (
+ <Button
+ onClick={copy}
+ className="absolute top-0 right-0 m-2 z-10 hover-action"
+ size="xs"
+ variant="secondary"
+ >
+ <CopyIcon size={14} strokeWidth={1.5} />
+ </Button>
+ );
+};
diff --git a/frontend/src/components/editor/inputs/Inputs.styles.ts b/frontend/src/components/editor/inputs/Inputs.styles.ts
index d01fa60859..e44c08e5fa 100644
--- a/frontend/src/components/editor/inputs/Inputs.styles.ts
+++ b/frontend/src/components/editor/inputs/Inputs.styles.ts
@@ -3,7 +3,7 @@ import { cva } from "class-variance-authority";
import "./Inputs.css";
export const button = cva(
- "flex items-center justify-center m-0 leading-none font-medium border border-foreground/10 shadow-xsSolid shadow-foreground/10 hover:cursor-pointer focus:shadow-md dark:border-border",
+ "flex items-center justify-center m-0 leading-none font-medium border border-foreground/10 shadow-xsSolid shadow-foreground/10 hover:cursor-pointer focus:shadow-md dark:border-border text-sm",
{
variants: {
color: {
diff --git a/frontend/src/components/editor/renderers/plugins.ts b/frontend/src/components/editor/renderers/plugins.ts
index abfc1b4133..9a35f22ae0 100644
--- a/frontend/src/components/editor/renderers/plugins.ts
+++ b/frontend/src/components/editor/renderers/plugins.ts
@@ -2,11 +2,13 @@
import { GridLayoutPlugin } from "./grid-layout/plugin";
import { ICellRendererPlugin, LayoutType } from "./types";
import { CellData } from "@/core/cells/types";
+import { VerticalLayoutPlugin } from "./vertical-layout/vertical-layout";
// If more renderers are added, we may want to consider lazy loading them.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const cellRendererPlugins: Array<ICellRendererPlugin<any, any>> = [
GridLayoutPlugin,
+ VerticalLayoutPlugin,
];
export function deserializeLayout(
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 d3520d3d76..422be5d6b9 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
+++ b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
@@ -1,14 +1,17 @@
/* Copyright 2023 Marimo. All rights reserved. */
-import { memo, useRef } from "react";
+import { memo, useRef, useState } from "react";
import { CellRuntimeState } from "@/core/cells/types";
import { CellId, HTMLCellId } from "@/core/cells/ids";
import { OutputArea } from "@/components/editor/Output";
-import clsx from "clsx";
import { ICellRendererPlugin, ICellRendererProps } from "../types";
import { VerticalLayoutWrapper } from "./vertical-layout-wrapper";
import { z } from "zod";
import { useDelayVisibility } from "./useDelayVisiblity";
import { AppMode } from "@/core/mode";
+import { ReadonlyPythonCode } from "@/components/editor/code/readonly-python-code";
+import { Code2Icon } from "lucide-react";
+import { cn } from "@/utils/cn";
+import { Button } from "@/components/ui/button";
type VerticalLayout = null;
type VerticalLayoutProps = ICellRendererProps<VerticalLayout>;
@@ -19,24 +22,37 @@ const VerticalLayoutRenderer: React.FC<VerticalLayoutProps> = ({
mode,
}) => {
const { invisible } = useDelayVisibility(cells.length, mode);
+ const [showCode, setShowCode] = useState(false);
+ // Show code if there is at least one cell with code
+ const canShowCode = mode === "read" && cells.some((cell) => cell.code);
return (
- <VerticalLayoutWrapper
- className="sm:pt-8"
- invisible={invisible}
- appConfig={appConfig}
- >
+ <VerticalLayoutWrapper invisible={invisible} appConfig={appConfig}>
{cells.map((cell) => (
<VerticalCell
key={cell.id}
cellId={cell.id}
output={cell.output}
status={cell.status}
+ code={cell.code}
stopped={cell.stopped}
+ showCode={showCode && canShowCode}
errored={cell.errored}
mode={mode}
interrupted={cell.interrupted}
/>
))}
+ {canShowCode && (
+ <div className="absolute m-4 left-0 top-0">
+ <Button
+ variant="secondary"
+ onClick={() => setShowCode((prev) => !prev)}
+ data-testid="show-code"
+ >
+ <Code2Icon className="w-4 h-4 mr-2" />
+ {showCode ? "Hide code" : "Show code"}
+ </Button>
+ </div>
+ )}
</VerticalLayoutWrapper>
);
};
@@ -47,7 +63,9 @@ interface VerticalCellProps
"output" | "status" | "stopped" | "errored" | "interrupted"
> {
cellId: CellId;
+ code: string;
mode: AppMode;
+ showCode: boolean;
}
const VerticalCell = memo(
@@ -58,19 +76,41 @@ const VerticalCell = memo(
stopped,
errored,
interrupted,
+ code,
+ showCode,
mode,
}: VerticalCellProps) => {
const cellRef = useRef<HTMLDivElement>(null);
const loading = status === "running" || status === "queued";
- const className = clsx("Cell", "hover-actions-parent", {
- published: true,
+ const className = cn("Cell", "hover-actions-parent", {
+ published: !showCode,
+ interactive: mode === "edit",
"has-error": errored,
stopped: stopped,
});
const HTMLId = HTMLCellId.create(cellId);
const hidden = errored || interrupted || stopped;
+
+ // Read mode and show code
+ if (mode === "read" && showCode) {
+ return (
+ <div tabIndex={-1} id={HTMLId} ref={cellRef} className={className}>
+ <OutputArea
+ allowExpand={true}
+ output={output}
+ className="output-area"
+ cellId={cellId}
+ stale={loading && !interrupted}
+ />
+ <div className="tray">
+ <ReadonlyPythonCode code={code} />
+ </div>
+ </div>
+ );
+ }
+
return hidden ? null : (
<div tabIndex={-1} id={HTMLId} ref={cellRef} className={className}>
<OutputArea
diff --git a/frontend/src/css/Cell.css b/frontend/src/css/Cell.css
index 376ddb36b4..9ea93524c2 100644
--- a/frontend/src/css/Cell.css
+++ b/frontend/src/css/Cell.css
@@ -1,3 +1,10 @@
+/*
+ * Cell can have many states:
+ * - interactive (can hover, focus, click), while in edit mode
+ * - stale
+ * - error
+ * - disabled
+ * */
.Cell {
position: relative;
margin-bottom: 21px;
@@ -7,6 +14,212 @@
border: 1px solid transparent;
box-shadow: var(--light-shadow);
+ &:focus-visible {
+ /* focus-visible outlines the entire cell body in black, but the cell's
+ * body is an irregular shape because of pseudo-elements that extend
+ * its hit-box / hover area. */
+ outline: none;
+ }
+
+ /* Interactive */
+ &.interactive {
+ /* Only restrain output length in edit mode. */
+ .output-area,
+ .console-output-area {
+ max-height: 610px;
+ overflow: auto;
+ }
+
+ .output-area {
+ border-top-left-radius: 8px;
+ border-top-right-radius: 8px;
+ }
+
+ &:hover {
+ box-shadow: var(--medium-shadow);
+ }
+
+ &:focus-within {
+ border: 1px solid var(--gray-5);
+
+ /* a sharp box shadow with a slight blur to outline the element */
+ box-shadow: var(--heavy-shadow);
+
+ /* a little bit of motion
+ *
+ * Note: we use left/top instead of transform because transform creates a new
+ * stacking context and makes completion tooltip appear below surrounding
+ * cells/outputs. See
+ *
+ * https://stackoverflow.com/questions/20851452/z-index-is-canceled-by-setting-transformrotate,
+ * https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context
+ * */
+ left: -1px;
+ top: -1px;
+ }
+
+ .cm {
+ border-radius: 8px;
+ }
+ }
+
+ /* Stale styles for Cell */
+ &.stale,
+ &.disabled.stale {
+ .output-area,
+ .cm-gutters,
+ .cm {
+ background-color: var(--gray-2);
+ opacity: 0.5;
+ }
+ }
+
+ /* Disabled styles for Cell */
+ &.disabled,
+ &.disabled.has-error,
+ &.disabled:hover,
+ &.disabled.has-error:hover {
+ box-shadow: var(--light-shadow);
+
+ &:focus-within {
+ box-shadow: var(--heavy-shadow);
+ }
+
+ .output-area,
+ .cm-gutters,
+ .cm {
+ background-color: var(--gray-1);
+ opacity: 0.5;
+ }
+ }
+
+ /* Needs Run */
+ &.needs-run {
+ box-shadow: var(--light-shadow-stale);
+
+ &:hover {
+ box-shadow: var(--medium-shadow-stale);
+ }
+
+ &:focus-within:hover,
+ &:focus-within {
+ box-shadow: var(--heavy-shadow-stale);
+ }
+
+ &:focus-within .cm-editor {
+ box-shadow: none;
+ border: 1px solid transparent;
+ }
+
+ .output-area {
+ border-bottom: 1px solid var(--stale-color);
+ }
+
+ .RunButton {
+ visibility: visible;
+ }
+ }
+
+ /* Error styles for Cell */
+ &.has-error,
+ &.error-outline {
+ outline: 1px solid var(--red-4);
+ box-shadow: var(--light-shadow-error);
+ }
+
+ &.has-error:hover {
+ box-shadow: var(--medium-shadow-error);
+ }
+
+ &.has-error:focus-within,
+ &.has-error:focus-within:hover {
+ box-shadow: var(--heavy-shadow-error);
+ }
+
+ &.error-outline,
+ &.error-outline:focus-within {
+ box-shadow: 8px 8px 0 0 hsl(var(--error) / 80%);
+ background-color: var(--red-2);
+ }
+
+ /* Focus state */
+ &.focus-outline {
+ border-color: var(--blue-8);
+
+ /* custom shadow until our theme overrides can support colored shadow */
+ --tw-shadow: 0 4px 0px -1px var(--blue-8), 0 2px 4px -2px var(--blue-8) !important;
+
+ @apply shadow-lg;
+ }
+
+ /* Published - when its just the output */
+ &.published {
+ margin-bottom: 0;
+ border: none;
+ box-shadow: none;
+
+ .output-area {
+ /* flow-root interferes with margin collapsing, but appears to be unneeded
+ * when cell outlines are hidden; clear:both is sufficient.
+ *
+ * if developers just use css-grid instead of float, this won't matter.
+ * */
+ display: block;
+ padding-top: 0;
+ padding-bottom: 0;
+ border: none;
+ box-shadow: none;
+ }
+
+ &:hover {
+ border: none;
+ box-shadow: none;
+ }
+
+ &:focus-within {
+ transform: none;
+ }
+ }
+
+ /* -------------------------- Shoulders/Buttons ---------------------------- */
+ .shoulder-left {
+ position: absolute;
+ left: -34px;
+ display: flex;
+ flex-direction: column;
+ gap: 4px;
+ justify-content: center;
+ height: 100%;
+ z-index: 2;
+ }
+
+ .shoulder-elem-top {
+ margin-bottom: auto;
+ }
+
+ .shoulder-elem-bottom {
+ vertical-align: bottom;
+ }
+
+ .shoulder-right {
+ display: inline-flex;
+ flex-direction: column;
+ justify-content: flex-end;
+ height: 100%;
+ position: absolute;
+ right: -98px;
+ width: 80px;
+ gap: 4px;
+ z-index: 2;
+ }
+
+ .shoulder-bottom {
+ position: absolute;
+ bottom: -2px;
+ right: 2px;
+ z-index: 2; /* lie above editor */
+ }
+
@apply bg-background;
}
@@ -14,183 +227,69 @@
@apply border-border;
}
-/* Background determined by disconnected gradient/noise. */
-#App.disconnected .Cell,
-#App.disconnected .console-output-area,
-#App.disconnected .cm .cm-gutters,
-#App.disconnected .Cell .cm-editor.cm-focused .cm-activeLineGutter,
-#App.disconnected .Cell .cm-editor.cm-focused .cm-activeLine,
-#App.disconnected .Cell .ShoulderButton {
- background-color: transparent;
-}
-
-/* Hide when disconnected. */
-#App.disconnected .cell-running-icon,
-#App.disconnected .cell-queued-icon,
-#App.disconnected .elapsed-time {
- visibility: hidden;
- animation: none;
-}
-
-#App.disconnected .console-output-area {
- border-color: transparent;
-}
-
-.Cell:not(.disabled):hover {
- box-shadow: var(--medium-shadow);
-}
-
-.Cell.disabled .output-area,
-.Cell.disabled.stale .output-area,
-.Cell.disabled .cm-gutters,
-.Cell.disabled.stale .cm-gutters,
-.Cell.disabled .cm,
-.Cell.disabled.stale .cm {
- background-color: var(--gray-2);
- opacity: 0.5;
-}
-
-.Cell.stale .output-area,
-.Cell.stale .cm,
-.Cell.stale .cm-gutters {
- background-color: var(--gray-1);
- opacity: 0.5;
-}
-
-.Cell.has-error,
-.Cell.error-outline {
- outline: 1px solid var(--red-4);
- box-shadow: var(--light-shadow-error);
-}
-
-.Cell.has-error:hover {
- box-shadow: var(--medium-shadow-error);
-}
-
-.Cell:not(.published).has-error:focus-within {
- box-shadow: var(--heavy-shadow-error);
-}
-
-.Cell.error-outline,
-.Cell:not(.published).error-outline:focus-within {
- box-shadow: 8px 8px 0 0 hsl(var(--error) / 80%);
- background-color: var(--red-2);
-}
+#App.disconnected {
+ /* Background determined by disconnected gradient/noise. */
+ .Cell,
+ .console-output-area,
+ .cm .cm-gutters,
+ .Cell .cm-editor.cm-focused .cm-activeLineGutter,
+ .Cell .cm-editor.cm-focused .cm-activeLine,
+ .Cell .ShoulderButton {
+ background-color: transparent;
+ }
-.Cell.focus-outline {
- border-color: var(--blue-8);
-
- /* custom shadow until our theme overrides can support colored shadow */
- --tw-shadow: 0 4px 0px -1px var(--blue-8), 0 2px 4px -2px var(--blue-8) !important;
-
- @apply shadow-lg;
-}
+ /* Hide when disconnected. */
+ .cell-running-icon,
+ .cell-queued-icon,
+ .elapsed-time {
+ visibility: hidden;
+ animation: none;
+ }
-.Cell:focus-visible {
- /* focus-visible outlines the entire cell body in black, but the cell's
- * body is an irregular shape because of psuedo-elements that extend
- * its hit-box / hover area. */
- outline: none;
-}
-
-.Cell:not(.published):focus-within {
- border: 1px solid var(--gray-5);
-
- /* a sharp box shadow with a slight blur to outline the element */
- box-shadow: var(--heavy-shadow);
-
- /* a little bit of motion
- *
- * Note: we use left/top instead of transform because transform creates a new
- * stacking context and makes completion tooltip appear below surrounding
- * cells/outputs. See
- *
- * https://stackoverflow.com/questions/20851452/z-index-is-canceled-by-setting-transformrotate,
- * https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context
- * */
- left: -1px;
- top: -1px;
+ .console-output-area {
+ border-color: transparent;
+ }
}
.tray {
display: flex;
position: relative;
-}
-.tray:first-child .cm-editor {
- border-top-left-radius: 9px;
- border-top-right-radius: 9px;
-}
-
-.tray:last-child .cm-editor {
- border-bottom-left-radius: 9px;
- border-bottom-right-radius: 9px;
-}
+ &:first-child .cm-editor {
+ border-top-left-radius: 9px;
+ border-top-right-radius: 9px;
+ }
-:root {
- /* In wide mode, this will overextend, but that's fine */
- --gutter-width: calc(50vw - (var(--content-width) / 2));
-}
+ &:last-child .cm-editor {
+ border-bottom-left-radius: 9px;
+ border-bottom-right-radius: 9px;
+ }
-/* expand the hover area left and right */
-.tray::before,
-.tray::after {
- content: "";
- position: absolute;
- width: var(--gutter-width);
- max-width: var(--gutter-width);
- height: 100%;
-}
+ /* expand the hover area left and right */
+ &::before,
+ &::after {
+ content: "";
+ position: absolute;
+ width: var(--gutter-width);
+ max-width: var(--gutter-width);
+ height: 100%;
+ }
-.tray::before {
- left: calc(var(--gutter-width) * -1);
-}
+ &::before {
+ left: calc(var(--gutter-width) * -1);
+ }
-.tray::after {
- right: calc(var(--gutter-width) * -1);
+ &::after {
+ right: calc(var(--gutter-width) * -1);
+ }
}
-/* -------------------------- Shoulders/Buttons ---------------------------- */
-.Cell .shoulder-left {
- position: absolute;
- left: -34px;
- display: flex;
- flex-direction: column;
- gap: 4px;
- justify-content: center;
- height: 100%;
- z-index: 2;
-}
-
-.shoulder-elem-top {
- margin-bottom: auto;
-}
-
-.shoulder-elem-bottom {
- vertical-align: bottom;
-}
-
-.Cell .shoulder-right {
- display: inline-flex;
- flex-direction: column;
- justify-content: flex-end;
- height: 100%;
- position: absolute;
- right: -98px;
- width: 80px;
- gap: 4px;
- z-index: 2;
-}
-
-.Cell .shoulder-bottom {
- position: absolute;
- bottom: -2px;
- right: 2px;
- z-index: 2; /* lie above editor */
+:root {
+ /* In wide mode, this will overextend, but that's fine */
+ --gutter-width: calc(50vw - (var(--content-width) / 2));
}
/* ------------------------ CodeMirror Editor ----------------------------- */
-
.cm {
width: 100%;
}
@@ -205,37 +304,44 @@
}
/* .Cell is needed to take precedence over codemirror's generated class ... */
-.Cell .cm-editor.cm-focused {
- outline: 0;
-}
+.Cell .cm-editor {
+ &.cm-focused {
+ outline: 0;
-.Cell .cm-editor.cm-focused .cm-activeLineGutter {
- background: #e2f2ff;
-}
+ .cm-activeLineGutter {
+ background: #e2f2ff;
+ }
-.dark .Cell .cm-editor.cm-focused .cm-activeLineGutter {
- background: #0e1e25;
-}
+ .cm-activeLine {
+ background: hsl(210deg 100% 50% / 3%);
+ }
+ }
+
+ .cm-activeLine {
+ background: transparent;
+
+ /* Soften the corners of the active-line highlight. */
+ border-radius: 2px;
+ }
-.Cell .cm-editor .cm-activeLineGutter {
- background: transparent;
+ .cm-activeLineGutter {
+ background: transparent;
+ }
+
+ .cm-content {
+ font-size: 0.875rem;
+ font-family: var(--monospace-font);
+ }
}
-.Cell .cm-editor.cm-focused .cm-activeLine {
- background: hsl(210deg 100% 50% / 3%);
+.dark .Cell .cm-editor.cm-focused .cm-activeLineGutter {
+ background: #0e1e25;
}
.dark .Cell .cm-editor.cm-focused .cm-activeLine {
background: hsl(210deg 100% 2% / 20%);
}
-.Cell .cm-editor .cm-activeLine {
- background: transparent;
-
- /* Soften the corners of the active-line highlight. */
- border-radius: 2px;
-}
-
/* ------------------------------ Output Areas ------------------------------ */
.output-area,
@@ -253,13 +359,6 @@
* breaks margin collapse. */
}
-/* Only restrain output length in edit mode. */
-.Cell:not(.published) .output-area,
-.console-output-area {
- max-height: 600px;
- overflow: auto;
-}
-
.marimo-output-stale,
.Cell.stale .output-area.marimo-output-stale,
.Cell.stale .console-output-area.marimo-output-stale {
@@ -283,75 +382,3 @@
border-left: 1px solid var(--gray-3);
background-color: var(--gray-1);
}
-
-/* ------------------------------- Published -------------------------------- */
-
-.Cell.published .output-area {
- /* flow-root interferes with margin collapsing, but appears to be unneeded
- * when cell outlines are hidden; clear:both is sufficient.
- *
- * if developers just use css-grid instead of float, this won't matter.
- * */
- display: block;
- padding-top: 0%;
- padding-bottom: 0%;
-}
-
-/* We remove all borders and vertical padding to make sure that margins
- * collapse across cells. Removing the borders will cause a 1px shift/jump,
- * but nothing can be done about that. */
-.Cell.published:hover {
- border: none;
- box-shadow: none;
-}
-
-.Cell.published:focus-within {
- transform: none;
-}
-
-.Cell:focus-within .cm-editor {
- box-shadow: none;
- border: 1px solid transparent;
-}
-
-.Cell.published {
- margin-bottom: 0;
-}
-
-.Cell.published,
-.Cell.published .output-area,
-.Cell.published .console-output-area {
- border: none;
- box-shadow: none;
-}
-
-.Cell .cm-content {
- font-size: 0.875rem;
- font-family: var(--monospace-font);
-}
-
-/* ------------------------------ Needs Run---------------------------------- */
-.Cell.needs-run {
- box-shadow: var(--light-shadow-stale);
-}
-
-.Cell.needs-run:hover {
- box-shadow: var(--medium-shadow-stale);
-}
-
-.Cell.needs-run:focus-within {
- box-shadow: var(--heavy-shadow-stale);
-}
-
-.Cell.needs-run:focus-within .cm-editor {
- box-shadow: none;
- border: 1px solid transparent;
-}
-
-.Cell.needs-run .output-area {
- border-bottom: 1px solid var(--stale-color);
-}
-
-.Cell.needs-run .RunButton {
- visibility: visible;
-}
diff --git a/frontend/src/plugins/impl/data-frames/python/code-panel.tsx b/frontend/src/plugins/impl/data-frames/python/code-panel.tsx
index e4b7f3058b..ece7fe6804 100644
--- a/frontend/src/plugins/impl/data-frames/python/code-panel.tsx
+++ b/frontend/src/plugins/impl/data-frames/python/code-panel.tsx
@@ -1,14 +1,8 @@
/* Copyright 2023 Marimo. All rights reserved. */
-import React, { memo } from "react";
+import React from "react";
import { Transformations } from "../schema";
-import { python } from "@codemirror/lang-python";
-import CodeMirror, { EditorView } from "@uiw/react-codemirror";
import { pythonPrintTransforms } from "./python-print";
-import { CopyIcon } from "lucide-react";
-import { Button } from "@/components/ui/button";
-import { Events } from "@/utils/events";
-import { toast } from "@/components/ui/use-toast";
-import { useThemeForPlugin } from "@/theme/useTheme";
+import { ReadonlyPythonCode } from "@/components/editor/code/readonly-python-code";
interface Props {
dataframeName: string;
@@ -21,45 +15,10 @@ export const CodePanel: React.FC<Props> = ({ transforms, dataframeName }) => {
}
return (
- <PythonCode
+ <ReadonlyPythonCode
+ className="min-h-[200px]"
+ minHeight="200px"
code={pythonPrintTransforms(dataframeName, transforms.transforms)}
/>
);
};
-
-const PythonCode = memo((props: { code: string }) => {
- const { theme } = useThemeForPlugin();
- return (
- <div className="relative min-h-[200px]">
- <FloatingCopyButton text={props.code} />
- <CodeMirror
- minHeight="200px"
- className="cm"
- theme={theme === "dark" ? "dark" : "light"}
- height="100%"
- editable={true}
- extensions={[python(), EditorView.lineWrapping]}
- value={props.code}
- readOnly={true}
- />
- </div>
- );
-});
-PythonCode.displayName = "PythonCode";
-
-const FloatingCopyButton = (props: { text: string }) => {
- const copy = Events.stopPropagation(() => {
- navigator.clipboard.writeText(props.text);
- toast({ title: "Copied to clipboard" });
- });
-
- return (
- <Button
- onClick={copy}
- className="absolute top-0 right-0 m-2 z-10"
- variant="secondary"
- >
- <CopyIcon size={16} strokeWidth={1.5} />
- </Button>
- );
-};