Skip to content

Commit 812a08c

Browse files
Add column sizing mode to data editor (#4037)
1 parent f701cc5 commit 812a08c

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

frontend/src/plugins/impl/DataEditorPlugin.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export const DataEditorPlugin = createPlugin<Edits>("marimo-data-editor", {
5757
]),
5858
)
5959
.nullish(),
60+
columnSizingMode: z.enum(["auto", "fit"]).default("auto"),
6061
}),
6162
)
6263
.withFunctions({})
@@ -70,6 +71,7 @@ export const DataEditorPlugin = createPlugin<Edits>("marimo-data-editor", {
7071
fieldTypes={props.data.fieldTypes}
7172
edits={props.value.edits}
7273
onEdits={props.setValue}
74+
columnSizingMode={props.data.columnSizingMode}
7375
/>
7476
</TooltipProvider>
7577
);
@@ -138,6 +140,7 @@ const LoadingDataEditor = (props: Props) => {
138140
);
139141
props.onEdits((v) => ({ ...v, edits: [...v.edits, ...newEdits] }));
140142
}}
143+
columnSizingMode={props.columnSizingMode}
141144
/>
142145
);
143146
};

frontend/src/plugins/impl/data-editor/data-editor.tsx

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Copyright 2024 Marimo. All rights reserved. */
2-
import React, { useCallback, useMemo, useRef } from "react";
2+
import React, { useCallback, useEffect, useMemo, useRef } from "react";
33
import { AgGridReact } from "ag-grid-react";
44
import type {
55
ColDef,
@@ -32,6 +32,7 @@ export interface DataEditorProps<T> {
3232
}>,
3333
) => void;
3434
onAddRows: (newRows: object[]) => void;
35+
columnSizingMode: "fit" | "auto";
3536
}
3637

3738
function cellEditorForDataType(dataType: DataType) {
@@ -67,6 +68,7 @@ const DataEditor: React.FC<DataEditorProps<object>> = ({
6768
fieldTypes,
6869
onAddEdits,
6970
onAddRows,
71+
columnSizingMode,
7072
}) => {
7173
const { theme } = useTheme();
7274
const gridRef = useRef<AgGridReact>(null);
@@ -126,9 +128,31 @@ const DataEditor: React.FC<DataEditorProps<object>> = ({
126128
};
127129
}, []);
128130

129-
const onGridReady = useCallback((params: GridReadyEvent) => {
130-
params.api.sizeColumnsToFit();
131-
}, []);
131+
const sizeColumns = useCallback(() => {
132+
// Size the columns to either fit the grid or auto-size based on content
133+
const api = gridRef.current?.api;
134+
if (!api) {
135+
return;
136+
}
137+
138+
if (columnSizingMode === "fit") {
139+
api.sizeColumnsToFit();
140+
} else if (columnSizingMode === "auto") {
141+
api.autoSizeAllColumns();
142+
}
143+
}, [columnSizingMode]);
144+
145+
const onGridReady = useCallback(
146+
(params: GridReadyEvent) => {
147+
sizeColumns();
148+
},
149+
[sizeColumns],
150+
);
151+
152+
useEffect(() => {
153+
// Update grid layout when column sizing prop changes
154+
sizeColumns();
155+
}, [columnSizingMode, sizeColumns]);
132156

133157
const onCellEditingStopped = useCallback(
134158
(event: CellEditingStoppedEvent) => {

marimo/_plugins/ui/_impl/data_editor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Any,
1010
Callable,
1111
Final,
12+
Literal,
1213
Optional,
1314
TypedDict,
1415
Union,
@@ -121,6 +122,8 @@ class data_editor(
121122
Can be a Pandas dataframe, a list of dicts, or a dict of lists.
122123
label (str): Markdown label for the element.
123124
on_change (Optional[Callable]): Optional callback to run when this element's value changes.
125+
column_sizing_mode (Literal["auto", "fit"]): The column sizing mode for the table.
126+
`auto` will size columns based on the content, `fit` will size columns to fit the view.
124127
"""
125128

126129
_name: Final[str] = "marimo-data-editor"
@@ -140,6 +143,7 @@ def __init__(
140143
None,
141144
]
142145
] = None,
146+
column_sizing_mode: Literal["auto", "fit"] = "auto",
143147
) -> None:
144148
validate_page_size(page_size)
145149
table_manager = get_table_manager(data)
@@ -163,6 +167,7 @@ def __init__(
163167
"field-types": field_types or None,
164168
"pagination": pagination,
165169
"page-size": page_size,
170+
"column-sizing-mode": column_sizing_mode,
166171
},
167172
on_change=on_change,
168173
)

tests/_plugins/ui/_impl/test_data_editor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def test_data_editor_initialization():
3030
assert editor._edits == {"edits": []}
3131
assert editor._component_args["pagination"] is True
3232
assert editor._component_args["page-size"] == 50
33+
assert editor._component_args["column-sizing-mode"] == "auto"
3334

3435

3536
@pytest.mark.skipif(

0 commit comments

Comments
 (0)