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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: table cell styles index error (#5253)
Shahmir Varqha7583767caf7f
diff --git a/marimo/_plugins/ui/_impl/table.py b/marimo/_plugins/ui/_impl/table.pyindex 73f95d5702..7d730dc30a 100644--- a/marimo/_plugins/ui/_impl/table.py+++ b/marimo/_plugins/ui/_impl/table.py@@ -920,37 +920,39 @@ class table()return column_preview- def _style_cells(self, skip: int, take: int) -> Optional[CellStyles]:+ def _style_cells(+ self, skip: int, take: int, total_rows: Union[int, Literal["too_many"]]+ ) -> Optional[CellStyles]:"""Calculate the styling of the cells in the table."""if self._style_cell is None:return None- else:- def do_style_cell(row: str, col: str) -> dict[str, Any]:- selected_cells = self._searched_manager.select_cells(- [TableCoordinate(row_id=row, column_name=col)]- )- if len(selected_cells) == 0 or self._style_cell is None:- return dict()- else:- return self._style_cell(row, col, selected_cells[0].value)+ def do_style_cell(row: str, col: str) -> dict[str, Any]:+ selected_cells = self._searched_manager.select_cells(+ [TableCoordinate(row_id=row, column_name=col)]+ )+ if not selected_cells or self._style_cell is None:+ return {}+ return self._style_cell(row, col, selected_cells[0].value)- columns = self._searched_manager.get_column_names()- response = self._get_row_ids(EmptyArgs())+ columns = self._searched_manager.get_column_names()+ response = self._get_row_ids(EmptyArgs())- row_ids: Union[list[int], range]- if response.all_rows is True or response.error is not None:- # TODO: Handle sorted rows, they have reverse order of row_ids- row_ids = range(skip, skip + take)- else:- row_ids = response.row_ids[skip : skip + take]+ # Clamp the take to the total number of rows+ if total_rows != "too_many" and skip + take > total_rows:+ take = total_rows - skip- return {- str(row): {- col: do_style_cell(str(row), col) for col in columns- }- for row in row_ids- }+ # Determine row range+ row_ids: Union[list[int], range]+ if response.all_rows or response.error:+ row_ids = range(skip, skip + take)+ else:+ row_ids = response.row_ids[skip : skip + take]++ return {+ str(row): {col: do_style_cell(str(row), col) for col in columns}+ for row in row_ids+ }def _search(self, args: SearchTableArgs) -> SearchTableResponse:"""Search and filter the table data.@@ -1003,13 +1005,8 @@ class table(return SearchTableResponse(data=clamp_rows_and_columns(self._manager),total_rows=total_rows,- # The __init__ will just call this with an arbitrary offset,- # we need to check this is not larger than our actual number of rows.cell_styles=self._style_cells(- offset,- min(total_rows, args.page_size)- if total_rows != "too_many"- else args.page_size,+ offset, args.page_size, total_rows),)@@ -1035,7 +1032,7 @@ class table(return SearchTableResponse(data=clamp_rows_and_columns(result),total_rows=total_rows,- cell_styles=self._style_cells(offset, args.page_size),+ cell_styles=self._style_cells(offset, args.page_size, total_rows),)def _get_row_ids(self, args: EmptyArgs) -> GetRowIdsResponse:diff --git a/marimo/_utils/uv.py b/marimo/_utils/uv.pyindex 651d8162e7..4e79b7f2d2 100644--- a/marimo/_utils/uv.py+++ b/marimo/_utils/uv.py@@ -1,3 +1,4 @@+# Copyright 2025 Marimo. All rights reserved.import osdiff --git a/tests/_plugins/ui/_impl/test_table.py b/tests/_plugins/ui/_impl/test_table.pyindex 686abb0219..88d56b9658 100644--- a/tests/_plugins/ui/_impl/test_table.py+++ b/tests/_plugins/ui/_impl/test_table.py@@ -1520,6 +1520,62 @@ def test_cell_style_of_next_page():assert "green" in cell_styles["2"]["a"]["backgroundColor"]+def test_cell_style_last_page():+ def always_green(_row, _col, _value):+ return {"backgroundColor": "green"}++ data = [{"a": 1}, {"a": 2}, {"a": 3}]+ table = ui.table(data, page_size=2, style_cell=always_green)+ last_page = table._search(SearchTableArgs(page_size=2, page_number=1))+ cell_styles = last_page.cell_styles+ assert len(cell_styles) == 1+ assert "2" in cell_styles+ assert "a" in cell_styles["2"]+ assert "backgroundColor" in cell_styles["2"]["a"]+ assert "green" in cell_styles["2"]["a"]["backgroundColor"]+++def test_cell_style_edge_cases():+ """Test cell styling with various edge cases around page sizes and row IDs."""++ def style_cell(row: str, _col: str, _value: Any) -> dict[str, Any]:+ return {"backgroundColor": "red" if int(row) % 2 == 0 else "blue"}++ # Test with empty data+ table = ui.table([], style_cell=style_cell)+ response = table._search(SearchTableArgs(page_size=10, page_number=0))+ assert response.cell_styles == {}++ # Test with single row+ table = ui.table([{"a": 1}], style_cell=style_cell)+ response = table._search(SearchTableArgs(page_size=10, page_number=0))+ assert response.cell_styles == {"0": {"a": {"backgroundColor": "red"}}}++ # Test with page size larger than total rows+ table = ui.table([{"a": 1}, {"a": 2}], style_cell=style_cell)+ response = table._search(SearchTableArgs(page_size=10, page_number=0))+ assert response.cell_styles == {+ "0": {"a": {"backgroundColor": "red"}},+ "1": {"a": {"backgroundColor": "blue"}},+ }++ # Test with skip beyond total rows+ response = table._search(SearchTableArgs(page_size=10, page_number=1))+ assert response.cell_styles == {}++ # Test with "too_many" total rows+ table = ui.table(+ [{"a": 1}, {"a": 2}],+ style_cell=style_cell,+ _internal_total_rows="too_many",+ )+ response = table._search(SearchTableArgs(page_size=10, page_number=0))+ assert response.cell_styles == {+ "0": {"a": {"backgroundColor": "red"}},+ "1": {"a": {"backgroundColor": "blue"}},+ }++@pytest.mark.skipif(not DependencyManager.polars.has(), reason="Polars not installed")