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.

support moving to new pages from row-viewer shortcuts (#7045)

Shahmir Varqha2805303bed46
diff --git a/frontend/src/components/data-table/__tests__/utils.test.ts b/frontend/src/components/data-table/__tests__/utils.test.ts
new file mode 100644
index 0000000000..b558297924
--- /dev/null
+++ b/frontend/src/components/data-table/__tests__/utils.test.ts
@@ -0,0 +1,79 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { describe, expect, it } from "vitest";
+import { getPageIndexForRow } from "../utils";
+
+describe("getPageIndexForRow", () => {
+ it("should return null when row is on current page", () => {
+ // Page 0, rows 0-9
+ expect(getPageIndexForRow(0, 0, 10)).toBeNull();
+ expect(getPageIndexForRow(5, 0, 10)).toBeNull();
+ expect(getPageIndexForRow(9, 0, 10)).toBeNull();
+
+ // Page 1, rows 10-19
+ expect(getPageIndexForRow(10, 1, 10)).toBeNull();
+ expect(getPageIndexForRow(15, 1, 10)).toBeNull();
+ expect(getPageIndexForRow(19, 1, 10)).toBeNull();
+ });
+
+ it("should return new page index when row is on a different page", () => {
+ // Row 15 should be on page 1 when viewing page 0
+ expect(getPageIndexForRow(15, 0, 10)).toBe(1);
+
+ // Row 5 should be on page 0 when viewing page 1
+ expect(getPageIndexForRow(5, 1, 10)).toBe(0);
+
+ // Row 25 should be on page 2 when viewing page 0
+ expect(getPageIndexForRow(25, 0, 10)).toBe(2);
+
+ // Row 0 should be on page 0 when viewing page 5
+ expect(getPageIndexForRow(0, 5, 10)).toBe(0);
+ });
+
+ it("should handle different page sizes", () => {
+ // Page size of 20
+ expect(getPageIndexForRow(0, 0, 20)).toBeNull();
+ expect(getPageIndexForRow(19, 0, 20)).toBeNull();
+ expect(getPageIndexForRow(20, 0, 20)).toBe(1);
+ expect(getPageIndexForRow(39, 0, 20)).toBe(1);
+ expect(getPageIndexForRow(40, 0, 20)).toBe(2);
+
+ // Page size of 5
+ expect(getPageIndexForRow(0, 0, 5)).toBeNull();
+ expect(getPageIndexForRow(4, 0, 5)).toBeNull();
+ expect(getPageIndexForRow(5, 0, 5)).toBe(1);
+ expect(getPageIndexForRow(9, 0, 5)).toBe(1);
+ expect(getPageIndexForRow(10, 0, 5)).toBe(2);
+ });
+
+ it("should handle boundary cases", () => {
+ // First row of next page
+ expect(getPageIndexForRow(10, 0, 10)).toBe(1);
+
+ // Last row of previous page
+ expect(getPageIndexForRow(9, 1, 10)).toBe(0);
+
+ // First row of current page
+ expect(getPageIndexForRow(10, 1, 10)).toBeNull();
+
+ // Last row of current page
+ expect(getPageIndexForRow(19, 1, 10)).toBeNull();
+
+ // Last row of last page
+ expect(getPageIndexForRow(99, 9, 10)).toBeNull();
+ });
+
+ it("should handle edge case of row 0", () => {
+ expect(getPageIndexForRow(0, 0, 10)).toBeNull();
+ expect(getPageIndexForRow(0, 1, 10)).toBe(0);
+ expect(getPageIndexForRow(0, 5, 10)).toBe(0);
+ });
+
+ it("should handle large page numbers and row indices", () => {
+ // Page 100, rows 1000-1009 (page size 10)
+ expect(getPageIndexForRow(1000, 100, 10)).toBeNull();
+ expect(getPageIndexForRow(1009, 100, 10)).toBeNull();
+ expect(getPageIndexForRow(1010, 100, 10)).toBe(101);
+ expect(getPageIndexForRow(999, 100, 10)).toBe(99);
+ });
+});
diff --git a/frontend/src/components/data-table/row-viewer-panel/row-viewer.tsx b/frontend/src/components/data-table/row-viewer-panel/row-viewer.tsx
index 2b8d274acc..73844572f0 100644
--- a/frontend/src/components/data-table/row-viewer-panel/row-viewer.tsx
+++ b/frontend/src/components/data-table/row-viewer-panel/row-viewer.tsx
@@ -145,12 +145,11 @@ export const RowViewerPanel: React.FC<RowViewerPanelProps> = ({
}
if (rows.length !== 1) {
+ const message = tooManyRows
+ ? "LazyFrame, no data available."
+ : `Expected 1 row, got ${rows.length} rows. Please report the issue.`;
return (
- <SimpleBanner
- kind="warn"
- Icon={AlertTriangle}
- message={`Expected 1 row, got ${rows.length} rows. Please report the issue.`}
- />
+ <SimpleBanner kind="warn" Icon={AlertTriangle} message={message} />
);
}
diff --git a/frontend/src/components/data-table/utils.ts b/frontend/src/components/data-table/utils.ts
index 71caf11cd0..6e2b259f53 100644
--- a/frontend/src/components/data-table/utils.ts
+++ b/frontend/src/components/data-table/utils.ts
@@ -44,3 +44,26 @@ export function getStableRowId<TData>(row: TData): string | undefined {
return String(row[INDEX_COLUMN_NAME]);
}
}
+
+/**
+ * Calculate which page a given row index should be on.
+ *
+ * @param rowIdx - The row index to check
+ * @param currentPageIndex - The current page index
+ * @param pageSize - The number of rows per page
+ * @returns The page index if pagination should change, or null if the row is on the current page
+ */
+export function getPageIndexForRow(
+ rowIdx: number,
+ currentPageIndex: number,
+ pageSize: number,
+): number | null {
+ const currentPageStart = currentPageIndex * pageSize;
+ const currentPageEnd = currentPageStart + pageSize - 1;
+
+ if (rowIdx < currentPageStart || rowIdx > currentPageEnd) {
+ return Math.floor(rowIdx / pageSize);
+ }
+
+ return null;
+}
diff --git a/frontend/src/plugins/impl/DataTablePlugin.tsx b/frontend/src/plugins/impl/DataTablePlugin.tsx
index 0922558c92..8a2182ceb0 100644
--- a/frontend/src/plugins/impl/DataTablePlugin.tsx
+++ b/frontend/src/plugins/impl/DataTablePlugin.tsx
@@ -51,7 +51,10 @@ import {
toFieldTypes,
type ValueCounts,
} from "@/components/data-table/types";
-import { loadTableData } from "@/components/data-table/utils";
+import {
+ getPageIndexForRow,
+ loadTableData,
+} from "@/components/data-table/utils";
import { ContextAwarePanelItem } from "@/components/editor/chrome/panels/context-aware-panel/context-aware-panel";
import { Alert, AlertTitle } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
@@ -847,6 +850,30 @@ const DataTableComponent = ({
},
);
+ const setViewedRow = useEvent((rowIdx: number) => {
+ setViewedRowIdx(rowIdx);
+
+ const outOfBounds =
+ rowIdx < 0 || (typeof totalRows === "number" && rowIdx >= totalRows);
+ if (outOfBounds || totalRows === TOO_MANY_ROWS) {
+ return;
+ }
+
+ // If the rowIdx moves to the next / previous page, update the pagination state
+ const newPageIndex = getPageIndexForRow(
+ rowIdx,
+ paginationState.pageIndex,
+ paginationState.pageSize,
+ );
+
+ if (newPageIndex !== null) {
+ setPaginationState((prev) => ({
+ ...prev,
+ pageIndex: newPageIndex,
+ }));
+ }
+ });
+
const cellSelection = value.filter(
(v) => v instanceof Object && v.columnName !== undefined,
) as CellSelectionState;
@@ -902,7 +929,7 @@ const DataTableComponent = ({
fieldTypes={memoizedUnclampedFieldTypes}
totalRows={totalRows}
rowIdx={viewedRowIdx}
- setRowIdx={setViewedRowIdx}
+ setRowIdx={setViewedRow}
isSelectable={isSelectable}
isRowSelected={rowSelection[viewedRowIdx]}
handleRowSelectionChange={handleRowSelectionChange}