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: apply mo.ui.dataframe row filter when submitted via .form() (#10092)

Myles Scolnicke96c0c3dff97
diff --git a/marimo/_plugins/ui/_impl/dataframes/dataframe.py b/marimo/_plugins/ui/_impl/dataframes/dataframe.py
index d7a41748aa..45c3967db5 100644
--- a/marimo/_plugins/ui/_impl/dataframes/dataframe.py
+++ b/marimo/_plugins/ui/_impl/dataframes/dataframe.py
@@ -22,6 +22,7 @@ from marimo._plugins.ui._impl.dataframes.transforms.apply import (
from marimo._plugins.ui._impl.dataframes.transforms.types import (
DataFrameType,
Transformations,
+ normalize_transforms_payload,
)
from marimo._plugins.ui._impl.table import (
DownloadAsArgs,
@@ -309,7 +310,9 @@ class dataframe(UIElement[dict[str, Any], DataFrameType]):
return self._undo(self._transform_container._original_df)
try:
- transformations = parse_raw(value, Transformations)
+ transformations = parse_raw(
+ normalize_transforms_payload(value), Transformations
+ )
result, self._column_types_per_step = (
self._transform_container.apply(transformations)
)
diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/types.py b/marimo/_plugins/ui/_impl/dataframes/transforms/types.py
index 7ae324f013..d877ab06bf 100644
--- a/marimo/_plugins/ui/_impl/dataframes/transforms/types.py
+++ b/marimo/_plugins/ui/_impl/dataframes/transforms/types.py
@@ -296,6 +296,32 @@ class Transformations:
transforms: list[Transform]
+def normalize_transforms_payload(value: dict[str, Any]) -> dict[str, Any]:
+ """Coerce the legacy flat-list `where` shape into a FilterGroup dict.
+
+ The DataFramePlugin frontend can still emit a `filter_rows` transform whose
+ `where` is a bare list of conditions (e.g. on `.form()` submit) instead of
+ a FilterGroup. Normalize it before parsing so `FilterRowsTransform.where`
+ stays a FilterGroup for all downstream consumers.
+ """
+ transforms = value.get("transforms")
+ if not isinstance(transforms, list):
+ return value
+ for transform in transforms:
+ if (
+ isinstance(transform, dict)
+ and transform.get("type") == "filter_rows"
+ and isinstance(transform.get("where"), list)
+ ):
+ transform["where"] = {
+ "type": "group",
+ "operator": "and",
+ "children": transform["where"],
+ "negate": False,
+ }
+ return value
+
+
T = TypeVar("T")
diff --git a/tests/_plugins/ui/_impl/dataframes/test_dataframe.py b/tests/_plugins/ui/_impl/dataframes/test_dataframe.py
index da83e25dc8..fc4ffae4c9 100644
--- a/tests/_plugins/ui/_impl/dataframes/test_dataframe.py
+++ b/tests/_plugins/ui/_impl/dataframes/test_dataframe.py
@@ -459,6 +459,44 @@ class TestDataframes:
assert "Alice" not in names
assert type(subject.value) is type(df)
+ @staticmethod
+ @pytest.mark.skipif(
+ not HAS_DEPS, reason="optional dependencies not installed"
+ )
+ def test_dataframe_filter_rows_accepts_legacy_where_list() -> None:
+ # A filter_rows transform with a legacy list-shaped `where` must still
+ # filter the data instead of silently returning the original df.
+ df = pd.DataFrame(
+ {
+ "name": ["Alice", "Bob", "Charlie"],
+ "age": [25, 30, 35],
+ }
+ )
+ subject = ui.dataframe(df)
+
+ result = subject._convert_value(
+ {
+ "transforms": [
+ {
+ "type": "filter_rows",
+ "operation": "keep_rows",
+ "where": [
+ {
+ "type": "condition",
+ "column_id": "age",
+ "operator": ">",
+ "value": 27,
+ }
+ ],
+ }
+ ]
+ }
+ )
+
+ assert subject._error is None
+ assert type(result) is type(df)
+ assert sorted(result["name"].tolist()) == ["Bob", "Charlie"]
+
@staticmethod
@pytest.mark.skipif(
not HAS_DEPS, reason="optional dependencies not installed"