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.

feat: export to pdf in notebook dropdown (#1919)

Myles Scolnicke39c750b5c50
diff --git a/frontend/src/components/editor/Cell.tsx b/frontend/src/components/editor/Cell.tsx
index e02bb28cfb..4ede0fdc2d 100644
--- a/frontend/src/components/editor/Cell.tsx
+++ b/frontend/src/components/editor/Cell.tsx
@@ -295,7 +295,7 @@ const CellComponent = (
const outputArea = hasOutput && (
<div className="relative">
- <div className="absolute top-5 -left-8 z-10">
+ <div className="absolute top-5 -left-8 z-10 print:hidden">
<CollapseToggle
isCollapsed={isCollapsed}
onClick={() => {
diff --git a/frontend/src/components/editor/Output.tsx b/frontend/src/components/editor/Output.tsx
index b4aa3dc2ab..23d09f0e27 100644
--- a/frontend/src/components/editor/Output.tsx
+++ b/frontend/src/components/editor/Output.tsx
@@ -1,7 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import React, { memo, useEffect, useMemo, useRef, useState } from "react";
-import { OutputMessage } from "@/core/kernel/messages";
+import type { OutputMessage } from "@/core/kernel/messages";
import { logNever } from "../../utils/assertNever";
import { JsonOutput } from "./output/JsonOutput";
@@ -10,7 +10,7 @@ import { ImageOutput } from "./output/ImageOutput";
import { MarimoErrorOutput } from "./output/MarimoErrorOutput";
import { TextOutput } from "./output/TextOutput";
import { VideoOutput } from "./output/VideoOutput";
-import { CellId } from "@/core/cells/ids";
+import type { CellId } from "@/core/cells/ids";
import { cn } from "@/utils/cn";
import { ErrorBoundary } from "./boundary/ErrorBoundary";
@@ -211,7 +211,7 @@ const ExpandableOutput = React.memo(
<>
<div>
{(isOverflowing || isExpanded) && (
- <div className="relative">
+ <div className="relative print:hidden">
<Button
data-testid="expand-output-button"
className={cn(
@@ -244,7 +244,7 @@ const ExpandableOutput = React.memo(
{children}
</div>
</div>
- <div className="increase-pointer-area-x contents" />
+ <div className="increase-pointer-area-x contents print:hidden" />
</>
);
},
diff --git a/frontend/src/components/editor/actions/useNotebookActions.tsx b/frontend/src/components/editor/actions/useNotebookActions.tsx
index b429501249..0215f96151 100644
--- a/frontend/src/components/editor/actions/useNotebookActions.tsx
+++ b/frontend/src/components/editor/actions/useNotebookActions.tsx
@@ -20,6 +20,7 @@ import {
CheckIcon,
KeyboardIcon,
Undo2Icon,
+ FileIcon,
} from "lucide-react";
import { commandPaletteAtom } from "../controls/command-palette";
import { useCellActions, useNotebook } from "@/core/cells/cells";
@@ -142,6 +143,22 @@ export function useNotebookActions() {
toasted.dismiss();
},
},
+ {
+ icon: <FileIcon size={14} strokeWidth={1.5} />,
+ label: "Download as PDF",
+ handle: async () => {
+ await runDuringPresentMode(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();
+ });
+ },
+ },
{
icon: (
<MarkdownIcon strokeWidth={1.5} style={{ width: 14, height: 14 }} />
diff --git a/frontend/src/components/editor/app-container.tsx b/frontend/src/components/editor/app-container.tsx
index 68dbb3667b..7ad87d927c 100644
--- a/frontend/src/components/editor/app-container.tsx
+++ b/frontend/src/components/editor/app-container.tsx
@@ -34,6 +34,7 @@ export const AppContainer: React.FC<PropsWithChildren<Props>> = ({
"bg-background w-full h-full text-textColor",
"flex flex-col overflow-y-auto overflow-x-hidden",
width === "full" && "config-width-full",
+ "print:height-fit",
)}
>
{children}
diff --git a/frontend/src/components/editor/chrome/wrapper/app-chrome.tsx b/frontend/src/components/editor/chrome/wrapper/app-chrome.tsx
index 35ad3d9a61..88eb441ffb 100644
--- a/frontend/src/components/editor/chrome/wrapper/app-chrome.tsx
+++ b/frontend/src/components/editor/chrome/wrapper/app-chrome.tsx
@@ -160,7 +160,7 @@ export const AppChrome: React.FC<PropsWithChildren> = ({ children }) => {
collapsedSize={0}
collapsible={true}
className={cn(
- "dark:bg-[var(--slate-1)] no-print",
+ "dark:bg-[var(--slate-1)] no-print print:hidden",
isSidebarOpen && "border-r border-l border-[var(--slate-7)]",
)}
minSize={10}
@@ -190,7 +190,7 @@ export const AppChrome: React.FC<PropsWithChildren> = ({ children }) => {
collapsedSize={0}
collapsible={true}
className={cn(
- "dark:bg-[var(--slate-1)] no-print",
+ "dark:bg-[var(--slate-1)] no-print print:hidden",
isTerminalOpen && "border-[var(--slate-7)]",
)}
minSize={10}
@@ -217,7 +217,7 @@ export const AppChrome: React.FC<PropsWithChildren> = ({ children }) => {
);
return (
- <div className="flex flex-col flex-1 overflow-hidden absolute inset-0">
+ <div className="flex flex-col flex-1 overflow-hidden absolute inset-0 print:relative">
<PanelGroup
autoSaveId="marimo:chrome:v1:l2"
direction={"horizontal"}
diff --git a/frontend/src/components/editor/chrome/wrapper/footer.tsx b/frontend/src/components/editor/chrome/wrapper/footer.tsx
index 4b5347e681..2a1a850dc9 100644
--- a/frontend/src/components/editor/chrome/wrapper/footer.tsx
+++ b/frontend/src/components/editor/chrome/wrapper/footer.tsx
@@ -30,7 +30,7 @@ export const Footer: React.FC = () => {
invariant(errorPanel, "Error panel not found");
return (
- <footer className="h-10 py-2 bg-background flex items-center text-muted-foreground text-md pl-1 pr-4 border-t border-border select-none no-print text-sm shadow-[0_0_4px_1px_rgba(0,0,0,0.1)] z-50">
+ <footer className="h-10 py-2 bg-background flex items-center text-muted-foreground text-md pl-1 pr-4 border-t border-border select-none no-print text-sm shadow-[0_0_4px_1px_rgba(0,0,0,0.1)] z-50 print:hidden">
<FooterItem
tooltip={errorPanel.tooltip}
selected={selectedPanel === errorPanel.type}
diff --git a/frontend/src/components/editor/chrome/wrapper/sidebar.tsx b/frontend/src/components/editor/chrome/wrapper/sidebar.tsx
index 748229163e..a5ee786f8d 100644
--- a/frontend/src/components/editor/chrome/wrapper/sidebar.tsx
+++ b/frontend/src/components/editor/chrome/wrapper/sidebar.tsx
@@ -21,7 +21,7 @@ export const Sidebar: React.FC = () => {
);
return (
- <div className="h-full py-4 px-1 flex flex-col items-start text-muted-foreground text-md select-none no-print text-sm z-50 dark:bg-background">
+ <div className="h-full py-4 px-1 flex flex-col items-start text-muted-foreground text-md select-none no-print text-sm z-50 dark:bg-background print:hidden">
{sidebarItems.map((p) => (
<SidebarItem
key={p.type}
diff --git a/frontend/src/components/editor/controls/Controls.tsx b/frontend/src/components/editor/controls/Controls.tsx
index b5174a2c3f..454ae937dd 100644
--- a/frontend/src/components/editor/controls/Controls.tsx
+++ b/frontend/src/components/editor/controls/Controls.tsx
@@ -20,7 +20,7 @@ import { ConfigButton } from "../../app-config/app-config-button";
import { LayoutSelect } from "../renderers/layout-select";
import { NotebookMenuDropdown } from "@/components/editor/controls/notebook-menu-dropdown";
import { FindReplace } from "@/components/find-replace/find-replace";
-import { AppConfig } from "@/core/config/config-schema";
+import type { AppConfig } from "@/core/config/config-schema";
import { useShouldShowInterrupt } from "../cell/useShouldShowInterrupt";
import { CommandPaletteButton } from "./command-palette-button";
import { cn } from "@/utils/cn";
@@ -222,7 +222,7 @@ const RunControlButton = ({
};
const topRightControls =
- "absolute top-3 right-5 m-0 flex items-center space-x-3 min-h-[28px] no-print pointer-events-auto z-30";
+ "absolute top-3 right-5 m-0 flex items-center space-x-3 min-h-[28px] no-print pointer-events-auto z-30 print:hidden";
const bottomRightControls =
- "absolute bottom-5 right-5 flex flex-col gap-2 items-center no-print pointer-events-auto z-30";
+ "absolute bottom-5 right-5 flex flex-col gap-2 items-center no-print pointer-events-auto z-30 print:hidden";
diff --git a/frontend/src/components/editor/notebook-banner.tsx b/frontend/src/components/editor/notebook-banner.tsx
index c6fbeae8b1..f0be8af244 100644
--- a/frontend/src/components/editor/notebook-banner.tsx
+++ b/frontend/src/components/editor/notebook-banner.tsx
@@ -16,7 +16,7 @@ export const NotebookBanner: React.FC = (props) => {
}
return (
- <div className="flex flex-col gap-4 mb-5">
+ <div className="flex flex-col gap-4 mb-5 print:hidden">
{banners.map((banner) => (
<Banner
kind={banner.variant || "info"}
diff --git a/frontend/src/components/editor/renderers/CellArray.tsx b/frontend/src/components/editor/renderers/CellArray.tsx
index f2927b2bd3..2d105aaba1 100644
--- a/frontend/src/components/editor/renderers/CellArray.tsx
+++ b/frontend/src/components/editor/renderers/CellArray.tsx
@@ -245,7 +245,7 @@ const AddCellButtons: React.FC = () => {
};
return (
- <div className="flex justify-center mt-4 pt-6 pb-32 group gap-4 w-full">
+ <div className="flex justify-center mt-4 pt-6 pb-32 group gap-4 w-full print:hidden">
<div
className={cn(
"shadow-sm border border-border rounded transition-all duration-200 overflow-hidden divide-x divide-border flex",
diff --git a/frontend/src/components/editor/renderers/vertical-layout/sidebar/wrapped-with-sidebar.tsx b/frontend/src/components/editor/renderers/vertical-layout/sidebar/wrapped-with-sidebar.tsx
index 286645c6ce..51b6cbd8a6 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/sidebar/wrapped-with-sidebar.tsx
+++ b/frontend/src/components/editor/renderers/vertical-layout/sidebar/wrapped-with-sidebar.tsx
@@ -1,6 +1,6 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { cn } from "@/utils/cn";
-import React from "react";
+import type React from "react";
import { SheetMenu } from "./sheet-sidebar";
import { useAtom } from "jotai";
import { sidebarAtom } from "./state";
diff --git a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx
index c32915d0f7..32faf4d227 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx
+++ b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout-wrapper.tsx
@@ -1,7 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { PropsWithChildren } from "react";
+import type { PropsWithChildren } from "react";
import { cn } from "@/utils/cn";
-import { AppConfig } from "@/core/config/config-schema";
+import type { AppConfig } from "@/core/config/config-schema";
interface Props {
className?: string;
@@ -16,7 +16,12 @@ export const VerticalLayoutWrapper: React.FC<PropsWithChildren<Props>> = ({
children,
}) => {
return (
- <div className={cn("px-1 sm:px-16 md:px-20 xl:px-24", className)}>
+ <div
+ className={cn(
+ "px-1 sm:px-16 md:px-20 xl:px-24 print:px-0 print:pb-0",
+ className,
+ )}
+ >
<div
className={cn(
// Large mobile bottom padding due to mobile browser navigation bar
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 dcc1569b6f..0b393c31ff 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
+++ b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
@@ -143,7 +143,7 @@ const ActionButtons: React.FC<{
return (
<div
className={cn(
- "right-0 top-0 z-50 m-4 no-print flex gap-2",
+ "right-0 top-0 z-50 m-4 no-print flex gap-2 print:hidden",
// If the notebook is static, we have a banner at the top, so
// we can't use fixed positioning. Ideally this is sticky, but the
// current dom structure makes that difficult.
diff --git a/frontend/src/components/find-replace/find-replace.tsx b/frontend/src/components/find-replace/find-replace.tsx
index f5de271ddb..a489e9eef2 100644
--- a/frontend/src/components/find-replace/find-replace.tsx
+++ b/frontend/src/components/find-replace/find-replace.tsx
@@ -4,7 +4,8 @@ import {
openFindReplacePanel,
} from "@/core/codemirror/find-replace/state";
import { useAtom, useAtomValue } from "jotai";
-import React, { useEffect, useState, useRef } from "react";
+import type React from "react";
+import { useEffect, useState, useRef } from "react";
import { Input } from "../ui/input";
import { Button } from "../ui/button";
import {
@@ -25,7 +26,7 @@ import {
XIcon,
} from "lucide-react";
import { useHotkey } from "@/hooks/useHotkey";
-import { EditorView } from "@codemirror/view";
+import type { EditorView } from "@codemirror/view";
import { FocusScope } from "@react-aria/focus";
import { toast } from "../ui/use-toast";
import {
@@ -107,7 +108,7 @@ export const FindReplace: React.FC = () => {
e.preventDefault();
}}
onBlur={() => setIsFocused(false)}
- className="fixed top-0 right-0 w-[500px] flex flex-col bg-[var(--sage-1)] p-4 z-50 mt-2 mr-3 rounded-md shadow-lg border gap-2"
+ className="fixed top-0 right-0 w-[500px] flex flex-col bg-[var(--sage-1)] p-4 z-50 mt-2 mr-3 rounded-md shadow-lg border gap-2 print:hidden"
onKeyDown={(e) => {
if (e.key === "Escape") {
dispatch({ type: "setIsOpen", isOpen: false });
diff --git a/frontend/src/core/edit-app.tsx b/frontend/src/core/edit-app.tsx
index e6419a584d..49a2eeafd5 100644
--- a/frontend/src/core/edit-app.tsx
+++ b/frontend/src/core/edit-app.tsx
@@ -327,7 +327,7 @@ export const EditApp: React.FC<AppProps> = ({ userConfig, appConfig }) => {
>
<AppHeader
connection={connection}
- className={cn("pt-4 sm:pt-12 pb-2 mb-4")}
+ className={cn("pt-4 sm:pt-12 pb-2 mb-4 print:hidden")}
>
{isEditing && (
<div className="flex items-center justify-center container">
diff --git a/frontend/src/css/app/print.css b/frontend/src/css/app/print.css
index d37c75db07..288eaaddb1 100644
--- a/frontend/src/css/app/print.css
+++ b/frontend/src/css/app/print.css
@@ -16,3 +16,73 @@ body.printing #App {
@apply bg-background;
}
+
+@media print {
+ * {
+ -webkit-print-color-adjust: exact;
+ print-color-adjust: exact;
+ }
+
+ #App {
+ /* Full screen print */
+ height: fit-content !important;
+ overflow-y: visible !important;
+ overflow-x: hidden !important;
+ }
+
+ h1,
+ h2,
+ h3,
+ h4 {
+ break-after: avoid-page;
+ }
+
+ article {
+ orphans: 2;
+ widows: 2;
+ }
+
+ .container {
+ width: 100%;
+ max-width: none;
+ }
+
+ a[href]:after {
+ content: " (" attr(href) ")";
+ }
+
+ * {
+ box-shadow: none !important;
+ text-shadow: none !important;
+ }
+
+ /* avoid breaking up a ui element (table, chart) */
+ div:has(> marimo-ui-element) {
+ page-break-inside: avoid;
+ }
+
+ body {
+ color: #000;
+ background-color: #fff;
+ }
+
+ [data-panel-group],
+ [data-panel] {
+ overflow: visible !important;
+ height: fit-content !important;
+ }
+
+ .tray,
+ .tray::after,
+ .tray::before {
+ display: none !important;
+ }
+
+ .output-area,
+ .console-output-area {
+ width: 100% !important;
+ padding-left: 0 !important;
+ padding-right: 0 !important;
+ max-height: none !important;
+ }
+}