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.

Add column sizing mode to data editor (#4037)

Kacper Paszkowski812a08c4cee6
diff --git a/frontend/src/plugins/impl/DataEditorPlugin.tsx b/frontend/src/plugins/impl/DataEditorPlugin.tsx
index 35aed75141..1c2627567f 100644
--- a/frontend/src/plugins/impl/DataEditorPlugin.tsx
+++ b/frontend/src/plugins/impl/DataEditorPlugin.tsx
@@ -57,6 +57,7 @@ export const DataEditorPlugin = createPlugin<Edits>("marimo-data-editor", {
]),
)
.nullish(),
+ columnSizingMode: z.enum(["auto", "fit"]).default("auto"),
}),
)
.withFunctions({})
@@ -70,6 +71,7 @@ export const DataEditorPlugin = createPlugin<Edits>("marimo-data-editor", {
fieldTypes={props.data.fieldTypes}
edits={props.value.edits}
onEdits={props.setValue}
+ columnSizingMode={props.data.columnSizingMode}
/>
</TooltipProvider>
);
@@ -138,6 +140,7 @@ const LoadingDataEditor = (props: Props) => {
);
props.onEdits((v) => ({ ...v, edits: [...v.edits, ...newEdits] }));
}}
+ columnSizingMode={props.columnSizingMode}
/>
);
};
diff --git a/frontend/src/plugins/impl/data-editor/data-editor.tsx b/frontend/src/plugins/impl/data-editor/data-editor.tsx
index 862f69ccdb..a5bfbce596 100644
--- a/frontend/src/plugins/impl/data-editor/data-editor.tsx
+++ b/frontend/src/plugins/impl/data-editor/data-editor.tsx
@@ -1,5 +1,5 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import React, { useCallback, useMemo, useRef } from "react";
+import React, { useCallback, useEffect, useMemo, useRef } from "react";
import { AgGridReact } from "ag-grid-react";
import type {
ColDef,
@@ -32,6 +32,7 @@ export interface DataEditorProps<T> {
}>,
) => void;
onAddRows: (newRows: object[]) => void;
+ columnSizingMode: "fit" | "auto";
}
function cellEditorForDataType(dataType: DataType) {
@@ -67,6 +68,7 @@ const DataEditor: React.FC<DataEditorProps<object>> = ({
fieldTypes,
onAddEdits,
onAddRows,
+ columnSizingMode,
}) => {
const { theme } = useTheme();
const gridRef = useRef<AgGridReact>(null);
@@ -126,9 +128,31 @@ const DataEditor: React.FC<DataEditorProps<object>> = ({
};
}, []);
- const onGridReady = useCallback((params: GridReadyEvent) => {
- params.api.sizeColumnsToFit();
- }, []);
+ const sizeColumns = useCallback(() => {
+ // Size the columns to either fit the grid or auto-size based on content
+ const api = gridRef.current?.api;
+ if (!api) {
+ return;
+ }
+
+ if (columnSizingMode === "fit") {
+ api.sizeColumnsToFit();
+ } else if (columnSizingMode === "auto") {
+ api.autoSizeAllColumns();
+ }
+ }, [columnSizingMode]);
+
+ const onGridReady = useCallback(
+ (params: GridReadyEvent) => {
+ sizeColumns();
+ },
+ [sizeColumns],
+ );
+
+ useEffect(() => {
+ // Update grid layout when column sizing prop changes
+ sizeColumns();
+ }, [columnSizingMode, sizeColumns]);
const onCellEditingStopped = useCallback(
(event: CellEditingStoppedEvent) => {
diff --git a/marimo/_plugins/ui/_impl/data_editor.py b/marimo/_plugins/ui/_impl/data_editor.py
index fec96b2178..94f0d92926 100644
--- a/marimo/_plugins/ui/_impl/data_editor.py
+++ b/marimo/_plugins/ui/_impl/data_editor.py
@@ -9,6 +9,7 @@ from typing import (
Any,
Callable,
Final,
+ Literal,
Optional,
TypedDict,
Union,
@@ -121,6 +122,8 @@ class data_editor(
Can be a Pandas dataframe, 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.
+ column_sizing_mode (Literal["auto", "fit"]): The column sizing mode for the table.
+ `auto` will size columns based on the content, `fit` will size columns to fit the view.
"""
_name: Final[str] = "marimo-data-editor"
@@ -140,6 +143,7 @@ class data_editor(
None,
]
] = None,
+ column_sizing_mode: Literal["auto", "fit"] = "auto",
) -> None:
validate_page_size(page_size)
table_manager = get_table_manager(data)
@@ -163,6 +167,7 @@ class data_editor(
"field-types": field_types or None,
"pagination": pagination,
"page-size": page_size,
+ "column-sizing-mode": column_sizing_mode,
},
on_change=on_change,
)
diff --git a/tests/_plugins/ui/_impl/test_data_editor.py b/tests/_plugins/ui/_impl/test_data_editor.py
index f5416ae586..0f11b8134a 100644
--- a/tests/_plugins/ui/_impl/test_data_editor.py
+++ b/tests/_plugins/ui/_impl/test_data_editor.py
@@ -30,6 +30,7 @@ def test_data_editor_initialization():
assert editor._edits == {"edits": []}
assert editor._component_args["pagination"] is True
assert editor._component_args["page-size"] == 50
+ assert editor._component_args["column-sizing-mode"] == "auto"
@pytest.mark.skipif(