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 value after sorting, when columnar dictionary (#3787)
Myles Scolnick1a5feffb8348
diff --git a/marimo/_plugins/ui/_impl/tables/default_table.py b/marimo/_plugins/ui/_impl/tables/default_table.pyindex 7c7bc1f580..627320f84d 100644--- a/marimo/_plugins/ui/_impl/tables/default_table.py+++ b/marimo/_plugins/ui/_impl/tables/default_table.py@@ -256,6 +256,36 @@ class DefaultTableManager(TableManager[JsonTableData]):def sort_values(self, by: ColumnName, descending: bool) -> DefaultTableManager:+ if isinstance(self.data, dict) and self.is_column_oriented:+ # For column-oriented data, extract the sort column and get sorted indices+ sort_column = cast(List[Any], self.data[by])+ try:+ sorted_indices = sorted(+ range(len(sort_column)),+ key=lambda i: sort_column[i],+ reverse=descending,+ )+ except TypeError:+ # Handle when values are not comparable+ sorted_indices = sorted(+ range(len(sort_column)),+ key=lambda i: str(sort_column[i]),+ reverse=descending,+ )+ # Apply sorted indices to each column while maintaining column orientation+ return DefaultTableManager(+ cast(+ JsonTableData,+ {+ col: [+ cast(List[Any], values)[i] for i in sorted_indices+ ]+ for col, values in self.data.items()+ },+ )+ )++ # For row-major data, continue with existing logicnormalized = self._normalize_data(self.data)try:data = sorted(normalized, key=lambda x: x[by], reverse=descending)diff --git a/marimo/_smoke_tests/tables/columnar_tables.py b/marimo/_smoke_tests/tables/columnar_tables.pynew file mode 100644index 0000000000..8df0562581--- /dev/null+++ b/marimo/_smoke_tests/tables/columnar_tables.py@@ -0,0 +1,46 @@+import marimo++__generated_with = "0.11.2"+app = marimo.App(width="medium")+++def _():+ import marimo as mo+ return (mo,)+++def _():+ import polars as pl+ return (pl,)+++def _(pl):+ test_df = pl.DataFrame({"num": [0, 1]})+ return (test_df,)+++def _(mo, test_df):+ table = mo.ui.table(test_df.to_dict(as_series=False))+ table+ return (table,)+++def _(table):+ table.value+ return+++def test_is_dict_of_lists(table):+ assert "num" in table.value+ assert isinstance(table.value["num"], list)+ return+++if __name__ == "__main__":+ app.run()diff --git a/tests/_plugins/ui/_impl/tables/test_default_table.py b/tests/_plugins/ui/_impl/tables/test_default_table.pyindex d53443a480..d515b28713 100644--- a/tests/_plugins/ui/_impl/tables/test_default_table.py+++ b/tests/_plugins/ui/_impl/tables/test_default_table.py@@ -354,13 +354,17 @@ class TestColumnarDefaultTable(unittest.TestCase):def test_sort(self) -> None:sorted_data = self.manager.sort_values(by="name", descending=True).data- expected_data = [- {"name": "Eve", "age": 22, "birth_year": date(2002, 1, 30)},- {"name": "Dave", "age": 28, "birth_year": date(1996, 3, 5)},- {"name": "Charlie", "age": 35, "birth_year": date(1989, 12, 1)},- {"name": "Bob", "age": 25, "birth_year": date(1999, 7, 14)},- {"name": "Alice", "age": 30, "birth_year": date(1994, 5, 24)},- ]+ expected_data = {+ "name": ["Eve", "Dave", "Charlie", "Bob", "Alice"],+ "age": [22, 28, 35, 25, 30],+ "birth_year": [+ date(2002, 1, 30),+ date(1996, 3, 5),+ date(1989, 12, 1),+ date(1999, 7, 14),+ date(1994, 5, 24),+ ],+ }assert sorted_data == expected_data@pytest.mark.skipif(