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: restore scroll after screenshot (#420)

Myles Scolnickc50fb76b03d5
diff --git a/frontend/e2e-tests/helper.ts b/frontend/e2e-tests/helper.ts
index ab235d3aed..1b78631cc8 100644
--- a/frontend/e2e-tests/helper.ts
+++ b/frontend/e2e-tests/helper.ts
@@ -94,7 +94,7 @@ export async function exportAsHTMLAndTakeScreenshot(page: Page) {
// Wait for networkidle so that the notebook is fully loaded
await page.waitForLoadState("networkidle");
- // Start waiting for download before clicking. Note no await.
+ // Start waiting for download before clicking.
const downloadPromise = page.waitForEvent("download");
await page.getByTestId("notebook-menu-dropdown").click();
await page.getByText("Export as HTML").click();
@@ -113,3 +113,18 @@ export async function exportAsHTMLAndTakeScreenshot(page: Page) {
});
await takeScreenshot(exportPage, path);
}
+
+export async function exportAsPNG(page: Page) {
+ // Wait for networkidle so that the notebook is fully loaded
+ await page.waitForLoadState("networkidle");
+
+ // Start waiting for download before clicking.
+ const downloadPromise = page.waitForEvent("download");
+ await page.getByTestId("notebook-menu-dropdown").click();
+ await page.getByText("Export as PNG").click();
+ const download = await downloadPromise;
+
+ // Wait for the download process to complete and save the downloaded file somewhere.
+ const path = `e2e-tests/screenshots/${download.suggestedFilename()}`;
+ await download.saveAs(path);
+}
diff --git a/frontend/e2e-tests/kitchen-sink.spec.ts b/frontend/e2e-tests/kitchen-sink.spec.ts
index 1eb0d50d0b..31786190b6 100644
--- a/frontend/e2e-tests/kitchen-sink.spec.ts
+++ b/frontend/e2e-tests/kitchen-sink.spec.ts
@@ -1,7 +1,11 @@
/* Copyright 2023 Marimo. All rights reserved. */
import { test } from "@playwright/test";
import { getAppUrl } from "../playwright.config";
-import { exportAsHTMLAndTakeScreenshot, takeScreenshot } from "./helper";
+import {
+ exportAsHTMLAndTakeScreenshot,
+ exportAsPNG,
+ takeScreenshot,
+} from "./helper";
test("can screenshot and export as html", async ({ page }) => {
const appUrl = getAppUrl("kitchen_sink.py//edit");
@@ -9,4 +13,5 @@ test("can screenshot and export as html", async ({ page }) => {
await takeScreenshot(page, __filename);
await exportAsHTMLAndTakeScreenshot(page);
+ await exportAsPNG(page);
});
diff --git a/frontend/src/components/editor/actions/useCellActionButton.tsx b/frontend/src/components/editor/actions/useCellActionButton.tsx
index b450f6c47a..3fee1d2b41 100644
--- a/frontend/src/components/editor/actions/useCellActionButton.tsx
+++ b/frontend/src/components/editor/actions/useCellActionButton.tsx
@@ -75,7 +75,7 @@ export function useCellActionButtons({
},
{
icon: <ImageIcon size={13} strokeWidth={1.5} />,
- label: "Export to PNG",
+ label: "Export as PNG",
hidden: !hasOutput,
handle: () => downloadCellOutput(cellId),
},
diff --git a/frontend/src/components/editor/actions/useNotebookActions.tsx b/frontend/src/components/editor/actions/useNotebookActions.tsx
index 3b35f9e02c..41e279c11b 100644
--- a/frontend/src/components/editor/actions/useNotebookActions.tsx
+++ b/frontend/src/components/editor/actions/useNotebookActions.tsx
@@ -38,7 +38,7 @@ export function useNotebookActions() {
const actions: ActionButton[] = [
{
icon: <ImageIcon size={14} strokeWidth={1.5} />,
- label: "Export to PNG",
+ label: "Export as PNG",
handle: async () => {
await runDuringPresentMode(() => {
const app = document.getElementById("App");
diff --git a/frontend/src/utils/download.ts b/frontend/src/utils/download.ts
index ed17d3011a..edac0dd8c4 100644
--- a/frontend/src/utils/download.ts
+++ b/frontend/src/utils/download.ts
@@ -6,9 +6,12 @@ export async function downloadHTMLAsImage(
element: HTMLElement,
filename: string
) {
+ // Capture current scroll position
+ const appEl = document.getElementById("App");
+ const currentScrollY = appEl?.scrollTop ?? 0;
+ // Add classnames for printing
+ document.body.classList.add("printing");
try {
- // Add classnames for printing
- document.body.classList.add("printing");
// Get screenshot
const dataUrl = await toPng(element);
downloadByURL(
@@ -24,6 +27,10 @@ export async function downloadHTMLAsImage(
} finally {
// Remove classnames for printing
document.body.classList.remove("printing");
+ // Restore scroll position
+ requestAnimationFrame(() => {
+ appEl?.scrollTo(0, currentScrollY);
+ });
}
}