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): pad unequal columns for row selection and search (#11325)
Kiran Gadhave169d793c4686
diff --git a/marimo/_plugins/ui/_impl/tables/default_table.py b/marimo/_plugins/ui/_impl/tables/default_table.pyindex eee53293a7..183e21f7f0 100644--- a/marimo/_plugins/ui/_impl/tables/default_table.py+++ b/marimo/_plugins/ui/_impl/tables/default_table.py@@ -130,13 +130,31 @@ class DefaultTableManager(TableManager[JsonTableData]):).to_parquet()return self._as_table_manager().to_parquet()+ def _padded_columns(self) -> dict[str, Sequence[Any]]:+ """Column-oriented data with every column padded to the row count.++ Shorter columns are padded with `None`, the same way the table is+ rendered and exported, so row indices are valid for every column.+ """+ num_rows = self.get_num_rows()+ return {+ col: (+ values+ if len(values) == num_rows+ else list(values) + [None] * (num_rows - len(values))+ )+ for col, values in cast(+ dict[str, Sequence[Any]], self.data+ ).items()+ }+def select_rows(self, indices: list[int]) -> DefaultTableManager:if isinstance(self.data, dict):# Column major dataif self.is_column_oriented:new_data: dict[Any, Any] = {- key: [cast(list[JSONType], value)[i] for i in indices]- for key, value in self.data.items()+ key: [value[i] for i in indices]+ for key, value in self._padded_columns().items()}return DefaultTableManager(new_data)else:@@ -171,16 +189,15 @@ class DefaultTableManager(TableManager[JsonTableData]):and isinstance(self.data, dict)and all(isinstance(v, list) for v in self.data.values())):+ columns = self._padded_columns()for row_id, column_name in cells:- column = self.data[column_name]- if isinstance(column, Sequence):- selected_cells.append(- TableCell(- row=row_id,- column=column_name,- value=column[int(row_id)],- )+ selected_cells.append(+ TableCell(+ row=row_id,+ column=column_name,+ value=columns[column_name][int(row_id)],)+ )elif isinstance(self.data, dict):rows_of_dict = list(self.data.items())for row_id, column_name in cells:@@ -249,20 +266,14 @@ class DefaultTableManager(TableManager[JsonTableData]):def search(self, query: str) -> DefaultTableManager:query = query.lower()if isinstance(self.data, dict) and self.is_column_oriented:+ columns = self._padded_columns()mask: list[bool] = [- any(- query in str(cast(list[Any], self.data[key])[row]).lower()- for key in self.data- )+ any(query in str(columns[key][row]).lower() for key in columns)for row in range(self.get_num_rows() or 0)]results = {- key: [- cast(list[Any], value)[i]- for i, match in enumerate(mask)- if match- ]- for key, value in self.data.items()+ key: [value[i] for i, match in enumerate(mask) if match]+ for key, value in columns.items()}return DefaultTableManager(cast(JsonTableData, results))return DefaultTableManager(@@ -399,17 +410,7 @@ class DefaultTableManager(TableManager[JsonTableData]):if isinstance(self.data, dict) and self.is_column_oriented:# Column-oriented: sort indices, then reorder all columnsnum_rows = self.get_num_rows()- # Match other table managers, we preserve None values- data_dict = {- col: (- values- if len(values) == num_rows- else list(values) + [None] * (num_rows - len(values))- )- for col, values in cast(- dict[str, Sequence[Any]], self.data- ).items()- }+ data_dict = self._padded_columns()indices = list(range(num_rows))# Apply sorts in reverse order for stable multi-column sortingdiff --git a/tests/_plugins/ui/_impl/tables/test_default_table.py b/tests/_plugins/ui/_impl/tables/test_default_table.pyindex e5dd7628e7..6cb4b98f3b 100644--- a/tests/_plugins/ui/_impl/tables/test_default_table.py+++ b/tests/_plugins/ui/_impl/tables/test_default_table.py@@ -1313,6 +1313,30 @@ class TestMismatchedColumnLengths(unittest.TestCase):{"a": 3, "b": None},]+ def test_select_rows_pads_short_columns(self) -> None:+ data = {"a": [2], "b": [10, 20]}+ mgr = DefaultTableManager(data)+ assert mgr.select_rows([1]).data == {"a": [None], "b": [20]}+ assert mgr.select_rows([0, 1]).data == {"a": [2, None], "b": [10, 20]}+ assert data == {"a": [2], "b": [10, 20]}++ def test_search_pads_short_columns(self) -> None:+ mgr = DefaultTableManager({"a": [2], "b": [10, 20]})+ assert mgr.search("20").data == {"a": [None], "b": [20]}+ assert mgr.search("none").data == {"a": [None], "b": [20]}++ def test_select_cells_pads_short_columns(self) -> None:+ mgr = DefaultTableManager({"a": [2], "b": [10, 20]})+ assert mgr.select_cells(+ [+ TableCoordinate(row_id=1, column_name="a"),+ TableCoordinate(row_id=1, column_name="b"),+ ]+ ) == [+ TableCell(row=1, column="a", value=None),+ TableCell(row=1, column="b", value=20),+ ]+def test_validate_header_tooltip_valid() -> None:columns = {"name", "age", "birth_year"}diff --git a/tests/_plugins/ui/_impl/test_table.py b/tests/_plugins/ui/_impl/test_table.pyindex 99015de3fc..652cef5455 100644--- a/tests/_plugins/ui/_impl/test_table.py+++ b/tests/_plugins/ui/_impl/test_table.py@@ -629,6 +629,15 @@ def test_search_sort_mismatched_columns_with_pagination(assert data == {"a": [2], "b": [10, 20]}+def test_select_rows_mismatched_columns() -> None:+ data = {"a": [2], "b": [10, 20]}+ table = ui.table(data, selection="multi")++ assert table._convert_value(["0", "1"]) == {"a": [2, None], "b": [10, 20]}+ assert table._convert_value(["1"]) == {"a": [None], "b": [20]}+ assert data == {"a": [2], "b": [10, 20]}++def test_search_sort_nonexistent_columns() -> None:data = ["banana", "apple", "cherry", "date", "elderberry"]table = ui.table(data)