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: column grouping (#3547)

Myles Scolnick754842b1c81c
diff --git a/frontend/src/components/editor/renderers/vertical-layout/__tests__/vertical-layout.test.ts b/frontend/src/components/editor/renderers/vertical-layout/__tests__/vertical-layout.test.ts
new file mode 100644
index 0000000000..036793d8dd
--- /dev/null
+++ b/frontend/src/components/editor/renderers/vertical-layout/__tests__/vertical-layout.test.ts
@@ -0,0 +1,40 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+/* eslint-disable @typescript-eslint/no-explicit-any */
+import { describe, it, expect } from "vitest";
+import { groupCellsByColumn } from "../vertical-layout";
+
+describe("groupCellsByColumn", () => {
+ it("should group cells by column and maintain order", () => {
+ const cells = [
+ { config: { column: 0 }, id: "1" },
+ { config: { column: 1 }, id: "2" },
+ { config: { column: 0 }, id: "3" },
+ { config: { column: 2 }, id: "4" },
+ { config: { column: 1 }, id: "5" },
+ ] as any[];
+
+ const result = groupCellsByColumn(cells);
+
+ expect(result).toEqual([
+ [0, [cells[0], cells[2]]],
+ [1, [cells[1], cells[4]]],
+ [2, [cells[3]]],
+ ]);
+ });
+
+ it("should use last seen column when column not specified", () => {
+ const cells = [
+ { config: { column: 0 }, id: "1" },
+ { config: {}, id: "2" },
+ { config: { column: 1 }, id: "3" },
+ { config: {}, id: "4" },
+ ] as any[];
+
+ const result = groupCellsByColumn(cells);
+
+ expect(result).toEqual([
+ [0, [cells[0], cells[1]]],
+ [1, [cells[2], cells[3]]],
+ ]);
+ });
+});
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 cb05f92caa..3b408a99ff 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
+++ b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
@@ -112,21 +112,7 @@ const VerticalLayoutRenderer: React.FC<VerticalLayoutProps> = ({
const renderCells = () => {
if (appConfig.width === "columns") {
- // Group cells by column
- const cellsByColumn = new Map<number, typeof cells>();
- cells.forEach((cell) => {
- const column = cell.config.column ?? 0;
- if (!cellsByColumn.has(column)) {
- cellsByColumn.set(column, []);
- }
- cellsByColumn.get(column)?.push(cell);
- });
-
- // Sort columns by index
- const sortedColumns = [...cellsByColumn.entries()].sort(
- ([a], [b]) => a - b,
- );
-
+ const sortedColumns = groupCellsByColumn(cells);
return (
<div className="flex flex-row gap-8 w-full">
{sortedColumns.map(([columnIndex, columnCells]) => (
@@ -395,3 +381,22 @@ export const VerticalLayoutPlugin: ICellRendererPlugin<
serializeLayout: (layout) => layout,
getInitialLayout: () => null,
};
+
+export function groupCellsByColumn(
+ cells: Array<CellRuntimeState & CellData>,
+): Array<[number, Array<CellRuntimeState & CellData>]> {
+ // Group cells by column
+ const cellsByColumn = new Map<number, Array<CellRuntimeState & CellData>>();
+ let lastSeenColumn = 0;
+ cells.forEach((cell) => {
+ const column = cell.config.column ?? lastSeenColumn;
+ lastSeenColumn = column;
+ if (!cellsByColumn.has(column)) {
+ cellsByColumn.set(column, []);
+ }
+ cellsByColumn.get(column)?.push(cell);
+ });
+
+ // Sort columns by index
+ return [...cellsByColumn.entries()].sort(([a], [b]) => a - b);
+}