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.

improvement: cell styles on sort/pagination (#7452)

Myles Scolnickcd62127b508a
diff --git a/marimo/_plugins/ui/_impl/table.py b/marimo/_plugins/ui/_impl/table.py
index 3e4c25f93e..a13ae4455d 100644
--- a/marimo/_plugins/ui/_impl/table.py
+++ b/marimo/_plugins/ui/_impl/table.py
@@ -319,9 +319,6 @@ class table(
table
```
- Note: when sorting is applied, per-cell hover and styling may not be
- correctly aligned with rows, similar to cell styling behavior.
-
In each case, access the table data with `table.value`.
Attributes:
@@ -1255,7 +1252,9 @@ class table(
if response.all_rows or response.error:
row_ids = range(skip, skip + take)
if descending and total_rows != "too_many":
- row_ids = range(total_rows - 1, total_rows - 1 - take, -1)
+ row_ids = range(
+ total_rows - 1 - skip, total_rows - 1 - skip - take, -1
+ )
else:
row_ids = response.row_ids[skip : skip + take]
@@ -1265,7 +1264,11 @@ class table(
}
def _hover_cells(
- self, skip: int, take: int, total_rows: Union[int, Literal["too_many"]]
+ self,
+ skip: int,
+ take: int,
+ total_rows: Union[int, Literal["too_many"]],
+ descending: bool = False,
) -> Optional[dict[RowId, dict[ColumnName, Optional[str]]]]:
"""Calculate hover text for cells in the table (plain strings or None)."""
if self._hover_cell is None:
@@ -1298,6 +1301,10 @@ class table(
row_ids: Union[list[int], range]
if response.all_rows or response.error:
row_ids = range(skip, skip + take)
+ if descending and total_rows != "too_many":
+ row_ids = range(
+ total_rows - 1 - skip, total_rows - 1 - skip - take, -1
+ )
else:
row_ids = response.row_ids[skip : skip + take]
@@ -1405,7 +1412,7 @@ class table(
offset, args.page_size, total_rows, descending
),
cell_hover_texts=self._hover_cells(
- offset, args.page_size, total_rows
+ offset, args.page_size, total_rows, descending
),
)
diff --git a/tests/_plugins/ui/_impl/test_table.py b/tests/_plugins/ui/_impl/test_table.py
index 5902b603cc..990851cf3c 100644
--- a/tests/_plugins/ui/_impl/test_table.py
+++ b/tests/_plugins/ui/_impl/test_table.py
@@ -1773,6 +1773,89 @@ def test_cell_search_df_styles_sorted(df: Any):
}
+ "df",
+ create_dataframes(
+ {
+ "Index": list(range(20)),
+ "Category": [f"Label {i % 5}" for i in range(20)],
+ "Value": [i * ((-1) ** i) for i in range(20)],
+ },
+ exclude=NON_EAGER_LIBS,
+ ),
+)
+def test_cell_styles_sorted_with_pagination(df: Any):
+ """Test that cell styles are correctly applied with sorting and pagination.
+
+ Regression test for issue #6223 - cell styles should work on all pages,
+ not just the last N rows of the entire dataset.
+ """
+
+ def cell_style(_row_id, _column_name, value):
+ # Handle both Python numbers and numpy numbers
+ try:
+ numeric_value = float(value)
+ return {"color": "black" if numeric_value > 0 else "red"}
+ except (TypeError, ValueError):
+ return {}
+
+ table = ui.table(df, style_cell=cell_style)
+
+ # Test page 0 (rows 19, 18, 17, 16, 15 when sorted descending by Index)
+ page0 = table._search(
+ SearchTableArgs(
+ page_size=5,
+ page_number=0,
+ query="",
+ sort=[SortArgs(by="Index", descending=True)],
+ )
+ )
+ assert "19" in page0.cell_styles
+ assert "18" in page0.cell_styles
+ assert "17" in page0.cell_styles
+ assert "16" in page0.cell_styles
+ assert "15" in page0.cell_styles
+ # Check that the style function was applied
+ # i=19: 19 * ((-1)**19) = 19 * -1 = -19 (negative, red)
+ assert page0.cell_styles["19"]["Value"] == {"color": "red"}
+ # i=18: 18 * ((-1)**18) = 18 * 1 = 18 (positive, black)
+ assert page0.cell_styles["18"]["Value"] == {"color": "black"}
+
+ # Test page 1 (rows 14, 13, 12, 11, 10 when sorted descending)
+ page1 = table._search(
+ SearchTableArgs(
+ page_size=5,
+ page_number=1,
+ query="",
+ sort=[SortArgs(by="Index", descending=True)],
+ )
+ )
+ assert "14" in page1.cell_styles
+ assert "13" in page1.cell_styles
+ assert "12" in page1.cell_styles
+ assert "11" in page1.cell_styles
+ assert "10" in page1.cell_styles
+ # i=13: 13 * ((-1)**13) = 13 * -1 = -13 (negative, red)
+ assert page1.cell_styles["13"]["Value"] == {"color": "red"}
+
+ # Test page 2 (rows 9, 8, 7, 6, 5 when sorted descending)
+ page2 = table._search(
+ SearchTableArgs(
+ page_size=5,
+ page_number=2,
+ query="",
+ sort=[SortArgs(by="Index", descending=True)],
+ )
+ )
+ assert "9" in page2.cell_styles
+ assert "8" in page2.cell_styles
+ assert "7" in page2.cell_styles
+ assert "6" in page2.cell_styles
+ assert "5" in page2.cell_styles
+ # i=9: 9 * ((-1)**9) = 9 * -1 = -9 (negative, red)
+ assert page2.cell_styles["9"]["Value"] == {"color": "red"}
+
+
@pytest.mark.skipif(
not DependencyManager.pandas.has(),
reason="Pandas not installed, only pandas has multi-col idx",