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(data-editor): make edit replay robust across structural changes (#10662)

Shahmir Varqha8869b186a8c1
diff --git a/frontend/plugins.openapi.yaml b/frontend/plugins.openapi.yaml
index e0e7042e01..09503a8c05 100644
--- a/frontend/plugins.openapi.yaml
+++ b/frontend/plugins.openapi.yaml
@@ -3071,6 +3071,11 @@ components:
- unknown
- type: string
- type: "null"
+ columnNames:
+ default: []
+ type: array
+ items:
+ type: string
editableColumns:
anyOf:
- type: array
diff --git a/frontend/src/plugins/impl/DataEditorPlugin.tsx b/frontend/src/plugins/impl/DataEditorPlugin.tsx
index a10e9d3439..ee96000e74 100644
--- a/frontend/src/plugins/impl/DataEditorPlugin.tsx
+++ b/frontend/src/plugins/impl/DataEditorPlugin.tsx
@@ -1,27 +1,28 @@
/* Copyright 2026 Marimo. All rights reserved. */
import glideCss from "@glideapps/glide-data-grid/dist/index.css?inline";
-import React, { useState } from "react";
+import React, { useEffect, useState } from "react";
import { z } from "zod";
import { inferFieldTypes } from "@/components/data-table/columns";
import { LoadingTable } from "@/components/data-table/loading-table";
-import { type FieldTypes, toFieldTypes } from "@/components/data-table/types";
+import {
+ type FieldTypesWithExternalType,
+ toFieldTypes,
+} from "@/components/data-table/types";
import { Alert, AlertTitle } from "@/components/ui/alert";
import { DelayMount } from "@/components/utils/delay-mount";
import { useAsyncData } from "@/hooks/useAsyncData";
import { createPlugin } from "../core/builder";
import type { Setter } from "../types";
import { columnToFieldTypesSchema } from "./data-frames/schema";
-import {
- BulkEdit,
- type DataEditorProps,
- type Edits,
-} from "./data-editor/types";
+import { orderColumnFields } from "./data-editor/data-utils";
+import { applyEditorEdits } from "./data-editor/editor-state";
+import type { EditorRow, EditorState, Edits } from "./data-editor/types";
import { vegaLoadData } from "./vega/loader";
import { getVegaFieldTypes } from "./vega/utils";
type CsvURL = string;
-type TableData<T> = T[] | CsvURL;
+type TableData = EditorRow[] | CsvURL;
// Lazy load the data editor since it brings in glide-data-grid
const LazyDataEditor = React.lazy(
@@ -45,6 +46,7 @@ export const DataEditorPlugin = createPlugin<Edits>("marimo-data-editor", {
label: z.string().nullable(),
data: z.union([z.string(), z.array(z.object({}).passthrough())]),
fieldTypes: columnToFieldTypesSchema.nullish(),
+ columnNames: z.array(z.string()).default([]),
editableColumns: z.union([z.array(z.string()), z.literal("all")]),
columnSizingMode: z.enum(["auto", "fit"]).default("auto"), // TODO: Remove this
}),
@@ -55,31 +57,28 @@ export const DataEditorPlugin = createPlugin<Edits>("marimo-data-editor", {
<LoadingDataEditor
data={props.data.data}
fieldTypes={props.data.fieldTypes}
+ columnNames={props.data.columnNames}
edits={props.value}
onEdits={props.setValue}
- host={props.host}
editableColumns={props.data.editableColumns}
/>
);
});
-interface Props extends Omit<
- DataEditorProps<object>,
- "data" | "onAddEdits" | "onAddRows"
-> {
- data: TableData<object>;
+interface Props {
+ data: TableData;
+ fieldTypes: FieldTypesWithExternalType | null | undefined;
edits: Edits;
onEdits: Setter<Edits>;
- host: HTMLElement;
editableColumns: string[] | "all";
+ columnNames: string[];
}
const LoadingDataEditor = (props: Props) => {
- const [data, setData] = useState<unknown[]>([]);
- const [columnFields, setColumnFields] = useState<FieldTypes>(new Map());
+ const [editorState, setEditorState] = useState<EditorState | null>(null);
// Load the data
- const { error } = useAsyncData(async () => {
+ const { data: loadedState, error } = useAsyncData(async () => {
const withoutExternalTypes = toFieldTypes(props.fieldTypes ?? []);
// If we already have the data, return it
@@ -87,7 +86,7 @@ const LoadingDataEditor = (props: Props) => {
// plain `Record`; column order doesn't matter for parsing.
const localData = Array.isArray(props.data)
? props.data
- : await vegaLoadData(
+ : await vegaLoadData<EditorRow>(
props.data,
{
type: "csv",
@@ -96,11 +95,20 @@ const LoadingDataEditor = (props: Props) => {
{ handleBigIntAndNumberLike: true },
);
- setData(localData);
- setColumnFields(
- toFieldTypes(props.fieldTypes ?? inferFieldTypes(localData)),
- );
- }, [props.fieldTypes, props.data]);
+ return {
+ data: localData,
+ columnFields: orderColumnFields(
+ toFieldTypes(props.fieldTypes ?? inferFieldTypes(localData)),
+ props.columnNames,
+ ),
+ } satisfies EditorState;
+ }, [props.fieldTypes, props.columnNames, props.data]);
+
+ useEffect(() => {
+ if (loadedState !== undefined) {
+ setEditorState(applyEditorEdits(loadedState, props.edits.edits));
+ }
+ }, [loadedState, props.edits.edits]);
if (error) {
return (
@@ -113,7 +121,7 @@ const LoadingDataEditor = (props: Props) => {
);
}
- if (!data) {
+ if (editorState === null) {
return (
<DelayMount milliseconds={200}>
<LoadingTable pageSize={10} />
@@ -123,54 +131,14 @@ const LoadingDataEditor = (props: Props) => {
return (
<LazyDataEditor
- data={data}
- setData={setData}
- columnFields={columnFields}
- setColumnFields={setColumnFields}
+ data={editorState.data}
+ columnFields={editorState.columnFields}
editableColumns={props.editableColumns}
- edits={props.edits.edits}
onAddEdits={(edits) => {
- props.onEdits((v) => ({ ...v, edits: [...v.edits, ...edits] }));
- }}
- onAddRows={(rows) => {
- const newEdits = rows.flatMap((row, rowIndex) =>
- Object.entries(row).map(([columnId, value]) => ({
- rowIdx: data.length + rowIndex,
- columnId,
- value,
- })),
+ setEditorState((state) =>
+ state === null ? null : applyEditorEdits(state, edits),
);
- props.onEdits((v) => ({ ...v, edits: [...v.edits, ...newEdits] }));
- }}
- onDeleteRows={(rowIndexes) => {
- props.onEdits((v) => {
- const newEdits = rowIndexes.map((rowIdx, index) => ({
- rowIdx: rowIdx - index,
- type: BulkEdit.Remove,
- }));
- return {
- ...v,
- edits: [...v.edits, ...newEdits],
- };
- });
- }}
- onRenameColumn={(columnIdx: number, newName: string) => {
- props.onEdits((v) => ({
- ...v,
- edits: [...v.edits, { columnIdx, newName, type: BulkEdit.Rename }],
- }));
- }}
- onDeleteColumn={(columnIdx: number) => {
- props.onEdits((v) => ({
- ...v,
- edits: [...v.edits, { columnIdx, type: BulkEdit.Remove }],
- }));
- }}
- onAddColumn={(columnIdx: number, newName: string) => {
- props.onEdits((v) => ({
- ...v,
- edits: [...v.edits, { columnIdx, newName, type: BulkEdit.Insert }],
- }));
+ props.onEdits((v) => ({ ...v, edits: [...v.edits, ...edits] }));
}}
/>
);
diff --git a/frontend/src/plugins/impl/__tests__/DataEditorPlugin.test.ts b/frontend/src/plugins/impl/__tests__/DataEditorPlugin.test.ts
index 3b773d1368..45f691212c 100644
--- a/frontend/src/plugins/impl/__tests__/DataEditorPlugin.test.ts
+++ b/frontend/src/plugins/impl/__tests__/DataEditorPlugin.test.ts
@@ -1,9 +1,55 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import { describe, expect, it } from "vitest";
+import React, { Suspense } from "react";
+import { act, render, screen, waitFor } from "@testing-library/react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import type { z } from "zod";
+import type { IPluginProps } from "../../types";
+import type { EditorRow, Edits } from "../data-editor/types";
import { DataEditorPlugin } from "../DataEditorPlugin";
+type PluginData = z.infer<typeof DataEditorPlugin.validator>;
+
+const mocks = vi.hoisted(() => ({
+ loadData: vi.fn(),
+}));
+
+vi.mock("../vega/loader", () => ({
+ vegaLoadData: mocks.loadData,
+}));
+
+vi.mock("../data-editor/glide-data-editor", async () => {
+ const React = await import("react");
+ return {
+ default: (props: { data: EditorRow[] }) =>
+ React.createElement(
+ "pre",
+ { "data-testid": "editor-data" },
+ JSON.stringify(props.data),
+ ),
+ };
+});
+
+function renderPlugin(data: PluginData, value: Edits) {
+ const props: IPluginProps<Edits, PluginData> = {
+ host: document.createElement("div"),
+ data,
+ value,
+ setValue: vi.fn(),
+ functions: {},
+ };
+ return React.createElement(
+ Suspense,
+ { fallback: null },
+ DataEditorPlugin.render(props),
+ );
+}
+
describe("DataEditorPlugin", () => {
+ beforeEach(() => {
+ mocks.loadData.mockReset();
+ });
+
it("normalizes an unrecognized field type", () => {
const result = DataEditorPlugin.validator.parse({
initialValue: { edits: [] },
@@ -27,4 +73,109 @@ describe("DataEditorPlugin", () => {
expect(result.fieldTypes).toEqual([["geom", ["geometry", "geometry"]]]);
});
+
+ it("applies the latest edits when an async data load completes", async () => {
+ let resolveLoad: ((data: EditorRow[]) => void) | undefined;
+ mocks.loadData.mockImplementationOnce(
+ () =>
+ new Promise<EditorRow[]>((resolve) => {
+ resolveLoad = resolve;
+ }),
+ );
+
+ const data = DataEditorPlugin.validator.parse({
+ initialValue: { edits: [] },
+ label: null,
+ data: "data.csv",
+ fieldTypes: null,
+ columnNames: ["name"],
+ editableColumns: "all",
+ });
+ const result = render(renderPlugin(data, { edits: [] }));
+ result.rerender(
+ renderPlugin(data, {
+ edits: [{ rowIdx: 0, columnId: "name", value: "latest" }],
+ }),
+ );
+ expect(resolveLoad).toBeDefined();
+ resolveLoad?.([{ name: "original" }]);
+
+ await waitFor(() => {
+ expect(screen.getByTestId("editor-data")).toHaveTextContent(
+ JSON.stringify([{ name: "latest" }]),
+ );
+ });
+ });
+
+ it("applies edit updates after data has loaded", async () => {
+ const data = DataEditorPlugin.validator.parse({
+ initialValue: { edits: [] },
+ label: null,
+ data: [{ name: "original" }],
+ fieldTypes: null,
+ columnNames: ["name"],
+ editableColumns: "all",
+ });
+ const result = render(renderPlugin(data, { edits: [] }));
+ await waitFor(() => {
+ expect(screen.getByTestId("editor-data")).toHaveTextContent(
+ JSON.stringify([{ name: "original" }]),
+ );
+ });
+
+ result.rerender(
+ renderPlugin(data, {
+ edits: [{ rowIdx: 0, columnId: "name", value: "updated" }],
+ }),
+ );
+
+ await waitFor(() => {
+ expect(screen.getByTestId("editor-data")).toHaveTextContent(
+ JSON.stringify([{ name: "updated" }]),
+ );
+ });
+ });
+
+ it("ignores a superseded async data load", async () => {
+ let resolveFirst: ((data: EditorRow[]) => void) | undefined;
+ let resolveSecond: ((data: EditorRow[]) => void) | undefined;
+ mocks.loadData.mockImplementation((source: string) => {
+ return new Promise<EditorRow[]>((resolve) => {
+ if (source === "first.csv") {
+ resolveFirst = resolve;
+ } else {
+ resolveSecond = resolve;
+ }
+ });
+ });
+
+ const makeData = (source: string) =>
+ DataEditorPlugin.validator.parse({
+ initialValue: { edits: [] },
+ label: null,
+ data: source,
+ fieldTypes: null,
+ columnNames: ["source"],
+ editableColumns: "all",
+ });
+ const result = render(renderPlugin(makeData("first.csv"), { edits: [] }));
+ result.rerender(renderPlugin(makeData("second.csv"), { edits: [] }));
+
+ expect(resolveFirst).toBeDefined();
+ expect(resolveSecond).toBeDefined();
+ resolveSecond?.([{ source: "second" }]);
+ await waitFor(() => {
+ expect(screen.getByTestId("editor-data")).toHaveTextContent(
+ JSON.stringify([{ source: "second" }]),
+ );
+ });
+
+ await act(async () => {
+ resolveFirst?.([{ source: "first" }]);
+ await Promise.resolve();
+ });
+ expect(screen.getByTestId("editor-data")).toHaveTextContent(
+ JSON.stringify([{ source: "second" }]),
+ );
+ });
});
diff --git a/frontend/src/plugins/impl/data-editor/__tests__/data-utils.test.ts b/frontend/src/plugins/impl/data-editor/__tests__/data-utils.test.ts
index 3db9bba147..e76b3b1643 100644
--- a/frontend/src/plugins/impl/data-editor/__tests__/data-utils.test.ts
+++ b/frontend/src/plugins/impl/data-editor/__tests__/data-utils.test.ts
@@ -6,6 +6,7 @@ import type { DataType } from "@/core/kernel/messages";
import {
insertColumn,
modifyColumnFields,
+ orderColumnFields,
removeColumn,
renameColumn,
} from "../data-utils";
@@ -15,16 +16,43 @@ import {
const asFieldTypes = (obj: Record<string, DataType>): FieldTypes =>
new Map(Object.entries(obj));
-describe("removeColumn", () => {
- const testData = [
- { int: 1, string: "a", bool: "True", datetime: "2025-07-12 00:07:13" },
- { int: 2, string: "b", bool: "False", datetime: null },
- { int: 3, string: "c", bool: null, datetime: "2025-07-12 00:07:13" },
- { int: 0, string: "", bool: "", datetime: "" },
- ];
+const testData = [
+ { int: 1, string: "a", bool: "True", datetime: "2025-07-12 00:07:13" },
+ { int: 2, string: "b", bool: "False", datetime: null },
+ { int: 3, string: "c", bool: null, datetime: "2025-07-12 00:07:13" },
+ { int: 0, string: "", bool: "", datetime: "" },
+];
+
+const mixedData = [{ a: 1, b: 2 }, { a: 3, b: 4, c: 5 }, { a: 6 }];
+
+const dataWithNulls = [
+ { a: 1, b: null, c: undefined },
+ { a: null, b: 2, c: 3 },
+];
+
+describe("orderColumnFields", () => {
+ it("uses explicit order for numeric-looking column names", () => {
+ const inferred = new Map([
+ ["2", "string"],
+ ["10", "string"],
+ ]) as FieldTypes;
+
+ expect([...orderColumnFields(inferred, ["10", "2"])]).toEqual([
+ ["10", "string"],
+ ["2", "string"],
+ ]);
+ });
- it("should remove column at index 0", () => {
- const result = removeColumn(testData, 0);
+ it("creates unknown fields for an empty table", () => {
+ expect([...orderColumnFields(new Map(), ["value"])]).toEqual([
+ ["value", "unknown"],
+ ]);
+ });
+});
+
+describe("removeColumn", () => {
+ it("should remove the first column", () => {
+ const result = removeColumn(testData, "int");
expect(result).toEqual([
{ string: "a", bool: "True", datetime: "2025-07-12 00:07:13" },
@@ -34,8 +62,8 @@ describe("removeColumn", () => {
]);
});
- it("should remove column at index 1", () => {
- const result = removeColumn(testData, 1);
+ it("should remove the second column", () => {
+ const result = removeColumn(testData, "string");
expect(result).toEqual([
{ int: 1, bool: "True", datetime: "2025-07-12 00:07:13" },
@@ -45,8 +73,8 @@ describe("removeColumn", () => {
]);
});
- it("should remove column at index 2", () => {
- const result = removeColumn(testData, 2);
+ it("should remove the third column", () => {
+ const result = removeColumn(testData, "bool");
expect(result).toEqual([
{ int: 1, string: "a", datetime: "2025-07-12 00:07:13" },
@@ -56,8 +84,8 @@ describe("removeColumn", () => {
]);
});
- it("should remove column at index 3", () => {
- const result = removeColumn(testData, 3);
+ it("should remove the last column", () => {
+ const result = removeColumn(testData, "datetime");
expect(result).toEqual([
{ int: 1, string: "a", bool: "True" },
@@ -67,40 +95,26 @@ describe("removeColumn", () => {
]);
});
- it("should handle removing non-existent column index", () => {
- const result = removeColumn(testData, 999);
- // Should return the original data since the index doesn't exist
- expect(result).toEqual(testData);
- });
-
- it("should handle negative index", () => {
- const result = removeColumn(testData, -1);
- // Should return the original data since negative index is invalid
+ it("should handle removing a non-existent column", () => {
+ const result = removeColumn(testData, "missing");
expect(result).toEqual(testData);
});
it("should preserve original data structure", () => {
const originalData = [...testData];
- removeColumn(testData, 1);
+ removeColumn(testData, "string");
// Original data should remain unchanged
expect(testData).toEqual(originalData);
});
it("should handle objects with different property counts", () => {
- const mixedData = [{ a: 1, b: 2 }, { a: 3, b: 4, c: 5 }, { a: 6 }];
-
- const result = removeColumn(mixedData, 1);
+ const result = removeColumn(mixedData, "b");
expect(result).toEqual([{ a: 1 }, { a: 3, c: 5 }, { a: 6 }]);
});
it("should handle null and undefined values in data", () => {
- const dataWithNulls = [
- { a: 1, b: null, c: undefined },
- { a: null, b: 2, c: 3 },
- ];
-
- const result = removeColumn(dataWithNulls, 1);
+ const result = removeColumn(dataWithNulls, "b");
expect(result).toEqual([
{ a: 1, c: undefined },
@@ -110,15 +124,8 @@ describe("removeColumn", () => {
});
describe("insertColumn", () => {
- const testData = [
- { int: 1, string: "a", bool: "True", datetime: "2025-07-12 00:07:13" },
- { int: 2, string: "b", bool: "False", datetime: null },
- { int: 3, string: "c", bool: null, datetime: "2025-07-12 00:07:13" },
- { int: 0, string: "", bool: "", datetime: "" },
- ];
-
it("should insert column at index 0", () => {
- const result = insertColumn(testData, "newColumn");
+ const result = insertColumn(testData, "newColumn", 0);
const expected = [
{
@@ -139,10 +146,17 @@ describe("insertColumn", () => {
{ newColumn: "", int: 0, string: "", bool: "", datetime: "" },
];
expect(result).toEqual(expected);
+ expect(Object.keys(result[0])).toEqual([
+ "newColumn",
+ "int",
+ "string",
+ "bool",
+ "datetime",
+ ]);
});
it("should insert column at index 1", () => {
- const result = insertColumn(testData, "newColumn");
+ const result = insertColumn(testData, "newColumn", 1);
expect(result).toEqual([
{
@@ -162,10 +176,17 @@ describe("insertColumn", () => {
},
{ int: 0, newColumn: "", string: "", bool: "", datetime: "" },
]);
+ expect(Object.keys(result[0])).toEqual([
+ "int",
+ "newColumn",
+ "string",
+ "bool",
+ "datetime",
+ ]);
});
it("should insert column at index 2", () => {
- const result = insertColumn(testData, "newColumn");
+ const result = insertColumn(testData, "newColumn", 2);
expect(result).toEqual([
{
@@ -188,7 +209,7 @@ describe("insertColumn", () => {
});
it("should insert column at index 3", () => {
- const result = insertColumn(testData, "newColumn");
+ const result = insertColumn(testData, "newColumn", 3);
expect(result).toEqual([
{
@@ -211,7 +232,7 @@ describe("insertColumn", () => {
});
it("should insert column at the end when index equals length", () => {
- const result = insertColumn(testData, "newColumn");
+ const result = insertColumn(testData, "newColumn", 4);
expect(result).toEqual([
{
@@ -234,7 +255,7 @@ describe("insertColumn", () => {
});
it("should handle inserting at index beyond array length", () => {
- const result = insertColumn(testData, "newColumn");
+ const result = insertColumn(testData, "newColumn", 999);
// Should add the column at the end
expect(result).toEqual([
{
@@ -270,14 +291,12 @@ describe("insertColumn", () => {
it("should handle array with single object", () => {
const singleRowData = [{ a: 1, b: 2, c: 3 }];
- const result = insertColumn(singleRowData, "newColumn");
+ const result = insertColumn(singleRowData, "newColumn", 1);
expect(result).toEqual([{ a: 1, newColumn: "", b: 2, c: 3 }]);
});
it("should handle objects with different property counts", () => {
- const mixedData = [{ a: 1, b: 2 }, { a: 3, b: 4, c: 5 }, { a: 6 }];
-
- const result = insertColumn(mixedData, "newColumn");
+ const result = insertColumn(mixedData, "newColumn", 1);
expect(result).toEqual([
{ a: 1, newColumn: "", b: 2 },
@@ -287,12 +306,7 @@ describe("insertColumn", () => {
});
it("should handle null and undefined values in data", () => {
- const dataWithNulls = [
- { a: 1, b: null, c: undefined },
- { a: null, b: 2, c: 3 },
- ];
-
- const result = insertColumn(dataWithNulls, "newColumn");
+ const result = insertColumn(dataWithNulls, "newColumn", 1);
expect(result).toEqual([
{ a: 1, newColumn: "", b: null, c: undefined },
@@ -301,7 +315,7 @@ describe("insertColumn", () => {
});
it("should handle special characters in column name", () => {
- const result = insertColumn(testData, "new-column_with_123");
+ const result = insertColumn(testData, "new-column_with_123", 1);
expect(result).toEqual([
{
@@ -331,13 +345,6 @@ describe("insertColumn", () => {
});
describe("renameColumn", () => {
- const testData = [
- { int: 1, string: "a", bool: "True", datetime: "2025-07-12 00:07:13" },
- { int: 2, string: "b", bool: "False", datetime: null },
- { int: 3, string: "c", bool: null, datetime: "2025-07-12 00:07:13" },
- { int: 0, string: "", bool: "", datetime: "" },
- ];
-
it("should rename a column successfully", () => {
const result = renameColumn(testData, "int", "number");
@@ -373,12 +380,7 @@ describe("renameColumn", () => {
it("should handle renaming to an existing column name", () => {
const result = renameColumn(testData, "int", "string");
- expect(result).toEqual([
- { string: 1, bool: "True", datetime: "2025-07-12 00:07:13" },
- { string: 2, bool: "False", datetime: null },
- { string: 3, bool: null, datetime: "2025-07-12 00:07:13" },
- { string: 0, bool: "", datetime: "" },
- ]);
+ expect(result).toEqual(testData);
});
it("should handle non-existent column name gracefully", () => {
@@ -421,8 +423,6 @@ describe("renameColumn", () => {
});
it("should handle objects with different property counts", () => {
- const mixedData = [{ a: 1, b: 2 }, { a: 3, b: 4, c: 5 }, { a: 6 }];
-
const result = renameColumn(mixedData, "a", "alpha");
expect(result).toEqual([
@@ -433,11 +433,6 @@ describe("renameColumn", () => {
});
it("should handle null and undefined values in data", () => {
- const dataWithNulls = [
- { a: 1, b: null, c: undefined },
- { a: null, b: 2, c: 3 },
- ];
-
const result = renameColumn(dataWithNulls, "b", "beta");
expect(result).toEqual([
diff --git a/frontend/src/plugins/impl/data-editor/__tests__/editor-state.test.ts b/frontend/src/plugins/impl/data-editor/__tests__/editor-state.test.ts
new file mode 100644
index 0000000000..098a15544d
--- /dev/null
+++ b/frontend/src/plugins/impl/data-editor/__tests__/editor-state.test.ts
@@ -0,0 +1,113 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { describe, expect, it } from "vitest";
+import type { FieldTypes } from "@/components/data-table/types";
+import { applyEditorEdits } from "../editor-state";
+
+describe("applyEditorEdits", () => {
+ it("replays edits against an empty table", () => {
+ const result = applyEditorEdits(
+ { data: [], columnFields: new Map([["value", "unknown"]]) },
+ [{ rowIdx: 0, columnId: "value", value: "first" }],
+ );
+
+ expect(result.data).toEqual([{ value: "first" }]);
+ });
+
+ it("replays an append after deleting every row", () => {
+ const result = applyEditorEdits(
+ {
+ data: [{ A: 1, B: "a" }],
+ columnFields: new Map([
+ ["A", "number"],
+ ["B", "string"],
+ ]) as FieldTypes,
+ },
+ [
+ { rowIdx: 0, type: "remove" },
+ { rowIdx: 0, columnId: "A", value: 2 },
+ { rowIdx: 0, columnId: "B", value: "b" },
+ ],
+ );
+
+ expect(result.data).toEqual([{ A: 2, B: "b" }]);
+ });
+
+ it("uses the evolving column order", () => {
+ const result = applyEditorEdits(
+ {
+ data: [{ A: "a", B: "b" }],
+ columnFields: new Map([
+ ["A", "string"],
+ ["B", "string"],
+ ]) as FieldTypes,
+ },
+ [
+ { columnIdx: 0, type: "remove" },
+ { columnIdx: 0, type: "rename", newName: "D" },
+ ],
+ );
+
+ expect(result.data).toEqual([{ D: "b" }]);
+ expect([...result.columnFields]).toEqual([["D", "string"]]);
+ });
+
+ it("preserves inserted column order through later edits", () => {
+ const result = applyEditorEdits(
+ {
+ data: [{ A: "a", C: "c" }],
+ columnFields: new Map([
+ ["A", "string"],
+ ["C", "string"],
+ ]) as FieldTypes,
+ },
+ [
+ {
+ columnIdx: 1,
+ type: "insert",
+ newName: "B",
+ dataType: "boolean",
+ },
+ { columnIdx: 1, type: "rename", newName: "D" },
+ { columnIdx: 2, type: "remove" },
+ ],
+ );
+
+ expect(result.data).toEqual([{ A: "a", D: "" }]);
+ expect(Object.keys(result.data[0])).toEqual(["A", "D"]);
+ expect([...result.columnFields]).toEqual([
+ ["A", "string"],
+ ["D", "boolean"],
+ ]);
+ });
+
+ it("does not mutate the previous state", () => {
+ const state = {
+ data: [{ A: "before" }],
+ columnFields: new Map([["A", "string"]]) as FieldTypes,
+ };
+
+ const result = applyEditorEdits(state, [
+ { rowIdx: 0, columnId: "A", value: "after" },
+ { columnIdx: 1, type: "insert", newName: "B" },
+ ]);
+
+ expect(state.data).toEqual([{ A: "before" }]);
+ expect([...state.columnFields]).toEqual([["A", "string"]]);
+ expect(result.data).toEqual([{ A: "after", B: "" }]);
+ });
+
+ it("ignores negative row indices", () => {
+ const state = {
+ data: [{ A: "unchanged" }],
+ columnFields: new Map([["A", "string"]]) as FieldTypes,
+ };
+
+ expect(
+ applyEditorEdits(state, [
+ { rowIdx: -1, columnId: "A", value: "invalid" },
+ { rowIdx: -1, type: "remove" },
+ ]).data,
+ ).toEqual(state.data);
+ });
+});
diff --git a/frontend/src/plugins/impl/data-editor/__tests__/glide-data-editor.test.tsx b/frontend/src/plugins/impl/data-editor/__tests__/glide-data-editor.test.tsx
index d6293d192e..ab35c156e6 100644
--- a/frontend/src/plugins/impl/data-editor/__tests__/glide-data-editor.test.tsx
+++ b/frontend/src/plugins/impl/data-editor/__tests__/glide-data-editor.test.tsx
@@ -6,18 +6,31 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import { TooltipProvider } from "@/components/ui/tooltip";
import { GlideDataEditor } from "../glide-data-editor";
-const capturedPortalRef = vi.hoisted(() => ({
+const capturedDataEditor = vi.hoisted(() => ({
ref: undefined as React.RefObject<HTMLElement> | undefined,
+ onCellEdited: undefined as
+ | ((cell: [number, number], value: { data: unknown }) => void)
+ | undefined,
+ onRowAppended: undefined as (() => void) | undefined,
}));
vi.mock("@glideapps/glide-data-grid", async () => {
const React = await import("react");
return {
default: React.forwardRef(function MockDataEditor(
- props: { portalElementRef?: React.RefObject<HTMLElement> },
+ props: {
+ portalElementRef?: React.RefObject<HTMLElement>;
+ onCellEdited?: (
+ cell: [number, number],
+ value: { data: unknown },
+ ) => void;
+ onRowAppended?: () => void;
+ },
_ref: React.Ref<HTMLDivElement>,
) {
- capturedPortalRef.ref = props.portalElementRef;
+ capturedDataEditor.ref = props.portalElementRef;
+ capturedDataEditor.onCellEdited = props.onCellEdited;
+ capturedDataEditor.onRowAppended = props.onRowAppended;
return <div data-testid="mock-data-editor" />;
}),
CompactSelection: {
@@ -40,23 +53,16 @@ vi.mock("@/theme/useTheme", () => ({
const editorProps = {
data: [{ name: "alice" }],
- setData: vi.fn(),
columnFields: new Map([["name", "string"]]) as Map<string, "string">,
- setColumnFields: vi.fn(),
editableColumns: "all" as const,
- edits: [],
onAddEdits: vi.fn(),
- onAddRows: vi.fn(),
- onDeleteRows: vi.fn(),
- onRenameColumn: vi.fn(),
- onDeleteColumn: vi.fn(),
- onAddColumn: vi.fn(),
};
describe("GlideDataEditor portal", () => {
let fullscreenElement: Element | null;
beforeEach(() => {
+ vi.clearAllMocks();
fullscreenElement = null;
Object.defineProperty(document, "fullscreenElement", {
get: () => fullscreenElement,
@@ -79,12 +85,42 @@ describe("GlideDataEditor portal", () => {
const portal = screen.getByTestId("glide-data-editor-portal");
expect(portal.parentElement).toBe(document.body);
expect(container.contains(portal)).toBe(false);
- expect(capturedPortalRef.ref).toBeDefined();
+ expect(capturedDataEditor.ref).toBeDefined();
await waitFor(() => {
- expect(capturedPortalRef.ref?.current).toBe(portal);
+ expect(capturedDataEditor.ref?.current).toBe(portal);
});
});
+ it("emits cell edits", () => {
+ const onAddEdits = vi.fn();
+ render(
+ <TooltipProvider>
+ <GlideDataEditor {...editorProps} onAddEdits={onAddEdits} />
+ </TooltipProvider>,
+ );
+
+ act(() => capturedDataEditor.onCellEdited?.([0, 0], { data: "bob" }));
+
+ expect(onAddEdits).toHaveBeenCalledWith([
+ { rowIdx: 0, columnId: "name", value: "bob" },
+ ]);
+ });
+
+ it("emits one positional edit per cell when appending a row", () => {
+ const onAddEdits = vi.fn();
+ render(
+ <TooltipProvider>
+ <GlideDataEditor {...editorProps} onAddEdits={onAddEdits} />
+ </TooltipProvider>,
+ );
+
+ act(() => capturedDataEditor.onRowAppended?.());
+
+ expect(onAddEdits).toHaveBeenCalledWith([
+ { rowIdx: 1, columnId: "name", value: "" },
+ ]);
+ });
+
it("mounts into the fullscreen element when fullscreen is already active", () => {
const fullscreenContainer = document.createElement("div");
document.body.appendChild(fullscreenContainer);
diff --git a/frontend/src/plugins/impl/data-editor/__tests__/glide-utils.test.ts b/frontend/src/plugins/impl/data-editor/__tests__/glide-utils.test.ts
index 2776e504bd..0b48d19713 100644
--- a/frontend/src/plugins/impl/data-editor/__tests__/glide-utils.test.ts
+++ b/frontend/src/plugins/impl/data-editor/__tests__/glide-utils.test.ts
@@ -3,7 +3,7 @@
import type { GridSelection } from "@glideapps/glide-data-grid";
import { CompactSelection, GridCellKind } from "@glideapps/glide-data-grid";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import { pasteCells } from "../glide-utils";
+import { isValidCellValue, pasteCells } from "../glide-utils";
import type { ModifiedGridColumn } from "../types";
// Mock navigator.clipboard
@@ -16,6 +16,26 @@ Object.defineProperty(navigator, "clipboard", {
writable: true,
});
+describe("isValidCellValue", () => {
+ it("accepts only finite numbers", () => {
+ expect(isValidCellValue("number", 3.5)).toBe(true);
+ expect(isValidCellValue("number", Number.POSITIVE_INFINITY)).toBe(false);
+ expect(isValidCellValue("number", Number.NEGATIVE_INFINITY)).toBe(false);
+ expect(isValidCellValue("number", "Infinity")).toBe(false);
+ expect(isValidCellValue("number", Number.NaN)).toBe(false);
+ });
+
+ it("preserves integer precision", () => {
+ expect(isValidCellValue("integer", 3)).toBe(true);
+ expect(isValidCellValue("integer", 3.5)).toBe(false);
+ expect(isValidCellValue("integer", "9007199254740993")).toBe(true);
+ expect(isValidCellValue("integer", "9007199254740993.0")).toBe(true);
+ expect(isValidCellValue("integer", "9007199254740993.1")).toBe(false);
+ expect(isValidCellValue("integer", "1.0000000000000001")).toBe(false);
+ expect(isValidCellValue("integer", "1e-324")).toBe(false);
+ });
+});
+
describe("pasteCells", () => {
beforeEach(() => {
vi.clearAllMocks();
@@ -76,13 +96,11 @@ describe("pasteCells", () => {
it("should handle empty clipboard text", async () => {
mockClipboard.readText.mockResolvedValue("");
- const mockSetLocalData = vi.fn();
const mockOnAddEdits = vi.fn();
pasteCells({
selection: createMockSelection(0, 0),
data: createMockData(),
- setData: mockSetLocalData,
columns: createMockColumns(),
editableColumns: "all",
onAddEdits: mockOnAddEdits,
@@ -91,20 +109,17 @@ describe("pasteCells", () => {
// Wait for the async operation
await vi.waitFor(() => {
expect(mockOnAddEdits).not.toHaveBeenCalled();
- expect(mockSetLocalData).not.toHaveBeenCalled();
});
});
it("should handle whitespace-only clipboard text", async () => {
mockClipboard.readText.mockResolvedValue(" \n\t ");
- const mockSetLocalData = vi.fn();
const mockOnAddEdits = vi.fn();
pasteCells({
selection: createMockSelection(0, 0),
data: createMockData(),
- setData: mockSetLocalData,
columns: createMockColumns(),
editableColumns: "all",
onAddEdits: mockOnAddEdits,
@@ -112,20 +127,17 @@ describe("pasteCells", () => {
await vi.waitFor(() => {
expect(mockOnAddEdits).not.toHaveBeenCalled();
- expect(mockSetLocalData).not.toHaveBeenCalled();
});
});
it("should paste string data correctly", async () => {
mockClipboard.readText.mockResolvedValue("David\t40\ttrue");
- const mockSetLocalData = vi.fn();
const mockOnAddEdits = vi.fn();
pasteCells({
selection: createMockSelection(0, 0),
data: createMockData(),
- setData: mockSetLocalData,
columns: createMockColumns(),
editableColumns: "all",
onAddEdits: mockOnAddEdits,
@@ -140,16 +152,14 @@ describe("pasteCells", () => {
});
});
- it("should convert number values correctly", async () => {
+ it("should skip fractional values for integer columns", async () => {
mockClipboard.readText.mockResolvedValue("Eve\t25.5\tfalse");
- const mockSetLocalData = vi.fn();
const mockOnAddEdits = vi.fn();
pasteCells({
selection: createMockSelection(0, 0),
data: createMockData(),
- setData: mockSetLocalData,
columns: createMockColumns(),
editableColumns: "all",
onAddEdits: mockOnAddEdits,
@@ -158,7 +168,6 @@ describe("pasteCells", () => {
await vi.waitFor(() => {
expect(mockOnAddEdits).toHaveBeenCalledWith([
{ rowIdx: 0, columnId: "name", value: "Eve" },
- { rowIdx: 0, columnId: "age", value: 25.5 },
{ rowIdx: 0, columnId: "active", value: false },
]);
});
@@ -167,13 +176,11 @@ describe("pasteCells", () => {
it("should handle boolean conversion with different values", async () => {
mockClipboard.readText.mockResolvedValue("Frank\t30\t1");
- const mockSetLocalData = vi.fn();
const mockOnAddEdits = vi.fn();
pasteCells({
selection: createMockSelection(0, 0),
data: createMockData(),
- setData: mockSetLocalData,
columns: createMockColumns(),
editableColumns: "all",
onAddEdits: mockOnAddEdits,
@@ -191,13 +198,11 @@ describe("pasteCells", () => {
it("should skip invalid number values", async () => {
mockClipboard.readText.mockResolvedValue("Grace\tinvalid\tfalse");
- const mockSetLocalData = vi.fn();
const mockOnAddEdits = vi.fn();
pasteCells({
selection: createMockSelection(0, 0),
data: createMockData(),
- setData: mockSetLocalData,
columns: createMockColumns(),
editableColumns: "all",
onAddEdits: mockOnAddEdits,
@@ -211,16 +216,36 @@ describe("pasteCells", () => {
});
});
+ it("should preserve unsafe integer precision", async () => {
+ mockClipboard.readText.mockResolvedValue("Grace\t9007199254740993\tfalse");
+
+ const mockOnAddEdits = vi.fn();
+
+ pasteCells({
+ selection: createMockSelection(0, 0),
+ data: createMockData(),
+ columns: createMockColumns(),
+ editableColumns: "all",
+ onAddEdits: mockOnAddEdits,
+ });
+
+ await vi.waitFor(() => {
+ expect(mockOnAddEdits).toHaveBeenCalledWith([
+ { rowIdx: 0, columnId: "name", value: "Grace" },
+ { rowIdx: 0, columnId: "age", value: "9007199254740993" },
+ { rowIdx: 0, columnId: "active", value: false },
+ ]);
+ });
+ });
+
it("should handle multiple rows", async () => {
mockClipboard.readText.mockResolvedValue("Hank\t40\ttrue\nIvy\t35\tfalse");
- const mockSetLocalData = vi.fn();
const mockOnAddEdits = vi.fn();
pasteCells({
selection: createMockSelection(0, 0),
data: createMockData(),
- setData: mockSetLocalData,
columns: createMockColumns(),
editableColumns: "all",
onAddEdits: mockOnAddEdits,
@@ -243,13 +268,11 @@ describe("pasteCells", () => {
"Jack\t45\ttrue\nKate\t50\tfalse\nLiam\t55\ttrue",
);
- const mockSetLocalData = vi.fn();
const mockOnAddEdits = vi.fn();
pasteCells({
selection: createMockSelection(0, 0),
data: createMockData(), // Only 3 rows
- setData: mockSetLocalData,
columns: createMockColumns(),
editableColumns: "all",
onAddEdits: mockOnAddEdits,
@@ -273,13 +296,11 @@ describe("pasteCells", () => {
it("should respect column bounds - not exceed column count", async () => {
mockClipboard.readText.mockResolvedValue("Mia\t30\ttrue\textra");
- const mockSetLocalData = vi.fn();
const mockOnAddEdits = vi.fn();
pasteCells({
selection: createMockSelection(0, 0),
data: createMockData(),
- setData: mockSetLocalData,
columns: createMockColumns(), // Only 3 columns
editableColumns: "all",
onAddEdits: mockOnAddEdits,
@@ -297,13 +318,11 @@ describe("pasteCells", () => {
it("should handle starting position offset", async () => {
mockClipboard.readText.mockResolvedValue("Noah\t25\ttrue");
- const mockSetLocalData = vi.fn();
const mockOnAddEdits = vi.fn();
pasteCells({
selection: createMockSelection(1, 1), // Start at column 1, row 1
data: createMockData(),
- setData: mockSetLocalData,
columns: createMockColumns(),
editableColumns: "all",
onAddEdits: mockOnAddEdits,
@@ -317,7 +336,6 @@ describe("pasteCells", () => {
});
it("should handle no selection", async () => {
- const mockSetLocalData = vi.fn();
const mockOnAddEdits = vi.fn();
pasteCells({
@@ -327,7 +345,6 @@ describe("pasteCells", () => {
columns: CompactSelection.empty(),
},
data: createMockData(),
- setData: mockSetLocalData,
columns: createMockColumns(),
editableColumns: "all",
onAddEdits: mockOnAddEdits,
@@ -335,7 +352,6 @@ describe("pasteCells", () => {
await vi.waitFor(() => {
expect(mockOnAddEdits).not.toHaveBeenCalled();
- expect(mockSetLocalData).not.toHaveBeenCalled();
});
});
@@ -344,7 +360,6 @@ describe("pasteCells", () => {
new Error("Clipboard access denied"),
);
- const mockSetLocalData = vi.fn();
const mockOnAddEdits = vi.fn();
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {
// Do nothing
@@ -353,7 +368,6 @@ describe("pasteCells", () => {
pasteCells({
selection: createMockSelection(0, 0),
data: createMockData(),
- setData: mockSetLocalData,
columns: createMockColumns(),
editableColumns: "all",
onAddEdits: mockOnAddEdits,
@@ -361,53 +375,21 @@ describe("pasteCells", () => {
await vi.waitFor(() => {
expect(mockOnAddEdits).not.toHaveBeenCalled();
- expect(mockSetLocalData).not.toHaveBeenCalled();
});
consoleSpy.mockRestore();
});
- it("should update local data when edits are applied", async () => {
- mockClipboard.readText.mockResolvedValue("Olivia\t28\ttrue");
-
- const mockSetLocalData = vi.fn();
- const mockOnAddEdits = vi.fn();
-
- pasteCells({
- selection: createMockSelection(0, 0),
- data: createMockData(),
- setData: mockSetLocalData,
- columns: createMockColumns(),
- editableColumns: "all",
- onAddEdits: mockOnAddEdits,
- });
-
- await vi.waitFor(() => {
- expect(mockSetLocalData).toHaveBeenCalled();
-
- // Verify the updater function is called
- const updater = mockSetLocalData.mock.calls[0][0];
- const originalData = createMockData();
- const updatedData = updater(originalData);
-
- expect(updatedData[0].name).toBe("Olivia");
- expect(updatedData[0].age).toBe(28);
- expect(updatedData[0].active).toBe(true);
- });
- });
-
it("should handle empty rows in clipboard data", async () => {
mockClipboard.readText.mockResolvedValue(
"Paul\t30\ttrue\n\nRachel\t25\tfalse",
);
- const mockSetLocalData = vi.fn();
const mockOnAddEdits = vi.fn();
pasteCells({
selection: createMockSelection(0, 0),
data: createMockData(),
- setData: mockSetLocalData,
columns: createMockColumns(),
editableColumns: "all",
onAddEdits: mockOnAddEdits,
@@ -430,13 +412,11 @@ describe("pasteCells", () => {
"Olivia\t28\ttrue\nWilliams\t35\tfalse",
);
- const mockSetLocalData = vi.fn();
const mockOnAddEdits = vi.fn();
pasteCells({
selection: createMockSelection(0, 0),
data: createMockData(),
- setData: mockSetLocalData,
columns: createMockColumns(),
editableColumns: ["age"],
onAddEdits: mockOnAddEdits,
diff --git a/frontend/src/plugins/impl/data-editor/data-utils.ts b/frontend/src/plugins/impl/data-editor/data-utils.ts
index 8a8b3201a9..ddaa0bd59b 100644
--- a/frontend/src/plugins/impl/data-editor/data-utils.ts
+++ b/frontend/src/plugins/impl/data-editor/data-utils.ts
@@ -2,79 +2,99 @@
import type { FieldTypes } from "@/components/data-table/types";
import type { DataType } from "@/core/kernel/messages";
+import { logNever } from "@/utils/assertNever";
import { Logger } from "@/utils/Logger";
-
-/**
- * For modifying data, we do not worry about the order of the columns.
- * Because we define getCellContent based on columnTitle, the order does not matter.
- *
- * For column fields, we do care about the order
- */
-
-export function removeColumn<T>(data: T[], columnIdx: number): T[] {
- return data.map((row) => {
- const rowData = row as Record<string, unknown>;
- const keys = Object.keys(rowData);
-
- // If the column index is out of bounds, return the original row
- if (columnIdx < 0 || columnIdx >= keys.length) {
- return rowData as T;
+import { BulkEdit, type EditorRow } from "./types";
+
+export function orderColumnFields(
+ columnFields: FieldTypes,
+ columnNames: string[],
+): FieldTypes {
+ const ordered = new Map<string, DataType>();
+ for (const columnName of columnNames) {
+ ordered.set(columnName, columnFields.get(columnName) ?? "unknown");
+ }
+ for (const [columnName, dataType] of columnFields) {
+ if (!ordered.has(columnName)) {
+ ordered.set(columnName, dataType);
}
+ }
+ return ordered;
+}
- const keyToRemove = keys[columnIdx];
-
- // Create new object without the specified key
- const { [keyToRemove]: _, ...rest } = rowData;
- return rest as T;
+export function removeColumn(
+ data: EditorRow[],
+ columnName: string,
+): EditorRow[] {
+ return data.map((row) => {
+ const { [columnName]: _, ...rest } = row;
+ return rest;
});
}
-/**
- * Insert a new column at the end of the data.
- * @param data - The data to insert the column into
- * @param newName - The name of the new column
- * @returns The data with the new column inserted at the end
- */
-export function insertColumn<T>(data: T[], newName?: string): T[] {
+export function insertColumn(
+ data: EditorRow[],
+ newName?: string,
+ columnIdx?: number,
+): EditorRow[] {
if (!newName) {
return data;
}
- return data.map((row) => ({
- ...(row as Record<string, unknown>),
- [newName]: "",
- })) as T[];
+ return data.map((row) => {
+ const entries = Object.entries(row);
+ const insertAt = Math.max(
+ 0,
+ Math.min(columnIdx ?? entries.length, entries.length),
+ );
+ entries.splice(insertAt, 0, [newName, ""]);
+ return Object.fromEntries(entries);
+ });
}
-export function renameColumn<T>(
- data: T[],
+export function renameColumn(
+ data: EditorRow[],
oldName: string,
newName: string,
-): T[] {
+): EditorRow[] {
if (!oldName || !newName || oldName === newName) {
return data;
}
+ if (data.some((row) => Object.hasOwn(row, newName))) {
+ return data;
+ }
return data.map((row) => {
- const rowData = row as Record<string, unknown>;
- const { [oldName]: _, ...rest } = rowData;
- return { ...rest, [newName]: rowData[oldName] } as T;
+ return Object.fromEntries(
+ Object.entries(row).map(([columnName, value]) => [
+ columnName === oldName ? newName : columnName,
+ value,
+ ]),
+ );
});
}
-// Order of columns is important
-export function modifyColumnFields(opts: {
+type ModifyColumnFieldsOptions = {
columnFields: FieldTypes;
columnIdx: number;
- type: "insert" | "remove" | "rename";
- dataType?: DataType;
- newColumnName?: string;
-}): FieldTypes {
- const { columnFields, columnIdx, type, dataType, newColumnName } = opts;
+} & (
+ | { type: typeof BulkEdit.Remove }
+ | {
+ type: typeof BulkEdit.Insert | typeof BulkEdit.Rename;
+ newColumnName: string;
+ dataType?: DataType;
+ }
+);
- switch (type) {
- case "insert": {
- if (!newColumnName) {
+// Order of columns is important
+export function modifyColumnFields(
+ opts: ModifyColumnFieldsOptions,
+): FieldTypes {
+ const { columnFields, columnIdx } = opts;
+
+ switch (opts.type) {
+ case BulkEdit.Insert: {
+ if (!opts.newColumnName) {
Logger.error("newName is required for insert");
return columnFields;
}
@@ -82,12 +102,12 @@ export function modifyColumnFields(opts: {
const entries = [...columnFields.entries()];
const newEntries: Array<[string, DataType]> = [
...entries.slice(0, columnIdx),
- [newColumnName, dataType || "string"],
+ [opts.newColumnName, opts.dataType ?? "string"],
...entries.slice(columnIdx),
];
return new Map(newEntries);
}
- case "remove": {
+ case BulkEdit.Remove: {
if (columnIdx < 0 || columnIdx >= columnFields.size) {
return columnFields;
}
@@ -101,8 +121,8 @@ export function modifyColumnFields(opts: {
}
return columnFields;
}
- case "rename": {
- if (!newColumnName) {
+ case BulkEdit.Rename: {
+ if (!opts.newColumnName) {
Logger.error("newName is required for rename");
return columnFields;
}
@@ -115,10 +135,13 @@ export function modifyColumnFields(opts: {
const entries = [...columnFields.entries()];
const newEntries: Array<[string, DataType]> = [
...entries.slice(0, columnIdx),
- [newColumnName, dataType || "string"],
+ [opts.newColumnName, opts.dataType ?? "string"],
...entries.slice(columnIdx + 1),
];
return new Map(newEntries);
}
+ default:
+ logNever(opts);
+ return columnFields;
}
}
diff --git a/frontend/src/plugins/impl/data-editor/editor-state.ts b/frontend/src/plugins/impl/data-editor/editor-state.ts
new file mode 100644
index 0000000000..b14bb9d44c
--- /dev/null
+++ b/frontend/src/plugins/impl/data-editor/editor-state.ts
@@ -0,0 +1,108 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import type { FieldTypes } from "@/components/data-table/types";
+import { logNever } from "@/utils/assertNever";
+import {
+ insertColumn,
+ modifyColumnFields,
+ removeColumn,
+ renameColumn,
+} from "./data-utils";
+import { isColumnEdit, isPositionalEdit, isRowEdit } from "./glide-utils";
+import { BulkEdit, type EditorState, type Edits } from "./types";
+
+export function applyEditorEdits(
+ state: EditorState,
+ edits: Edits["edits"],
+): EditorState {
+ let nextData = [...state.data];
+ let nextColumnFields: FieldTypes = new Map(state.columnFields);
+
+ for (const edit of edits) {
+ if (isPositionalEdit(edit)) {
+ if (edit.rowIdx < 0) {
+ continue;
+ }
+ while (nextData.length <= edit.rowIdx) {
+ nextData.push({});
+ }
+ nextData[edit.rowIdx] = {
+ ...nextData[edit.rowIdx],
+ [edit.columnId]: edit.value,
+ };
+ if (!nextColumnFields.has(edit.columnId)) {
+ nextColumnFields.set(edit.columnId, "unknown");
+ }
+ continue;
+ }
+
+ if (isRowEdit(edit)) {
+ if (
+ edit.type === BulkEdit.Remove &&
+ edit.rowIdx >= 0 &&
+ edit.rowIdx < nextData.length
+ ) {
+ nextData.splice(edit.rowIdx, 1);
+ }
+ continue;
+ }
+
+ if (!isColumnEdit(edit)) {
+ continue;
+ }
+
+ const columnName = [...nextColumnFields.keys()][edit.columnIdx];
+ switch (edit.type) {
+ case BulkEdit.Remove:
+ if (columnName === undefined) {
+ break;
+ }
+ nextData = removeColumn(nextData, columnName);
+ nextColumnFields = modifyColumnFields({
+ columnFields: nextColumnFields,
+ columnIdx: edit.columnIdx,
+ type: "remove",
+ });
+ break;
+ case BulkEdit.Insert:
+ if (
+ !edit.newName ||
+ edit.columnIdx < 0 ||
+ edit.columnIdx > nextColumnFields.size ||
+ nextColumnFields.has(edit.newName)
+ ) {
+ break;
+ }
+ nextData = insertColumn(nextData, edit.newName, edit.columnIdx);
+ nextColumnFields = modifyColumnFields({
+ columnFields: nextColumnFields,
+ columnIdx: edit.columnIdx,
+ type: "insert",
+ dataType: edit.dataType,
+ newColumnName: edit.newName,
+ });
+ break;
+ case BulkEdit.Rename:
+ if (
+ columnName === undefined ||
+ !edit.newName ||
+ nextColumnFields.has(edit.newName)
+ ) {
+ break;
+ }
+ nextData = renameColumn(nextData, columnName, edit.newName);
+ nextColumnFields = modifyColumnFields({
+ columnFields: nextColumnFields,
+ columnIdx: edit.columnIdx,
+ type: "rename",
+ dataType: nextColumnFields.get(columnName),
+ newColumnName: edit.newName,
+ });
+ break;
+ default:
+ logNever(edit);
+ }
+ }
+
+ return { data: nextData, columnFields: nextColumnFields };
+}
diff --git a/frontend/src/plugins/impl/data-editor/glide-data-editor.tsx b/frontend/src/plugins/impl/data-editor/glide-data-editor.tsx
index 902ac95e17..f187d2cb2e 100644
--- a/frontend/src/plugins/impl/data-editor/glide-data-editor.tsx
+++ b/frontend/src/plugins/impl/data-editor/glide-data-editor.tsx
@@ -14,13 +14,7 @@ import DataEditor, {
type Rectangle,
} from "@glideapps/glide-data-grid";
import { CopyIcon, TrashIcon } from "lucide-react";
-import React, {
- useCallback,
- useEffect,
- useMemo,
- useRef,
- useState,
-} from "react";
+import React, { useCallback, useMemo, useRef, useState } from "react";
import useEvent from "react-use-event-hook";
import type { FieldTypes } from "@/components/data-table/types";
import {
@@ -34,59 +28,39 @@ import { copyToClipboard } from "@/utils/copy";
import {
getColumnHeaderIcon,
getColumnKind,
- isColumnEdit,
- isPositionalEdit,
- isRowEdit,
+ isValidCellValue,
pasteCells,
} from "./glide-utils";
import { getGlideTheme } from "./themes";
-import { BulkEdit, type Edits, type ModifiedGridColumn } from "./types";
import "@glideapps/glide-data-grid/dist/index.css"; // TODO: We are reimporting this
import { ErrorBoundary } from "@/components/editor/boundary/ErrorBoundary";
import { Button } from "@/components/ui/button";
import { toast } from "@/components/ui/use-toast";
import type { DataType } from "@/core/kernel/messages";
-import { useNonce } from "@/hooks/useNonce";
import { logNever } from "@/utils/assertNever";
import { Events } from "@/utils/events";
import { AddColumnSub, RenameColumnSub } from "./components";
-import {
- insertColumn,
- modifyColumnFields,
- removeColumn,
- renameColumn,
-} from "./data-utils";
import { GlideDataEditorPortal } from "./glide-portal";
-
-interface GlideDataEditorProps<T> {
- data: T[];
- setData: (data: T[] | ((prev: T[]) => T[])) => void;
+import {
+ BulkEdit,
+ type EditorRow,
+ type Edits,
+ type ModifiedGridColumn,
+} from "./types";
+
+interface GlideDataEditorProps {
+ data: EditorRow[];
columnFields: FieldTypes;
- setColumnFields: React.Dispatch<React.SetStateAction<FieldTypes>>;
editableColumns: string[] | "all";
- edits: Edits["edits"];
onAddEdits: (edits: Edits["edits"]) => void;
- onAddRows: (newRows: object[]) => void;
- onDeleteRows: (rows: number[]) => void;
- onRenameColumn: (columnIdx: number, newName: string) => void;
- onDeleteColumn: (columnIdx: number) => void;
- onAddColumn: (columnIdx: number, newName: string) => void;
}
-export const GlideDataEditor = <T,>({
+export const GlideDataEditor = ({
data,
- setData,
columnFields,
- setColumnFields,
editableColumns,
- edits,
onAddEdits,
- onAddRows,
- onDeleteRows,
- onRenameColumn,
- onDeleteColumn,
- onAddColumn,
-}: GlideDataEditorProps<T>) => {
+}: GlideDataEditorProps) => {
const { theme } = useTheme();
const dataEditorRef = useRef<DataEditorRef>(null);
const portalElementRef = useRef<HTMLDivElement>(null);
@@ -99,8 +73,6 @@ export const GlideDataEditor = <T,>({
});
const [columnWidths, setColumnWidths] = useState<Record<string, number>>({});
- const rerender = useNonce();
- const hasAppliedEdits = useRef(false);
const columns: ModifiedGridColumn[] = useMemo(() => {
const columns: ModifiedGridColumn[] = [];
@@ -130,114 +102,12 @@ export const GlideDataEditor = <T,>({
return columns;
}, [columnFields, columnWidths, editableColumns, theme]);
- // Apply initial edits after data has loaded
- useEffect(() => {
- // Don't apply if already applied or data hasn't loaded yet
- if (hasAppliedEdits.current || data.length === 0) {
- return;
- }
-
- // Mark as applied once data loads - prevents re-applying user edits
- hasAppliedEdits.current = true;
-
- // No initial edits to apply
- if (edits.length === 0) {
- return;
- }
-
- // Group edits by row index to build new rows
- const newRows = new Map<number, Record<string, unknown>>();
-
- for (const edit of edits) {
- if (isPositionalEdit(edit)) {
- if (edit.rowIdx >= data.length) {
- // This is a new row
- if (!newRows.has(edit.rowIdx)) {
- newRows.set(edit.rowIdx, {});
- }
- const row = newRows.get(edit.rowIdx);
- if (row) {
- row[edit.columnId] = edit.value;
- }
- } else {
- // This is an existing row, update the data
- setData((prev) => {
- const newData = [...prev];
- newData[edit.rowIdx][edit.columnId as keyof T] =
- edit.value as T[keyof T];
- return newData;
- });
- }
- } else if (isRowEdit(edit) && edit.type === BulkEdit.Remove) {
- // Add rows is currently handled under positional edits, so we only cover deletes here
- setData((prev) => prev.filter((_, i) => i !== edit.rowIdx));
- } else if (isColumnEdit(edit)) {
- switch (edit.type) {
- case BulkEdit.Remove:
- // Remove the column from the data
- setData((prev) => removeColumn(prev, edit.columnIdx));
- setColumnFields((prev) =>
- modifyColumnFields({
- columnFields: prev,
- columnIdx: edit.columnIdx,
- type: "remove",
- }),
- );
- break;
- case BulkEdit.Insert:
- setColumnFields((prev) =>
- modifyColumnFields({
- columnFields: prev,
- columnIdx: edit.columnIdx,
- type: "insert",
- newColumnName: edit.newName,
- }),
- );
- setData((prev) => insertColumn(prev, edit.newName));
- break;
- case BulkEdit.Rename: {
- const oldName = columns[edit.columnIdx].title;
- const newName = edit.newName;
- if (!oldName || !newName) {
- return;
- }
-
- setColumnFields((prev) =>
- modifyColumnFields({
- columnFields: prev,
- columnIdx: edit.columnIdx,
- type: "rename",
- newColumnName: newName,
- }),
- );
-
- setData((prev) => renameColumn(prev, oldName, newName));
- break;
- }
- }
- }
- }
-
- // Add new rows in order
- const sortedNewRows = [...newRows.entries()]
- .toSorted(([a], [b]) => a - b)
- .map(([, row]) => row);
-
- if (sortedNewRows.length > 0) {
- setData((prev) => [...prev, ...(sortedNewRows as T[])]);
- }
-
- // Force re-render to update the total rows
- rerender();
- // oxlint-disable-next-line react-hooks/exhaustive-deps
- }, [data.length]);
-
const getCellContent = useCallback(
(cell: Item): GridCell => {
const [col, row] = cell;
const dataRow = data[row];
- const dataItem = dataRow[columns[col].title as keyof T];
+ const dataItem = dataRow[columns[col].title];
const columnKind = columns[col].kind;
const editable =
editableColumns === "all" ||
@@ -289,15 +159,9 @@ export const GlideDataEditor = <T,>({
newData = null;
}
- setData((prev) => {
- const data = [...prev];
- data[row][key as keyof T] = newData as T[keyof T];
- return data;
- });
-
onAddEdits([{ rowIdx: row, columnId: key, value: newData }]);
},
- [columns, onAddEdits, setData],
+ [columns, onAddEdits],
);
const onColumnResize = useCallback((column: GridColumn, newSize: number) => {
@@ -314,22 +178,7 @@ export const GlideDataEditor = <T,>({
const key = columns[col].title;
const columnType = columnFields.get(key);
- // Verify the new value is of the correct type
- switch (columnType) {
- case "number":
- case "integer":
- if (Number.isNaN(Number(newValue.data))) {
- return false;
- }
- break;
- case "boolean":
- if (typeof newValue.data !== "boolean") {
- return false;
- }
- break;
- }
-
- return true;
+ return isValidCellValue(columnType, newValue.data);
},
[columnFields, columns],
);
@@ -351,7 +200,6 @@ export const GlideDataEditor = <T,>({
pasteCells({
selection,
data,
- setData,
columns,
editableColumns,
onAddEdits,
@@ -371,11 +219,11 @@ export const GlideDataEditor = <T,>({
return;
}
},
- [columns, data, editableColumns, onAddEdits, selection, setData],
+ [columns, data, editableColumns, onAddEdits, selection],
);
const onRowAppend = useCallback(() => {
- const newRow: Record<string, unknown> = Object.fromEntries(
+ const newRow: EditorRow = Object.fromEntries(
columns.map((column) => {
const dataType = column.dataType;
switch (dataType) {
@@ -399,22 +247,23 @@ export const GlideDataEditor = <T,>({
}
}),
);
- onAddRows([newRow]);
-
- // Update data
- setData((prev) => [...prev, newRow as T]);
- }, [columns, onAddRows, setData]);
+ onAddEdits(
+ Object.entries(newRow).map(([columnId, value]) => ({
+ rowIdx: data.length,
+ columnId,
+ value,
+ })),
+ );
+ }, [columns, data.length, onAddEdits]);
const handleDeleteRows = () => {
const rows = selection.rows.toArray();
- onDeleteRows(rows);
-
- let index = 0;
- for (const row of rows) {
- const adjustedRow = row - index; // Adjust for previously deleted rows
- setData((prev) => prev.filter((_, i) => i !== adjustedRow));
- index++;
- }
+ onAddEdits(
+ rows.map((rowIdx, index) => ({
+ rowIdx: rowIdx - index,
+ type: BulkEdit.Remove,
+ })),
+ );
// Clear selection
setSelection({
@@ -445,45 +294,20 @@ export const GlideDataEditor = <T,>({
const handleRenameColumn = (newName: string) => {
if (menu) {
- const oldColumnName = columns[menu.col].title;
-
// Validate the new column name
if (columnFields.has(newName)) {
toastColumnExists(newName);
return;
}
- const dataType = columns[menu.col].dataType;
-
- onRenameColumn(menu.col, newName);
- setColumnFields((prev) =>
- modifyColumnFields({
- columnFields: prev,
- columnIdx: menu.col,
- type: "rename",
- dataType,
- newColumnName: newName,
- }),
- );
-
- // Update the data
- setData((prev) => renameColumn(prev, oldColumnName, newName));
+ onAddEdits([{ columnIdx: menu.col, newName, type: BulkEdit.Rename }]);
setMenu(undefined);
}
};
const handleDeleteColumn = () => {
if (menu) {
- onDeleteColumn(menu.col);
- setColumnFields((prev) =>
- modifyColumnFields({
- columnFields: prev,
- columnIdx: menu.col,
- type: "remove",
- }),
- );
-
- setData((prev) => removeColumn(prev, menu.col));
+ onAddEdits([{ columnIdx: menu.col, type: BulkEdit.Remove }]);
setMenu(undefined);
}
};
@@ -506,21 +330,14 @@ export const GlideDataEditor = <T,>({
return;
}
- onAddColumn(clampedColumnIdx, columnName);
-
- setColumnFields((prev) =>
- modifyColumnFields({
- columnFields: prev,
+ onAddEdits([
+ {
columnIdx: clampedColumnIdx,
- type: "insert",
+ newName: columnName,
dataType,
- newColumnName: columnName,
- }),
- );
-
- // Update the data - add the new column to all rows,
- // ordering does not matter as we call getCellContent based on columnTitle
- setData((prev) => insertColumn(prev, columnName));
+ type: BulkEdit.Insert,
+ },
+ ]);
setMenu(undefined);
}
};
diff --git a/frontend/src/plugins/impl/data-editor/glide-utils.ts b/frontend/src/plugins/impl/data-editor/glide-utils.ts
index 36696d8901..187cf57148 100644
--- a/frontend/src/plugins/impl/data-editor/glide-utils.ts
+++ b/frontend/src/plugins/impl/data-editor/glide-utils.ts
@@ -10,10 +10,11 @@ import { logNever } from "@/utils/assertNever";
import { Logger } from "@/utils/Logger";
import type {
ColumnEdit,
+ EditorRow,
Edits,
ModifiedGridColumn,
PositionalEdit,
- RowEdit,
+ RemoveRowEdit,
} from "./types";
export function getColumnKind(fieldType: DataType): GridCellKind {
@@ -24,7 +25,15 @@ export function getColumnKind(fieldType: DataType): GridCellKind {
return GridCellKind.Number;
case "boolean":
return GridCellKind.Boolean;
+ case "integer":
+ case "date":
+ case "datetime":
+ case "time":
+ case "geometry":
+ case "unknown":
+ return GridCellKind.Text;
default:
+ logNever(fieldType);
return GridCellKind.Text;
}
}
@@ -52,13 +61,48 @@ export function getColumnHeaderIcon(fieldType: DataType): GridColumnIcon {
}
}
+export function isValidCellValue(
+ dataType: DataType | undefined,
+ value: unknown,
+): boolean {
+ switch (dataType) {
+ case "number":
+ return Number.isFinite(Number(value));
+ case "integer":
+ if (typeof value === "bigint") {
+ return true;
+ }
+ if (typeof value === "number") {
+ return Number.isFinite(value) && Number.isInteger(value);
+ }
+ if (typeof value === "string") {
+ const normalized = value.trim();
+ return /^[+-]?[0-9]+(?:\.0+)?$/u.test(normalized);
+ }
+ return false;
+ case "boolean":
+ return typeof value === "boolean";
+ case undefined:
+ case "string":
+ case "date":
+ case "datetime":
+ case "time":
+ case "geometry":
+ case "unknown":
+ return true;
+ default:
+ logNever(dataType);
+ return false;
+ }
+}
+
export function isPositionalEdit(
edit: Edits["edits"][number],
): edit is PositionalEdit {
return "rowIdx" in edit && "columnId" in edit && "value" in edit;
}
-export function isRowEdit(edit: Edits["edits"][number]): edit is RowEdit {
+export function isRowEdit(edit: Edits["edits"][number]): edit is RemoveRowEdit {
return "rowIdx" in edit && "type" in edit;
}
@@ -66,16 +110,14 @@ export function isColumnEdit(edit: Edits["edits"][number]): edit is ColumnEdit {
return "columnIdx" in edit && "type" in edit;
}
-export function pasteCells<T>(options: {
+export function pasteCells(options: {
selection: GridSelection;
- data: T[];
- setData: (updater: (prev: T[]) => T[]) => void;
+ data: EditorRow[];
columns: ModifiedGridColumn[];
editableColumns: string[] | "all";
onAddEdits: (edits: Edits["edits"]) => void;
}) {
- const { selection, data, setData, onAddEdits, columns, editableColumns } =
- options;
+ const { selection, data, onAddEdits, columns, editableColumns } = options;
if (!selection.current) {
return;
}
@@ -126,7 +168,7 @@ export function pasteCells<T>(options: {
const targetColIdx = startCol + colIndex;
// Check if we've exceeded the column bounds
- if (!columns || targetColIdx >= columns.length) {
+ if (targetColIdx >= columns.length) {
break;
}
@@ -143,10 +185,19 @@ export function pasteCells<T>(options: {
let convertedValue: unknown = cellValue;
switch (columnType) {
- case "integer":
+ case "integer": {
+ const numValue = Number(cellValue);
+ if (!isValidCellValue(columnType, cellValue)) {
+ continue;
+ }
+ convertedValue = Number.isSafeInteger(numValue)
+ ? numValue
+ : cellValue.trim();
+ break;
+ }
case "number": {
const numValue = Number(cellValue);
- if (Number.isNaN(numValue)) {
+ if (!isValidCellValue(columnType, numValue)) {
continue;
}
convertedValue = numValue;
@@ -157,6 +208,16 @@ export function pasteCells<T>(options: {
convertedValue = boolValue === "true" || boolValue === "1";
break;
}
+ case "string":
+ case "date":
+ case "datetime":
+ case "time":
+ case "geometry":
+ case "unknown":
+ break;
+ default:
+ logNever(columnType);
+ continue;
}
// Get the column ID from the columns array using the title
@@ -172,27 +233,6 @@ export function pasteCells<T>(options: {
if (edits.length > 0) {
onAddEdits(edits);
-
- setData((prev: T[]) => {
- const newData = [...prev];
-
- // Apply all edits to the data
- for (const edit of edits) {
- if (isPositionalEdit(edit)) {
- const rowIdx = edit.rowIdx;
- const columnId = edit.columnId;
-
- if (rowIdx < newData.length) {
- const row = newData[rowIdx] as Record<string, unknown>;
- if (columnId in row) {
- row[columnId] = edit.value;
- }
- }
- }
- }
-
- return newData;
- });
}
})
.catch((error) => {
diff --git a/frontend/src/plugins/impl/data-editor/types.ts b/frontend/src/plugins/impl/data-editor/types.ts
index b1d739c3de..7825737df8 100644
--- a/frontend/src/plugins/impl/data-editor/types.ts
+++ b/frontend/src/plugins/impl/data-editor/types.ts
@@ -1,9 +1,16 @@
/* Copyright 2026 Marimo. All rights reserved. */
import type { GridCellKind, GridColumn } from "@glideapps/glide-data-grid";
-import type { FieldTypesWithExternalType } from "@/components/data-table/types";
+import type { FieldTypes } from "@/components/data-table/types";
import type { DataType } from "@/core/kernel/messages";
+export type EditorRow = Record<string, unknown>;
+
+export interface EditorState {
+ data: EditorRow[];
+ columnFields: FieldTypes;
+}
+
export interface PositionalEdit {
rowIdx: number;
columnId: string;
@@ -16,31 +23,38 @@ export const BulkEdit = {
Rename: "rename",
} as const;
-type BulkEdit = (typeof BulkEdit)[keyof typeof BulkEdit];
-
-export interface RowEdit {
+export interface RemoveRowEdit {
rowIdx: number;
- type: BulkEdit;
+ type: typeof BulkEdit.Remove;
+}
+
+export interface RemoveColumnEdit {
+ columnIdx: number;
+ type: typeof BulkEdit.Remove;
}
-export interface ColumnEdit {
+export interface RenameColumnEdit {
columnIdx: number;
- newName?: string;
- type: BulkEdit;
+ newName: string;
+ type: typeof BulkEdit.Rename;
}
+export interface InsertColumnEdit {
+ columnIdx: number;
+ newName: string;
+ type: typeof BulkEdit.Insert;
+ dataType?: DataType;
+}
+
+export type ColumnEdit = RemoveColumnEdit | RenameColumnEdit | InsertColumnEdit;
+
+export type Edit = PositionalEdit | RemoveRowEdit | ColumnEdit;
+
export interface Edits {
- edits: (PositionalEdit | RowEdit | ColumnEdit)[];
+ edits: Edit[];
}
export type ModifiedGridColumn = GridColumn & {
kind: GridCellKind;
dataType: DataType;
};
-
-export interface DataEditorProps<T> {
- data: T[];
- fieldTypes: FieldTypesWithExternalType | null | undefined;
- onAddEdits: (edits: Edits["edits"]) => void;
- onAddRows: (newRows: object[]) => void;
-}
diff --git a/marimo/_plugins/ui/_impl/data_editor.py b/marimo/_plugins/ui/_impl/data_editor.py
index f24574c040..320fbde6d7 100644
--- a/marimo/_plugins/ui/_impl/data_editor.py
+++ b/marimo/_plugins/ui/_impl/data_editor.py
@@ -5,6 +5,7 @@ import ast
import datetime
from copy import deepcopy
from dataclasses import dataclass
+from decimal import Decimal, InvalidOperation
from typing import (
TYPE_CHECKING,
Any,
@@ -20,31 +21,22 @@ from narwhals.typing import IntoDataFrame
import marimo._output.data.data as mo_data
from marimo import _loggers
+from marimo._data.models import DataType
from marimo._output.rich_help import mddoc
from marimo._plugins.ui._core.ui_element import UIElement
from marimo._plugins.ui._impl.tables.utils import get_table_manager
+from marimo._utils.assert_never import log_never
from marimo._utils.deprecated import deprecated
LOGGER = _loggers.marimo_logger()
if TYPE_CHECKING:
- from collections.abc import Callable
+ from collections.abc import Callable, Sequence
from narwhals.dtypes import DType
from typing_extensions import TypeIs
-@dataclass
-class DataEditorValue:
- """A dataclass representing the value of a data editor.
-
- Attributes:
- data (List[Dict[str, Any]]): Row-oriented data as a list of dictionaries.
- """
-
- data: list[dict[str, Any]]
-
-
class PositionalEdit(TypedDict):
"""A typed dictionary representing a single edit in the data editor.
@@ -59,33 +51,40 @@ class PositionalEdit(TypedDict):
value: Any
-class ColumnEdit(TypedDict):
- """A typed dictionary representing a bulk edit of a column.
+class RemoveColumnEdit(TypedDict):
+ columnIdx: int
+ type: Literal["remove"]
- Attributes:
- columnIdx (int): The index of the column being edited.
- If insert/remove, this is the index of the column to be edited. If rename, this is the index of the column to be renamed.
- newName (Optional[str]): The new name of the column.
- type (Literal["insert", "remove", "rename"]): The type of edit.
- """
+class RenameColumnEdit(TypedDict):
columnIdx: int
- newName: str | None
- type: Literal["insert", "remove", "rename"]
+ newName: str
+ type: Literal["rename"]
+
+
+class _RequiredInsertColumnEdit(TypedDict):
+ columnIdx: int
+ newName: str
+ type: Literal["insert"]
+
+
+class InsertColumnEdit(_RequiredInsertColumnEdit, total=False):
+ dataType: DataType
+
+
+ColumnEdit = RemoveColumnEdit | RenameColumnEdit | InsertColumnEdit
class RowEdit(TypedDict):
- """A typed dictionary representing a bulk edit of a row.
+ """A typed dictionary representing a row removal.
Attributes:
- rowIdx (int): The index of the row being edited.
- type (Literal["insert", "remove"]): The type of edit.
-
- Note: Insert is already handled with positional edits, so we can focus on 'remove' here
+ rowIdx (int): The index of the row being removed.
+ type (Literal["remove"]): The type of edit.
"""
rowIdx: int
- type: Literal["insert", "remove"]
+ type: Literal["remove"]
class DataEdits(TypedDict):
@@ -100,6 +99,319 @@ class DataEdits(TypedDict):
RowOrientedData = list[dict[str, Any]]
ColumnOrientedData = dict[str, list[Any]]
+Scalar = str | int | float | bool | None
+ScalarData = list[Scalar]
+_DEFAULT_SCALAR_COLUMN = "value"
+_TYPE_INFERENCE_SAMPLE_SIZE = 10
+_MAX_SAFE_INTEGER = Decimal(2**53 - 1)
+
+
+def _dtype_from_data_type(data_type: DataType | None) -> DType | None:
+ if data_type == "string":
+ return nw.String()
+ if data_type == "boolean":
+ return nw.Boolean()
+ if data_type == "integer":
+ return nw.Int64()
+ if data_type == "number":
+ return nw.Float64()
+ if data_type == "date":
+ return nw.Date()
+ if data_type == "datetime":
+ return nw.Datetime()
+ if data_type == "time":
+ return nw.Time()
+ return None
+
+
+def _convert_to_integer(value: Any) -> int:
+ if isinstance(value, bool):
+ return int(value)
+ if isinstance(value, int):
+ return value
+ try:
+ decimal_value = Decimal(str(value))
+ except (InvalidOperation, ValueError) as error:
+ raise ValueError(f"Cannot convert {value!r} to integer") from error
+ if (
+ not decimal_value.is_finite()
+ or decimal_value != decimal_value.to_integral_value()
+ ):
+ raise ValueError(
+ f"Cannot convert non-integral value {value!r} to integer"
+ )
+ return int(decimal_value)
+
+
+def _sample_indices(length: int) -> Sequence[int]:
+ if length <= _TYPE_INFERENCE_SAMPLE_SIZE:
+ return range(length)
+ return [
+ *(
+ index * length // _TYPE_INFERENCE_SAMPLE_SIZE
+ for index in range(_TYPE_INFERENCE_SAMPLE_SIZE - 1)
+ ),
+ length - 1,
+ ]
+
+
+def _infer_conversion_examples_from_rows(
+ data: RowOrientedData,
+) -> dict[str, Any]:
+ values: dict[str, Any] = {}
+ for row_idx in _sample_indices(len(data)):
+ values.update(
+ (column, value)
+ for column, value in data[row_idx].items()
+ if value is not None
+ )
+ return values
+
+
+def _infer_conversion_examples_from_columns(
+ data: ColumnOrientedData,
+ row_count: int,
+) -> dict[str, Any]:
+ values: dict[str, Any] = {}
+ for row_idx in _sample_indices(row_count):
+ for column, column_values in data.items():
+ if (
+ row_idx < len(column_values)
+ and column_values[row_idx] is not None
+ ):
+ values[column] = column_values[row_idx]
+ return values
+
+
+@dataclass
+class _EditableTable:
+ data: RowOrientedData | ColumnOrientedData
+ column_names: list[str]
+ row_count: int
+ conversion_examples: dict[str, Any]
+ dtypes: dict[str, DType]
+
+ @classmethod
+ def from_rows(
+ cls,
+ data: RowOrientedData,
+ schema: nw.Schema | None,
+ column_names: Sequence[str] | None,
+ ) -> _EditableTable:
+ column_order: list[str] = []
+ column_set: set[str] = set()
+ for row in data:
+ for column in row:
+ if column not in column_set:
+ column_order.append(column)
+ column_set.add(column)
+
+ fallback_columns = (
+ column_names
+ if column_names is not None
+ else schema.keys()
+ if schema is not None
+ else ()
+ )
+ for column in fallback_columns:
+ if column not in column_set:
+ column_order.append(column)
+ column_set.add(column)
+
+ dtypes = dict(schema.items()) if schema is not None else {}
+ return cls(
+ data,
+ column_order,
+ len(data),
+ _infer_conversion_examples_from_rows(data),
+ dtypes,
+ )
+
+ @classmethod
+ def from_columns(
+ cls,
+ data: ColumnOrientedData,
+ schema: nw.Schema | None,
+ ) -> _EditableTable:
+ column_order = list(data)
+ if schema is not None:
+ column_order.extend(
+ column for column in schema if column not in data
+ )
+ dtypes = dict(schema.items()) if schema is not None else {}
+ row_count = max((len(values) for values in data.values()), default=0)
+ return cls(
+ data,
+ column_order,
+ row_count,
+ _infer_conversion_examples_from_columns(data, row_count),
+ dtypes,
+ )
+
+ def apply(self, edits: DataEdits) -> None:
+ for edit in edits["edits"]:
+ if is_positional_edit(edit):
+ self._apply_positional_edit(edit)
+ elif is_row_edit(edit):
+ self._apply_row_edit(edit)
+ elif is_column_edit(edit):
+ self._apply_column_edit(edit)
+ else:
+ log_never(edit)
+
+ def _materialize_columns(self) -> None:
+ if isinstance(self.data, list):
+ return
+ for column in self.column_names:
+ values = self.data.setdefault(column, [])
+ values.extend([None] * (self.row_count - len(values)))
+
+ def _get_cell_value(self, row_idx: int, column: str) -> Any:
+ if row_idx >= self.row_count:
+ return None
+ if isinstance(self.data, list):
+ return self.data[row_idx].get(column)
+ values = self.data.get(column, [])
+ return values[row_idx] if row_idx < len(values) else None
+
+ def _apply_positional_edit(self, edit: PositionalEdit) -> None:
+ column_id = edit["columnId"]
+ row_idx = edit["rowIdx"]
+ if row_idx < 0:
+ return
+ if column_id not in self.column_names:
+ self.column_names.append(column_id)
+ self.conversion_examples[column_id] = None
+ if isinstance(self.data, list):
+ for row in self.data:
+ row[column_id] = None
+ if isinstance(self.data, dict) and column_id not in self.data:
+ self.data[column_id] = [None] * self.row_count
+ self._materialize_columns()
+
+ if row_idx >= self.row_count:
+ new_row_count = row_idx + 1
+ if isinstance(self.data, list):
+ self.data.extend(
+ dict.fromkeys(self.column_names)
+ for _ in range(new_row_count - self.row_count)
+ )
+ else:
+ for values in self.data.values():
+ values.extend([None] * (new_row_count - len(values)))
+ self.row_count = new_row_count
+
+ dtype = self.dtypes.get(column_id)
+ conversion_value = (
+ self._get_cell_value(row_idx, column_id)
+ if dtype is not None
+ else self.conversion_examples.get(column_id)
+ )
+ converted_value = _convert_value(
+ edit["value"],
+ conversion_value,
+ dtype,
+ )
+ if isinstance(self.data, list):
+ self.data[row_idx][column_id] = converted_value
+ else:
+ values = self.data[column_id]
+ values.extend([None] * (row_idx - len(values) + 1))
+ values[row_idx] = converted_value
+
+ def _apply_row_edit(self, edit: RowEdit) -> None:
+ row_idx = edit["rowIdx"]
+ if edit["type"] != "remove" or not 0 <= row_idx < self.row_count:
+ return
+ if isinstance(self.data, list):
+ self.data.pop(row_idx)
+ else:
+ for values in self.data.values():
+ if row_idx < len(values):
+ values.pop(row_idx)
+ self.row_count -= 1
+
+ def _apply_column_edit(self, edit: ColumnEdit) -> None:
+ new_column_name = cast(str | None, edit.get("newName"))
+ _validate_column_edit(edit, len(self.column_names), new_column_name)
+
+ column_idx = edit["columnIdx"]
+ edit_type = edit["type"]
+ if edit_type == "insert":
+ assert new_column_name is not None
+ if new_column_name in self.column_names:
+ raise ValueError(f"Column {new_column_name} already exists")
+
+ self._materialize_columns()
+ self.column_names.insert(column_idx, new_column_name)
+ if isinstance(self.data, list):
+ for row_idx, row in enumerate(self.data):
+ self.data[row_idx] = {
+ column: None
+ if column == new_column_name
+ else row[column]
+ for column in self.column_names
+ if column == new_column_name or column in row
+ }
+ else:
+ items = list(self.data.items())
+ items.insert(
+ column_idx,
+ (new_column_name, [None] * self.row_count),
+ )
+ self.data.clear()
+ self.data.update(items)
+ self.conversion_examples[new_column_name] = None
+ dtype = _dtype_from_data_type(
+ cast(DataType | None, edit.get("dataType"))
+ )
+ if dtype is None:
+ self.dtypes.pop(new_column_name, None)
+ else:
+ self.dtypes[new_column_name] = dtype
+ return
+
+ old_name = self.column_names[column_idx]
+ if edit_type == "remove":
+ self._materialize_columns()
+ self.column_names.pop(column_idx)
+ if isinstance(self.data, list):
+ for row in self.data:
+ row.pop(old_name, None)
+ else:
+ self.data.pop(old_name, None)
+ self.conversion_examples.pop(old_name, None)
+ self.dtypes.pop(old_name, None)
+ return
+
+ assert edit_type == "rename"
+ assert new_column_name is not None
+ if old_name == new_column_name:
+ return
+ if new_column_name in self.column_names:
+ raise ValueError(f"Column {new_column_name} already exists")
+
+ self._materialize_columns()
+ self.column_names[column_idx] = new_column_name
+ if isinstance(self.data, list):
+ for row_idx, row in enumerate(self.data):
+ self.data[row_idx] = {
+ new_column_name if column == old_name else column: value
+ for column, value in row.items()
+ }
+ else:
+ renamed_columns = {
+ new_column_name if column == old_name else column: values
+ for column, values in self.data.items()
+ }
+ self.data.clear()
+ self.data.update(renamed_columns)
+ self.conversion_examples[new_column_name] = (
+ self.conversion_examples.pop(old_name, None)
+ )
+ dtype = self.dtypes.pop(old_name, None)
+ if dtype is not None:
+ self.dtypes[new_column_name] = dtype
@deprecated(
@@ -118,7 +430,12 @@ def experimental_data_editor(
class data_editor(
UIElement[
DataEdits,
- Union[RowOrientedData, ColumnOrientedData, IntoDataFrame],
+ Union[
+ ScalarData,
+ RowOrientedData,
+ ColumnOrientedData,
+ IntoDataFrame,
+ ],
]
):
"""A data editor component for editing tabular data.
@@ -129,8 +446,9 @@ class data_editor(
The data can be supplied as:
1. an eager dataframe (e.g., Polars, Pandas, PyArrow)
- 2. a list of dicts, with one dict for each row, keyed by column names
- 3. a dict of lists, with each list representing a column
+ 2. a list of scalar values, displayed in a column named `value`
+ 3. a list of dicts, with one dict for each row, keyed by column names
+ 4. a dict of lists, with each list representing a column
Examples:
Create a data editor from a Pandas dataframe:
@@ -157,12 +475,12 @@ class data_editor(
```
Attributes:
- value (RowOrientedData | ColumnOrientedData | IntoDataFrame): The current state of the edited data.
- data (RowOrientedData | ColumnOrientedData | IntoDataFrame): The original data passed to the editor.
+ value (ScalarData | RowOrientedData | ColumnOrientedData | IntoDataFrame): The current state of the edited data.
+ data (ScalarData | RowOrientedData | ColumnOrientedData | IntoDataFrame): The original data passed to the editor.
Args:
- data (RowOrientedData | ColumnOrientedData | IntoDataFrame): The data to be edited.
- Can be a Pandas dataframe, a list of dicts, or a dict of lists.
+ data (ScalarData | RowOrientedData | ColumnOrientedData | IntoDataFrame): The data to be edited.
+ Can be a dataframe, a list of scalars, a list of dicts, or a dict of lists.
label (str): Markdown label for the element.
on_change (Optional[Callable]): Optional callback to run when this element's value changes.
editable_columns (Union[list[str], Literal["all"]]): A list of column names to be editable.
@@ -178,11 +496,20 @@ class data_editor(
def __init__(
self,
- data: RowOrientedData | ColumnOrientedData | IntoDataFrame,
+ data: ScalarData
+ | RowOrientedData
+ | ColumnOrientedData
+ | IntoDataFrame,
*,
label: str = "",
on_change: Callable[
- [RowOrientedData | ColumnOrientedData | IntoDataFrame], None
+ [
+ ScalarData
+ | RowOrientedData
+ | ColumnOrientedData
+ | IntoDataFrame
+ ],
+ None,
]
| None = None,
editable_columns: list[str] | Literal["all"] = "all",
@@ -206,9 +533,9 @@ class data_editor(
self._data = data
self._edits: DataEdits | None = None
- field_types = table_manager.get_field_types()
-
column_names = table_manager.get_column_names()
+ self._column_names = column_names
+ field_types = table_manager.get_field_types()
if isinstance(editable_columns, list):
for col in editable_columns:
@@ -224,6 +551,7 @@ class data_editor(
args={
"data": mo_data.csv(table_manager.to_csv()).url,
"field-types": field_types or None,
+ "column-names": column_names,
"editable-columns": editable_columns,
"column-sizing-mode": "auto",
},
@@ -233,12 +561,12 @@ class data_editor(
@property
def data(
self,
- ) -> RowOrientedData | ColumnOrientedData | IntoDataFrame:
+ ) -> ScalarData | RowOrientedData | ColumnOrientedData | IntoDataFrame:
return self._data
def _convert_value(
self, value: DataEdits
- ) -> RowOrientedData | ColumnOrientedData | IntoDataFrame:
+ ) -> ScalarData | RowOrientedData | ColumnOrientedData | IntoDataFrame:
self._edits = value
# list/dict edit paths mutate in place, so deepcopy first.
# The dataframe path constructs a new native frame via narwhals
@@ -247,25 +575,27 @@ class data_editor(
data = self._data
if isinstance(data, (list, dict)):
data = deepcopy(data)
- return apply_edits(data, value)
+ return apply_edits(data, value, column_names=self._column_names)
def __hash__(self) -> int:
return id(self)
def apply_edits(
- data: RowOrientedData | ColumnOrientedData | IntoDataFrame,
+ data: ScalarData | RowOrientedData | ColumnOrientedData | IntoDataFrame,
edits: DataEdits,
schema: nw.Schema | None = None,
-) -> RowOrientedData | ColumnOrientedData | IntoDataFrame:
+ *,
+ column_names: Sequence[str] | None = None,
+) -> ScalarData | RowOrientedData | ColumnOrientedData | IntoDataFrame:
if len(edits["edits"]) == 0:
return data
- # If row-oriented, apply edits to the data
if isinstance(data, list):
- return _apply_edits_row_oriented(data, edits, schema)
- # If column-oriented, apply edits to the data
+ return _apply_edits_list(data, edits, schema, column_names)
elif isinstance(data, dict):
- return _apply_edits_column_oriented(data, edits, schema)
+ table = _EditableTable.from_columns(data, schema)
+ table.apply(edits)
+ return data
try:
return _apply_edits_dataframe(data, edits, schema)
@@ -275,36 +605,41 @@ def apply_edits(
) from e
-def _apply_edits_column_oriented(
- data: ColumnOrientedData,
+def _apply_edits_list(
+ data: ScalarData | RowOrientedData,
edits: DataEdits,
- schema: nw.Schema | None = None,
-) -> ColumnOrientedData:
- for edit in edits["edits"]:
- if is_positional_edit(edit):
- _apply_positional_edit_column_oriented(data, edit, schema)
- elif is_row_edit(edit):
- _apply_row_edit_column_oriented(data, edit)
- elif is_column_edit(edit):
- _apply_column_edit_column_oriented(data, edit)
-
- return data
+ schema: nw.Schema | None,
+ column_names: Sequence[str] | None,
+) -> ScalarData | RowOrientedData:
+ is_empty_scalar_data = (
+ not data
+ and schema is None
+ and list(column_names or ()) == [_DEFAULT_SCALAR_COLUMN]
+ )
+ if not is_empty_scalar_data and all(isinstance(row, dict) for row in data):
+ rows = cast(RowOrientedData, data)
+ table = _EditableTable.from_rows(rows, schema, column_names)
+ table.apply(edits)
+ return rows
+ if any(isinstance(row, dict) for row in data):
+ raise ValueError(
+ "Row-oriented data must contain either only dictionaries or "
+ "only scalar values"
+ )
-def _apply_edits_row_oriented(
- data: RowOrientedData,
- edits: DataEdits,
- schema: nw.Schema | None = None,
-) -> RowOrientedData:
- for edit in edits["edits"]:
- if is_positional_edit(edit):
- _apply_positional_edit_row_oriented(data, edit, schema)
- elif is_row_edit(edit):
- _apply_row_edit_row_oriented(data, edit)
- elif is_column_edit(edit):
- _apply_column_edit_row_oriented(data, edit)
+ scalar_column = (
+ column_names[0]
+ if column_names is not None and len(column_names) == 1
+ else _DEFAULT_SCALAR_COLUMN
+ )
+ rows = [{scalar_column: value} for value in data]
+ table = _EditableTable.from_rows(rows, schema, [scalar_column])
+ table.apply(edits)
- return data
+ if table.column_names == [scalar_column]:
+ return [cast(Scalar, row[scalar_column]) for row in rows]
+ return rows
def _apply_edits_dataframe(
@@ -315,102 +650,109 @@ def _apply_edits_dataframe(
schema = schema or cast(nw.Schema, df.schema)
# TODO: We should try to find more performant methods of bulk edits for dataframes
- new_data = _apply_edits_column_oriented(column_oriented, edits, schema)
+ table = _EditableTable.from_columns(column_oriented, schema)
+ table.apply(edits)
new_native_df = nw.from_dict(
- new_data, backend=nw.get_native_namespace(df)
+ column_oriented, backend=nw.get_native_namespace(df)
).to_native()
return new_native_df # type: ignore[no-any-return]
+def _convert_list(value: Any) -> list[Any]:
+ if not isinstance(value, str):
+ return value if isinstance(value, list) else [value]
+
+ try:
+ parsed = ast.literal_eval(value)
+ return parsed if isinstance(parsed, list) else list(parsed)
+ except (TypeError, ValueError, SyntaxError):
+ return value.split(",")
+
+
+def _convert_by_dtype(value: Any, dtype: DType) -> tuple[bool, Any]:
+ if dtype == nw.Datetime:
+ return True, datetime.datetime.fromisoformat(value)
+ if dtype == nw.Date:
+ return True, datetime.date.fromisoformat(value)
+ if dtype == nw.Time:
+ return True, datetime.time.fromisoformat(value)
+ if dtype == nw.Duration:
+ return True, datetime.timedelta(microseconds=float(value))
+ if hasattr(dtype, "is_float") and dtype.is_float():
+ return True, float(value)
+ if hasattr(dtype, "is_integer") and dtype.is_integer():
+ return True, _convert_to_integer(value)
+ if dtype == nw.String or dtype == nw.Enum or dtype == nw.Categorical:
+ return True, str(value)
+ if dtype == nw.Boolean:
+ return True, bool(value)
+ if dtype == nw.List:
+ return True, _convert_list(value)
+ return False, value
+
+
+def _convert_like(value: Any, example: Any) -> Any:
+ if example is None:
+ return value
+ if isinstance(example, bool):
+ return bool(value)
+ if isinstance(example, int):
+ if isinstance(value, (int, float)) and not isinstance(value, bool):
+ return value
+ try:
+ decimal_value = Decimal(str(value))
+ except (InvalidOperation, ValueError):
+ return float(value)
+ if decimal_value.is_finite() and (
+ decimal_value == decimal_value.to_integral_value()
+ ):
+ return int(decimal_value)
+ if (
+ not decimal_value.is_finite()
+ or abs(decimal_value) <= _MAX_SAFE_INTEGER
+ ):
+ return float(value)
+ return value
+ if isinstance(example, float):
+ return float(value)
+ if isinstance(example, str):
+ return str(value)
+ if isinstance(example, datetime.timedelta):
+ return datetime.timedelta(microseconds=float(value))
+ if isinstance(example, datetime.datetime):
+ return datetime.datetime.fromisoformat(value)
+ if isinstance(example, datetime.date):
+ return datetime.date.fromisoformat(value)
+ if isinstance(example, list):
+ return _convert_list(value)
+ return value
+
+
def _convert_value(
value: Any,
original_value: Any,
dtype: DType | None = None,
) -> Any:
- try:
- # None is a valid value for all dtypes
- if value is None:
- return None
-
- if dtype is not None:
- if dtype == nw.Datetime:
- return datetime.datetime.fromisoformat(value)
- elif dtype == nw.Date:
- return datetime.date.fromisoformat(value)
- elif dtype == nw.Duration:
- return datetime.timedelta(microseconds=float(value))
- elif hasattr(dtype, "is_float") and dtype.is_float():
- return float(value)
- elif hasattr(dtype, "is_integer") and dtype.is_integer():
- return int(value)
- elif (
- dtype == nw.String
- or dtype == nw.Enum
- or dtype == nw.Categorical
- ):
- return str(value)
- elif dtype == nw.Boolean:
- return bool(value)
- elif dtype == nw.List:
- # Handle list conversion
- if isinstance(value, str):
- # Attempt to parse string as a list
- try:
- return list(ast.literal_eval(value))
- except (ValueError, SyntaxError):
- # If parsing fails, split the string
- return value.split(",")
- elif isinstance(value, list):
- return value # type: ignore
- else:
- # If it's not a string or list, wrap it in a list
- return [value]
- else:
- # narwhals maps some pandas extension dtypes (notably
- # float16) to nw.Unknown. Stringifying here would coerce the
- # whole column to object; fall through to the original-value
- # coercion below, which keeps the column numeric.
- LOGGER.debug(
- "Unhandled dtype %s; coercing from original value", dtype
- )
-
- if original_value is None:
- return value
+ if value is None:
+ return None
+
+ if dtype is not None:
+ try:
+ handled, converted = _convert_by_dtype(value, dtype)
+ except ValueError as error:
+ LOGGER.error(str(error))
+ return original_value
+ if handled:
+ return converted
+ # Some pandas extension dtypes map to nw.Unknown. Using the example
+ # preserves a numeric column instead of coercing it to object.
+ LOGGER.debug("Unhandled dtype %s; coercing from example", dtype)
- # Try to convert the value to the original type
- original_type: Any = type(original_value)
-
- if isinstance(original_value, (int, float)):
- return original_type(value)
- elif isinstance(original_value, str):
- return str(value)
- # The more specific time checks are handled first to avoid parent classes matching
- elif isinstance(original_value, (datetime.timedelta)):
- return datetime.timedelta(microseconds=float(value))
- elif isinstance(original_value, (datetime.datetime)):
- return datetime.datetime.fromisoformat(value)
- elif isinstance(original_value, (datetime.date)):
- return datetime.date.fromisoformat(value)
- elif isinstance(original_value, list):
- # Handle list conversion
- if isinstance(value, str):
- # Attempt to parse string as a list
- try:
- return list(ast.literal_eval(value))
- except (ValueError, SyntaxError):
- # If parsing fails, split the string
- return list(value.split(","))
- elif isinstance(value, list):
- return value # type: ignore[return-value]
- else:
- # If it's not a string or list, wrap it in a list
- return [value]
- else:
- return value
- except ValueError as e:
- LOGGER.error(str(e))
- # If conversion fails, return the original value
- return original_value # type: ignore[return-value]
+ try:
+ return _convert_like(value, original_value)
+ except ValueError as error:
+ LOGGER.error(str(error))
+ return original_value if dtype is not None else value
def is_positional_edit(
@@ -434,204 +776,20 @@ def is_column_edit(
return "columnIdx" in edit and "type" in edit
-def _apply_positional_edit_column_oriented(
- data: ColumnOrientedData,
- edit: PositionalEdit,
- schema: nw.Schema | None = None,
-) -> None:
- """Apply a positional edit to column-oriented data."""
- column = data[edit["columnId"]]
- if edit["rowIdx"] >= len(column):
- # Extend the column with None values up to the new row index
- column.extend([None] * (edit["rowIdx"] - len(column) + 1))
- dtype = schema.get(edit["columnId"]) if schema else None
- column[edit["rowIdx"]] = _convert_value(
- edit["value"], column[0] if column else None, dtype
- )
-
-
-def _apply_positional_edit_row_oriented(
- data: RowOrientedData,
- edit: PositionalEdit,
- schema: nw.Schema | None = None,
-) -> None:
- """Apply a positional edit to row-oriented data."""
- if edit["rowIdx"] >= len(data):
- # Create a new row with None values for all columns
- new_row = {col: None for col in data[0]}
- data.append(new_row)
- original_value = data[0][edit["columnId"]] if data else None
- dtype = schema.get(edit["columnId"]) if schema else None
- data[edit["rowIdx"]][edit["columnId"]] = _convert_value(
- edit["value"], original_value, dtype
- )
-
-
-def _apply_row_edit_column_oriented(
- data: ColumnOrientedData,
- edit: RowEdit,
-) -> None:
- """Apply a row edit to column-oriented data."""
- if edit["type"] == "remove":
- rowIdx = edit["rowIdx"]
- for column in data.values():
- if not _is_valid_index(rowIdx, len(column)):
- continue
- del column[rowIdx]
-
-
-def _apply_row_edit_row_oriented(
- data: RowOrientedData,
- edit: RowEdit,
-) -> None:
- """Apply a row edit to row-oriented data."""
- rowIdx = edit["rowIdx"]
- if not _is_valid_index(rowIdx, len(data)):
- return
- if edit["type"] == "remove":
- data.pop(rowIdx)
-
-
def _validate_column_edit(
edit: ColumnEdit,
- data_length: int,
+ column_count: int,
new_column_name: str | None,
) -> None:
"""Validate column edit parameters."""
column_idx = edit["columnIdx"]
edit_type = edit["type"]
- if column_idx < 0 or column_idx > data_length:
- raise ValueError(f"Column index {column_idx} is out of bounds")
-
if edit_type in ("insert", "rename") and new_column_name is None:
raise ValueError(
"New column name is required for insert/rename operations"
)
-
-def _apply_column_edit_column_oriented(
- data: ColumnOrientedData,
- edit: ColumnEdit,
-) -> None:
- """Apply a column edit to column-oriented data."""
- column_order = list(data.keys())
- new_column_name = edit.get("newName")
-
- column_idx = edit["columnIdx"]
- edit_type = edit["type"]
-
- _validate_column_edit(edit, len(data), new_column_name)
-
- column_idx = edit["columnIdx"]
- edit_type = edit["type"]
-
- if edit_type == "insert":
- assert new_column_name is not None
-
- data_length = len(data[column_order[0]]) if column_order else 0
-
- if column_idx == len(column_order):
- # Add new column at the end
- data[new_column_name] = [None] * data_length
- else:
- # Insert new column at specific index
- column_data = data.copy()
- data.clear()
- for idx, key in enumerate(column_order):
- if idx == column_idx:
- data[new_column_name] = [None] * data_length
- data[key] = column_data[key]
- return
-
- # Find column by index
- column_id = None
- for idx, key in enumerate(column_order):
- if idx == column_idx:
- column_id = key
- break
-
- if column_id is None:
- raise ValueError(f"Column index {column_idx} not found")
-
- if edit_type == "rename":
- assert new_column_name is not None
-
- column_data = data.copy()
- data.clear()
- for key in column_order:
- if key == column_id:
- data[new_column_name] = column_data[key]
- else:
- data[key] = column_data[key]
- elif edit_type == "remove":
- del data[column_id]
-
-
-def _apply_column_edit_row_oriented(
- data: RowOrientedData,
- edit: ColumnEdit,
-) -> None:
- """Apply a column edit to row-oriented data."""
- if not data:
- return
-
- column_order = list(data[0].keys())
- new_column_name = edit.get("newName")
-
- _validate_column_edit(edit, len(data[0]) + 1, new_column_name)
-
- column_idx = edit["columnIdx"]
- edit_type = edit["type"]
-
- if edit_type == "insert":
- assert new_column_name is not None
-
- if column_idx < len(column_order):
- new_column_order = (
- column_order[:column_idx]
- + [new_column_name]
- + column_order[column_idx:]
- )
- else:
- new_column_order = column_order + [new_column_name]
-
- for row_idx, row in enumerate(data):
- new_row = {
- column: row.get(column, None) for column in new_column_order
- }
- data[row_idx] = new_row
- return
-
- # Find column by index
- column_id = None
- for idx, column in enumerate(data[0]):
- if idx == column_idx:
- column_id = column
- break
-
- if column_id is None:
- raise ValueError(f"Column index {column_idx} not found")
-
- if edit_type == "remove":
- for d in data:
- del d[column_id]
- elif edit_type == "rename":
- assert new_column_name is not None
-
- # Get the column name at the specified index
- column_name = list(data[0].keys())[column_idx]
-
- for row in data:
- new_row = {}
- for key in row:
- if key == column_name:
- new_row[new_column_name] = row[key]
- else:
- new_row[key] = row[key]
- row.clear()
- row.update(new_row)
-
-
-def _is_valid_index(index: int, length: int) -> bool:
- return index >= 0 and index < length
+ max_index = column_count if edit_type == "insert" else column_count - 1
+ if column_idx < 0 or column_idx > max_index:
+ raise ValueError(f"Column index {column_idx} is out of bounds")
diff --git a/tests/_plugins/ui/_impl/test_data_editor.py b/tests/_plugins/ui/_impl/test_data_editor.py
index 1667183e7d..596031791f 100644
--- a/tests/_plugins/ui/_impl/test_data_editor.py
+++ b/tests/_plugins/ui/_impl/test_data_editor.py
@@ -3,11 +3,13 @@ from __future__ import annotations
import datetime
from copy import deepcopy
-from typing import Any
+from typing import Any, cast
+from unittest.mock import Mock
import narwhals.stable.v2 as nw
import pytest
+from marimo._data.models import DataType
from marimo._dependencies.dependencies import DependencyManager
from marimo._plugins import ui
from marimo._plugins.ui._impl.data_editor import (
@@ -104,6 +106,107 @@ def test_apply_edits_column_oriented():
assert result == {"A": [1, 2, 3], "B": ["a", "x", "c"]}
+def test_data_editor_appends_to_scalar_list():
+ editor = data_editor([1, 2])
+ edits: DataEdits = {
+ "edits": [{"rowIdx": 2, "columnId": "value", "value": "3"}]
+ }
+
+ assert editor._convert_value(edits) == [1, 2, 3]
+
+
+def test_data_editor_appends_to_empty_scalar_list():
+ editor = data_editor([])
+ edits: DataEdits = {
+ "edits": [{"rowIdx": 0, "columnId": "value", "value": "x"}]
+ }
+
+ assert editor._convert_value(edits) == ["x"]
+
+
+ ("data", "expected_columns"),
+ [([], ["value"]), ({"A": []}, ["A"])],
+)
+def test_data_editor_exposes_columns_for_empty_data(data, expected_columns):
+ editor = data_editor(data)
+
+ assert editor._component_args["column-names"] == expected_columns
+ assert editor._component_args["field-types"] is None
+
+
+def test_data_editor_preserves_numeric_string_column_order():
+ editor = data_editor([{"10": "x", "2": "y"}])
+ edits: DataEdits = {"edits": [{"columnIdx": 0, "type": "remove"}]}
+
+ assert editor._component_args["column-names"] == ["10", "2"]
+ assert editor._convert_value(edits) == [{"2": "y"}]
+
+
+def test_data_editor_edits_heterogeneous_scalar_list():
+ editor = data_editor([1, "a"])
+ edits: DataEdits = {
+ "edits": [{"rowIdx": 1, "columnId": "value", "value": "b"}]
+ }
+
+ assert editor._convert_value(edits) == [1, "b"]
+
+
+def test_data_editor_appends_to_heterogeneous_scalar_list():
+ editor = data_editor([1, "a"])
+ edits: DataEdits = {
+ "edits": [{"rowIdx": 2, "columnId": "value", "value": "b"}]
+ }
+
+ assert editor._convert_value(edits) == [1, "a", "b"]
+
+
+def test_data_editor_uses_inferred_type_for_null_scalar():
+ editor = data_editor([None, 7])
+ edits: DataEdits = {
+ "edits": [{"rowIdx": 0, "columnId": "value", "value": "8"}]
+ }
+
+ assert editor._convert_value(edits) == [8, 7]
+
+
+ ("data", "column", "expected"),
+ [
+ ([1, 1.5, 2], "value", [3.5, 1.5, 2]),
+ (
+ [{"A": 1}, {"A": 1.5}, {"A": 2}],
+ "A",
+ [{"A": 3.5}, {"A": 1.5}, {"A": 2}],
+ ),
+ ({"A": [1, 1.5, 2]}, "A", {"A": [3.5, 1.5, 2]}),
+ ],
+)
+def test_apply_edits_preserves_untyped_numeric_precision(
+ data: Any, column: str, expected: Any
+):
+ edits: DataEdits = {
+ "edits": [{"rowIdx": 0, "columnId": column, "value": 3.5}]
+ }
+
+ assert apply_edits(data, edits) == expected
+
+
+def test_data_editor_promotes_scalar_list_when_adding_column():
+ editor = data_editor([1, 2])
+ edits: DataEdits = {
+ "edits": [
+ {"columnIdx": 1, "type": "insert", "newName": "B"},
+ {"rowIdx": 0, "columnId": "B", "value": "x"},
+ ]
+ }
+
+ assert editor._convert_value(edits) == [
+ {"value": 1, "B": "x"},
+ {"value": 2, "B": None},
+ ]
+
+
@pytest.mark.skipif(
not DependencyManager.polars.has(), reason="Polars not installed"
)
@@ -118,6 +221,313 @@ def test_apply_edits_new_row():
]
+def test_apply_edits_converts_every_cell_in_appended_row():
+ data = [{"A": 1, "B": 2.5}]
+ edits: DataEdits = {
+ "edits": [
+ {"rowIdx": 1, "columnId": "A", "value": "2"},
+ {"rowIdx": 1, "columnId": "B", "value": "3.5"},
+ ]
+ }
+
+ assert apply_edits(data, edits) == [
+ {"A": 1, "B": 2.5},
+ {"A": 2, "B": 3.5},
+ ]
+
+
+def test_apply_edits_tracks_appended_rows_after_removal():
+ data = [{"A": 1}]
+ edits: DataEdits = {
+ "edits": [
+ {"rowIdx": 2, "columnId": "A", "value": "3"},
+ {"rowIdx": 0, "type": "remove"},
+ {"rowIdx": 1, "columnId": "A", "value": "4"},
+ ]
+ }
+
+ assert apply_edits(data, edits) == [{"A": None}, {"A": 4}]
+
+
+def test_apply_edits_appends_to_empty_row_oriented_data():
+ edits: DataEdits = {
+ "edits": [
+ {"rowIdx": 0, "columnId": "A", "value": "x"},
+ {"rowIdx": 0, "columnId": "B", "value": 1},
+ ]
+ }
+
+ assert apply_edits([], edits) == [{"A": "x", "B": 1}]
+
+
+def test_apply_edits_backfills_columns_discovered_in_later_rows():
+ edits: DataEdits = {
+ "edits": [
+ {"rowIdx": 0, "columnId": "A", "value": "a"},
+ {"rowIdx": 1, "columnId": "B", "value": "b"},
+ ]
+ }
+
+ assert apply_edits([], edits) == [
+ {"A": "a", "B": None},
+ {"A": None, "B": "b"},
+ ]
+
+
+ ("data", "expected"),
+ [
+ ([], [{"A": 1, "B": None}]),
+ ({}, {"A": [1], "B": [None]}),
+ ],
+)
+def test_apply_edits_appends_schema_to_empty_data(data, expected):
+ edits: DataEdits = {
+ "edits": [{"rowIdx": 0, "columnId": "A", "value": "1"}]
+ }
+ schema = nw.Schema({"A": nw.Int64(), "B": nw.String()})
+
+ assert apply_edits(data, edits, schema) == expected
+
+
+def test_apply_edits_extends_row_oriented_data_to_index():
+ edits: DataEdits = {
+ "edits": [{"rowIdx": 2, "columnId": "A", "value": "x"}]
+ }
+
+ assert apply_edits([], edits) == [
+ {"A": None},
+ {"A": None},
+ {"A": "x"},
+ ]
+
+
+def test_apply_edits_ignores_negative_row_index():
+ data = [{"A": 1}]
+ edits: DataEdits = {"edits": [{"rowIdx": -1, "columnId": "A", "value": 2}]}
+
+ assert apply_edits(data, edits) == [{"A": 1}]
+
+
+def test_apply_edits_logs_unknown_edit(monkeypatch: pytest.MonkeyPatch):
+ edits = cast(DataEdits, {"edits": [{"type": "unknown"}]})
+ log_never = Mock()
+ monkeypatch.setattr(
+ "marimo._plugins.ui._impl.data_editor.log_never", log_never
+ )
+
+ assert apply_edits([], edits) == []
+ log_never.assert_called_once_with({"type": "unknown"})
+
+
+def test_data_editor_replays_add_after_removing_all_rows():
+ editor = data_editor([{"A": 1, "B": "a"}])
+ edits: DataEdits = {
+ "edits": [
+ {"rowIdx": 0, "type": "remove"},
+ {"rowIdx": 0, "columnId": "A", "value": "2"},
+ {"rowIdx": 0, "columnId": "B", "value": "b"},
+ ]
+ }
+
+ assert editor._convert_value(edits) == [{"A": 2, "B": "b"}]
+
+
+def test_apply_edits_uses_inferred_column_type_for_conversion():
+ data = [{"A": None}, {"A": 7}]
+ editor = data_editor(data)
+ edits: DataEdits = {
+ "edits": [
+ {"rowIdx": 0, "type": "remove"},
+ {"rowIdx": 0, "columnId": "A", "value": "8"},
+ ]
+ }
+
+ assert editor._convert_value(edits) == [{"A": 8}]
+
+
+def test_apply_edits_preserves_row_column_order_with_schema():
+ data = [{"B": "x", "A": 1}]
+ schema = nw.Schema({"A": nw.Int64(), "B": nw.String()})
+ edits: DataEdits = {"edits": [{"columnIdx": 0, "type": "remove"}]}
+
+ assert apply_edits(data, edits, schema) == [{"A": 1}]
+
+
+ ("data", "expected"),
+ [([], [{"C": 7}]), ({}, {"C": [7]})],
+)
+def test_apply_edits_tracks_schema_rename_without_rows(data, expected):
+ schema = nw.Schema({"A": nw.Int64()})
+ edits: DataEdits = {
+ "edits": [
+ {"columnIdx": 0, "type": "rename", "newName": "C"},
+ {"rowIdx": 0, "columnId": "C", "value": "7"},
+ ]
+ }
+
+ assert apply_edits(data, edits, schema) == expected
+
+
+ ("data", "expected"),
+ [([], [{"A": "007"}]), ({}, {"A": ["007"]})],
+)
+def test_apply_edits_drops_schema_for_reused_column_name(data, expected):
+ schema = nw.Schema({"A": nw.Int64()})
+ edits: DataEdits = {
+ "edits": [
+ {"columnIdx": 0, "type": "remove"},
+ {"columnIdx": 0, "type": "insert", "newName": "A"},
+ {"rowIdx": 0, "columnId": "A", "value": "007"},
+ ]
+ }
+
+ assert apply_edits(data, edits, schema) == expected
+
+
+ ("data_type", "value", "expected"),
+ [
+ ("number", "3.5", 3.5),
+ ("boolean", False, False),
+ (
+ "datetime",
+ "2026-08-26T10:30:00",
+ datetime.datetime(2026, 8, 26, 10, 30),
+ ),
+ ],
+)
+def test_apply_edits_uses_inserted_column_type(
+ data_type: DataType, value: Any, expected: Any
+):
+ data = [{"A": 1}]
+ edits: DataEdits = {
+ "edits": [
+ {
+ "columnIdx": 1,
+ "type": "insert",
+ "newName": "B",
+ "dataType": data_type,
+ },
+ {"rowIdx": 0, "columnId": "B", "value": value},
+ ]
+ }
+
+ assert apply_edits(data, edits) == [{"A": 1, "B": expected}]
+
+
+def test_apply_edits_tracks_column_changes_without_rows():
+ data = [{"A": 1, "B": "a"}]
+ edits: DataEdits = {
+ "edits": [
+ {"rowIdx": 0, "type": "remove"},
+ {"columnIdx": 1, "type": "insert", "newName": "C"},
+ {"columnIdx": 0, "type": "remove"},
+ {"columnIdx": 0, "type": "rename", "newName": "D"},
+ {"rowIdx": 0, "columnId": "D", "value": "x"},
+ {"rowIdx": 0, "columnId": "B", "value": "b"},
+ ]
+ }
+
+ assert apply_edits(data, edits) == [{"D": "x", "B": "b"}]
+
+
+ ("data", "expected"),
+ [
+ (
+ [{"A": 1, "B": "x"}, {"A": 2, "B": "y"}],
+ [
+ {"C": None, "D": None, "B": None},
+ {"C": None, "D": "v", "B": None},
+ {"C": 7, "D": None, "B": None},
+ ],
+ ),
+ (
+ {"A": [1, 2], "B": ["x", "y"]},
+ {
+ "C": [None, None, 7],
+ "D": [None, "v", None],
+ "B": [None, None, None],
+ },
+ ),
+ ],
+)
+def test_apply_edits_replays_mixed_edits_across_orientations(data, expected):
+ edits: DataEdits = {
+ "edits": [
+ {"rowIdx": 0, "type": "remove"},
+ {"rowIdx": 0, "type": "remove"},
+ {"columnIdx": 0, "type": "rename", "newName": "C"},
+ {"columnIdx": 1, "type": "insert", "newName": "D"},
+ {"rowIdx": 2, "columnId": "C", "value": "7"},
+ {"rowIdx": 1, "columnId": "D", "value": "v"},
+ ]
+ }
+ schema = nw.Schema({"A": nw.Int64(), "B": nw.String()})
+
+ assert apply_edits(data, edits, schema) == expected
+
+
+ ("data", "expected"),
+ [
+ ([{"A": 1}, {"A": 2}], [{"C": None}, {"C": "x"}]),
+ ({"A": [1, 2]}, {"C": [None, "x"]}),
+ ],
+)
+def test_apply_edits_preserves_rows_without_columns(data, expected):
+ edits: DataEdits = {
+ "edits": [
+ {"columnIdx": 0, "type": "remove"},
+ {"columnIdx": 0, "type": "insert", "newName": "C"},
+ {"rowIdx": 1, "columnId": "C", "value": "x"},
+ ]
+ }
+
+ assert apply_edits(data, edits) == expected
+
+
+def test_apply_edits_extends_every_column_to_new_row():
+ data = {"A": [1], "B": ["x"]}
+ edits: DataEdits = {
+ "edits": [{"rowIdx": 2, "columnId": "A", "value": "3"}]
+ }
+
+ assert apply_edits(data, edits) == {
+ "A": [1, None, 3],
+ "B": ["x", None, None],
+ }
+
+
+def test_apply_edits_preserves_sparse_row_shape():
+ data = [{"A": 1}, {"B": 2}]
+ edits: DataEdits = {
+ "edits": [{"rowIdx": 0, "columnId": "A", "value": "3"}]
+ }
+
+ assert apply_edits(data, edits) == [{"A": 3}, {"B": 2}]
+
+
+def test_invalid_edit_does_not_normalize_column_lengths():
+ data = {"A": [1], "B": []}
+ edits: DataEdits = {"edits": [{"columnIdx": 2, "type": "remove"}]}
+
+ with pytest.raises(ValueError, match="Column index 2 is out of bounds"):
+ apply_edits(data, edits)
+
+ assert data == {"A": [1], "B": []}
+
+
+def test_remove_column_preserves_rows_in_ragged_data():
+ data = {"A": [1, 2, 3], "B": ["x"]}
+ edits: DataEdits = {"edits": [{"columnIdx": 0, "type": "remove"}]}
+
+ assert apply_edits(data, edits) == {"B": ["x", None, None]}
+
+
@pytest.mark.skipif(
not DependencyManager.polars.has(), reason="Polars not installed"
)
@@ -353,6 +763,24 @@ def test_apply_edits_dataframe():
assert pd.DataFrame({"A": [1, 2, 3], "B": ["a", "x", "c"]}).equals(result)
+ not DependencyManager.pandas.has(), reason="Pandas not installed"
+)
[email protected]("value", ["invalid", 3.5])
+def test_apply_edits_dataframe_rejects_invalid_typed_value(value):
+ import pandas as pd
+
+ data = pd.DataFrame({"A": [1, 2]})
+ edits: DataEdits = {
+ "edits": [{"rowIdx": 0, "columnId": "A", "value": value}]
+ }
+
+ result = apply_edits(data, edits)
+
+ assert result["A"].tolist() == [1, 2]
+ assert result["A"].dtype == data["A"].dtype
+
+
@pytest.mark.skipif(
not DependencyManager.pandas.has(), reason="Pandas not installed"
)
@@ -1180,18 +1608,23 @@ class TestConvertValue:
def test_convert_value_value_error_handling(self):
"""Test error handling when conversion fails."""
- # This should fail to convert "invalid" to int
result = _convert_value("invalid", 42, None)
- # Should return original value when conversion fails
- assert result == 42
+ assert result == "invalid"
def test_convert_value_value_error_handling_with_dtype(self):
"""Test error handling when conversion fails with dtype."""
- # This should fail to convert "invalid" to int
result = _convert_value("invalid", 42, nw.Int64)
- # Should return original value when conversion fails
assert result == 42
+ @pytest.mark.parametrize("dtype", [None, nw.Int64])
+ def test_convert_value_preserves_large_integers(self, dtype):
+ result = _convert_value("9007199254740993", 1, dtype)
+ assert result == 9007199254740993
+
+ def test_convert_value_does_not_round_large_untyped_fraction(self):
+ result = _convert_value("9007199254740993.1", 1, None)
+ assert result == "9007199254740993.1"
+
def test_convert_value_list_parsing_error(self):
"""Test list parsing error handling."""
# This should fail to parse as a list