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: cell style not applied correctly (#6824)

Francis Shonubi44c336c7b951
diff --git a/marimo/_plugins/ui/_impl/table.py b/marimo/_plugins/ui/_impl/table.py
index c8f99327d7..fc153c565c 100644
--- a/marimo/_plugins/ui/_impl/table.py
+++ b/marimo/_plugins/ui/_impl/table.py
@@ -1211,7 +1211,11 @@ class table(
return column_preview
def _style_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[CellStyles]:
"""Calculate the styling of the cells in the table."""
if self._style_cell is None:
@@ -1236,6 +1240,8 @@ 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, total_rows - 1 - take, -1)
else:
row_ids = response.row_ids[skip : skip + take]
@@ -1366,20 +1372,24 @@ class table(
# Save the manager to be used for selection
self._searched_manager = result
+ descending = False
+
if self._lazy:
total_rows = "too_many"
else:
total_rows = result.get_num_rows(force=True) or 0
if args.sort and (self._style_cell or self._hover_cell):
- LOGGER.warning(
- "Cell styling/hover may not be correctly applied when table rows are sorted"
- )
+ for element in args.sort:
+ if element.descending:
+ descending = True
return SearchTableResponse(
data=clamp_rows_and_columns(result),
total_rows=total_rows,
- cell_styles=self._style_cells(offset, args.page_size, total_rows),
+ cell_styles=self._style_cells(
+ offset, args.page_size, total_rows, descending
+ ),
cell_hover_texts=self._hover_cells(
offset, args.page_size, total_rows
),
diff --git a/tests/_plugins/ui/_impl/test_table.py b/tests/_plugins/ui/_impl/test_table.py
index 3928043977..439e964238 100644
--- a/tests/_plugins/ui/_impl/test_table.py
+++ b/tests/_plugins/ui/_impl/test_table.py
@@ -1706,7 +1706,6 @@ def test_cell_search_df_styles(df: Any):
exclude=NON_EAGER_LIBS,
),
)
[email protected](reason="Sorted rows are not supported for styling yet")
def test_cell_search_df_styles_sorted(df: Any):
def always_green(_row, _col, _value):
return {"backgroundColor": "green"}