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: disable pdf export while editing (#2387)

Myles Scolnick206d5d90936a
diff --git a/frontend/src/components/editor/actions/types.ts b/frontend/src/components/editor/actions/types.ts
index f39dfa1b70..e2d1dc5482 100644
--- a/frontend/src/components/editor/actions/types.ts
+++ b/frontend/src/components/editor/actions/types.ts
@@ -9,6 +9,8 @@ export interface ActionButton {
label: string;
labelElement?: React.ReactNode;
description?: string;
+ disabled?: boolean;
+ tooltip?: React.ReactNode;
variant?: "danger" | "muted" | "disabled";
disableClick?: boolean;
icon?: React.ReactElement;
diff --git a/frontend/src/components/editor/actions/useNotebookActions.tsx b/frontend/src/components/editor/actions/useNotebookActions.tsx
index 680559242e..d22947aac7 100644
--- a/frontend/src/components/editor/actions/useNotebookActions.tsx
+++ b/frontend/src/components/editor/actions/useNotebookActions.tsx
@@ -1,9 +1,5 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import {
- kioskModeAtom,
- runDuringPresentMode,
- viewStateAtom,
-} from "@/core/mode";
+import { kioskModeAtom, viewStateAtom } from "@/core/mode";
import { downloadBlob, downloadHTMLAsImage } from "@/utils/download";
import { useAtom, useAtomValue, useSetAtom } from "jotai";
import {
@@ -66,6 +62,7 @@ import { useTogglePresenting } from "@/core/layout/useTogglePresenting";
import { useCopyNotebook } from "./useCopyNotebook";
import { isWasm } from "@/core/wasm/utils";
import { settingDialogAtom } from "@/components/app-config/app-config-button";
+import { renderShortcut } from "@/components/shortcuts/renderShortcut";
const NOOP_HANDLER = (event?: Event) => {
event?.preventDefault();
@@ -148,55 +145,6 @@ export function useNotebookActions() {
await downloadAsHTML({ filename });
},
},
- {
- icon: <ImageIcon size={14} strokeWidth={1.5} />,
- label: "Download as PNG",
- handle: async () => {
- const toasted = toast({
- title: "Starting download",
- description: "Downloading as PNG...",
- });
-
- await runDuringPresentMode(async () => {
- const app = document.getElementById("App");
- if (!app) {
- return;
- }
-
- // Wait 3 seconds for the app to render
- await new Promise((resolve) => setTimeout(resolve, 3000));
-
- await downloadHTMLAsImage(app, document.title);
- });
-
- toasted.dismiss();
- },
- },
- {
- icon: <FileIcon size={14} strokeWidth={1.5} />,
- label: "Download PDF",
- handle: async () => {
- const toasted = toast({
- title: "Starting download",
- description: "Downloading as PDF...",
- });
-
- await runDuringPresentMode(async () => {
- // Wait 3 seconds for the app to render
- await new Promise((resolve) => setTimeout(resolve, 3000));
- toasted.dismiss();
-
- const beforeprint = new Event("export-beforeprint");
- const afterprint = new Event("export-afterprint");
- function print() {
- window.dispatchEvent(beforeprint);
- setTimeout(() => window.print(), 0);
- setTimeout(() => window.dispatchEvent(afterprint), 0);
- }
- print();
- });
- },
- },
{
icon: (
<MarkdownIcon strokeWidth={1.5} style={{ width: 14, height: 14 }} />
@@ -221,6 +169,48 @@ export function useNotebookActions() {
);
},
},
+ {
+ divider: true,
+ icon: <ImageIcon size={14} strokeWidth={1.5} />,
+ label: "Download as PNG",
+ disabled: viewState.mode !== "present",
+ tooltip:
+ viewState.mode === "present" ? undefined : (
+ <span>
+ Only available in app view. <br />
+ Toggle with: {renderShortcut("global.hideCode", false)}
+ </span>
+ ),
+ handle: async () => {
+ const app = document.getElementById("App");
+ if (!app) {
+ return;
+ }
+ await downloadHTMLAsImage(app, document.title);
+ },
+ },
+ {
+ icon: <FileIcon size={14} strokeWidth={1.5} />,
+ label: "Download as PDF",
+ disabled: viewState.mode !== "present",
+ tooltip:
+ viewState.mode === "present" ? undefined : (
+ <span>
+ Only available in app view. <br />
+ Toggle with: {renderShortcut("global.hideCode", false)}
+ </span>
+ ),
+ handle: async () => {
+ const beforeprint = new Event("export-beforeprint");
+ const afterprint = new Event("export-afterprint");
+ function print() {
+ window.dispatchEvent(beforeprint);
+ setTimeout(() => window.print(), 0);
+ setTimeout(() => window.dispatchEvent(afterprint), 0);
+ }
+ print();
+ },
+ },
],
},
diff --git a/frontend/src/components/editor/controls/command-palette.tsx b/frontend/src/components/editor/controls/command-palette.tsx
index e8ba482e56..3714e69c74 100644
--- a/frontend/src/components/editor/controls/command-palette.tsx
+++ b/frontend/src/components/editor/controls/command-palette.tsx
@@ -17,7 +17,11 @@ import { atom, useAtom, useAtomValue } from "jotai";
import { useNotebookActions } from "../actions/useNotebookActions";
import { Objects } from "@/utils/objects";
import { parseShortcut } from "@/core/hotkeys/shortcuts";
-import { isParentAction, flattenActions } from "../actions/types";
+import {
+ isParentAction,
+ flattenActions,
+ type ActionButton,
+} from "../actions/types";
import { useCellActionButtons } from "../actions/useCellActionButton";
import { lastFocusedCellAtom } from "@/core/cells/focus";
import { useConfigActions } from "../actions/useConfigActions";
@@ -45,10 +49,8 @@ export const CommandPalette = () => {
const notebookActionsWithoutHotkeys = notebookActions.filter(
(action) => !action.hotkey,
);
- const keyedNotebookActions = Objects.keyBy(
- notebookActionsWithoutHotkeys,
- (action) => action.label,
- );
+ const keyedNotebookActions: Record<string, ActionButton | undefined> =
+ Objects.keyBy(notebookActionsWithoutHotkeys, (action) => action.label);
const { recentCommands, addRecentCommand } = useRecentCommands();
const recentCommandsSet = new Set(recentCommands);
@@ -60,7 +62,13 @@ export const CommandPalette = () => {
}
});
- const renderShortcutCommandItem = (shortcut: HotkeyAction) => {
+ const renderShortcutCommandItem = (
+ shortcut: HotkeyAction,
+ props: {
+ disabled?: boolean;
+ tooltip?: React.ReactNode;
+ },
+ ) => {
const action = registeredActions[shortcut];
if (!action) {
return null;
@@ -69,6 +77,7 @@ export const CommandPalette = () => {
return (
<CommandItem
+ disabled={props.disabled}
onSelect={() => {
addRecentCommand(shortcut);
// Close first and then run the action, so the dialog doesn't steal focus
@@ -80,7 +89,10 @@ export const CommandPalette = () => {
key={shortcut}
value={hotkey.name}
>
- <span>{hotkey.name}</span>
+ <span>
+ {hotkey.name}
+ {props.tooltip && <span className="ml-2">{props.tooltip}</span>}
+ </span>
<CommandShortcut>
<KeyboardHotkeys shortcut={hotkey.key} />
</CommandShortcut>
@@ -91,10 +103,12 @@ export const CommandPalette = () => {
const renderCommandItem = (
label: string,
handle: () => void,
+ props: { disabled?: boolean; tooltip?: React.ReactNode } = {},
hotkey?: HotkeyAction,
) => {
return (
<CommandItem
+ disabled={props.disabled}
onSelect={() => {
addRecentCommand(label);
setOpen(false);
@@ -105,7 +119,10 @@ export const CommandPalette = () => {
key={label}
value={label}
>
- <span>{label}</span>
+ <span>
+ {label}
+ {props.tooltip && <span className="ml-2">({props.tooltip})</span>}
+ </span>
{hotkey && (
<CommandShortcut>
<KeyboardHotkeys shortcut={hotkeys.getHotkey(hotkey).key} />
@@ -124,16 +141,20 @@ export const CommandPalette = () => {
<>
<CommandGroup heading="Recently Used">
{recentCommands.map((shortcut) => {
+ const action = keyedNotebookActions[shortcut];
// Hotkey
if (isHotkeyAction(shortcut)) {
- return renderShortcutCommandItem(shortcut);
+ return renderShortcutCommandItem(shortcut, {
+ disabled: action?.disabled,
+ tooltip: action?.tooltip,
+ });
}
// Other action
- const action = keyedNotebookActions[shortcut];
if (action && !isParentAction(action)) {
return renderCommandItem(
action.label,
action.handleHeadless || action.handle,
+ { disabled: action.disabled, tooltip: action.tooltip },
);
}
return null;
@@ -147,7 +168,11 @@ export const CommandPalette = () => {
if (recentCommandsSet.has(shortcut)) {
return null; // Don't show recent commands in the main list
}
- return renderShortcutCommandItem(shortcut);
+ const action = keyedNotebookActions[shortcut];
+ return renderShortcutCommandItem(shortcut, {
+ disabled: action?.disabled,
+ tooltip: action?.tooltip,
+ });
})}
{notebookActionsWithoutHotkeys.map((action) => {
if (recentCommandsSet.has(action.label)) {
@@ -156,6 +181,7 @@ export const CommandPalette = () => {
return renderCommandItem(
action.label,
action.handleHeadless || action.handle,
+ { disabled: action.disabled, tooltip: action.tooltip },
);
})}
{cellActions.map((action) => {
@@ -165,6 +191,7 @@ export const CommandPalette = () => {
return renderCommandItem(
`Cell > ${action.label}`,
action.handleHeadless || action.handle,
+ { disabled: action.disabled, tooltip: action.tooltip },
);
})}
</CommandGroup>
diff --git a/frontend/src/components/editor/controls/notebook-menu-dropdown.tsx b/frontend/src/components/editor/controls/notebook-menu-dropdown.tsx
index cb927f3c68..d95152126c 100644
--- a/frontend/src/components/editor/controls/notebook-menu-dropdown.tsx
+++ b/frontend/src/components/editor/controls/notebook-menu-dropdown.tsx
@@ -17,6 +17,7 @@ import { MinimalShortcut } from "../../shortcuts/renderShortcut";
import { useNotebookActions } from "../actions/useNotebookActions";
import type { ActionButton } from "../actions/types";
import { getMarimoVersion } from "@/core/dom/marimo-tag";
+import { Tooltip } from "@/components/ui/tooltip";
export const NotebookMenuDropdown: React.FC = () => {
const actions = useNotebookActions();
@@ -48,16 +49,32 @@ export const NotebookMenuDropdown: React.FC = () => {
};
const renderLeafAction = (action: ActionButton) => {
- return (
+ const item = (
<DropdownMenuItem
key={action.label}
variant={action.variant}
+ disabled={action.disabled}
onSelect={(evt) => action.handle(evt)}
data-testid={`notebook-menu-dropdown-${action.label}`}
>
{renderLabel(action)}
</DropdownMenuItem>
);
+
+ if (action.tooltip) {
+ return (
+ <Tooltip
+ content={action.tooltip}
+ key={action.label}
+ side="left"
+ delayDuration={100}
+ >
+ <span>{item}</span>
+ </Tooltip>
+ );
+ }
+
+ return item;
};
return (
diff --git a/frontend/src/components/shortcuts/renderShortcut.tsx b/frontend/src/components/shortcuts/renderShortcut.tsx
index 52c6c13622..3e6e1757ab 100644
--- a/frontend/src/components/shortcuts/renderShortcut.tsx
+++ b/frontend/src/components/shortcuts/renderShortcut.tsx
@@ -8,17 +8,20 @@ import { hotkeysAtom } from "@/core/config/config";
import { useAtomValue } from "jotai";
import { cn } from "@/utils/cn";
-export function renderShortcut(shortcut: HotkeyAction) {
- return <Shortcut shortcut={shortcut} />;
+export function renderShortcut(shortcut: HotkeyAction, includeName = true) {
+ return <Shortcut shortcut={shortcut} includeName={includeName} />;
}
-const Shortcut: React.FC<{ shortcut: HotkeyAction }> = ({ shortcut }) => {
+const Shortcut: React.FC<{ shortcut: HotkeyAction; includeName?: boolean }> = ({
+ shortcut,
+ includeName = true,
+}) => {
const hotkeys = useAtomValue(hotkeysAtom);
const hotkey = hotkeys.getHotkey(shortcut);
return (
- <span className="flex">
- <span className="mr-2">{hotkey.name}</span>
+ <span className="inline-flex">
+ {includeName && <span className="mr-2">{hotkey.name}</span>}
<KeyboardHotkeys shortcut={hotkey.key} />
</span>
);
diff --git a/marimo/_smoke_tests/third_party/plotly_example.py b/marimo/_smoke_tests/third_party/plotly_example.py
index ccb6360cdb..e6c9a137f7 100644
--- a/marimo/_smoke_tests/third_party/plotly_example.py
+++ b/marimo/_smoke_tests/third_party/plotly_example.py
@@ -1,14 +1,15 @@
# /// script
# requires-python = ">=3.11"
# dependencies = [
-# "plotly",
-# "pandas",
+# "plotly==5.24.1",
+# "pandas==2.2.3",
+# "marimo",
# ]
# ///
import marimo
-__generated_with = "0.8.14"
+__generated_with = "0.8.18"
app = marimo.App(width="full")
@@ -23,7 +24,7 @@ def __():
import plotly.express as px
px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
- return px,
+ return (px,)
@app.cell
@@ -37,7 +38,7 @@ def __(mo, px):
plot,
]
)
- return plot,
+ return (plot,)
@app.cell
@@ -145,7 +146,7 @@ def __():
def __(cars, mo):
sample_size = mo.ui.slider(label="Sample", start=100, stop=len(cars), step=100)
sample_size
- return sample_size,
+ return (sample_size,)
@app.cell
@@ -162,7 +163,7 @@ def __(cars, mo, px, sample_size):
_fig
plot3 = mo.ui.plotly(_fig)
plot3
- return plot3,
+ return (plot3,)
@app.cell