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
Download table regardless of cell selection (#6391)
Florian Verdonckc792f03bfb51
diff --git a/marimo/_plugins/ui/_impl/table.py b/marimo/_plugins/ui/_impl/table.pyindex ab98ab92f9..1bf062a935 100644--- a/marimo/_plugins/ui/_impl/table.py+++ b/marimo/_plugins/ui/_impl/table.py@@ -765,8 +765,11 @@ class table(def _download_as(self, args: DownloadAsArgs) -> str:"""Download the table data in the specified format.- Downloads selected rows if there are any, otherwise downloads all rows.- Raw data is downloaded without any formatting applied.+ For cell-selection modes ("single-cell"/"multi-cell"), selection is+ ignored and the current searched/filtered view is downloaded. For+ row-selection modes, downloads selected rows if any, otherwise the+ current searched/filtered view. Raw data is downloaded without any+ formatting applied.Args:args (DownloadAsArgs): Arguments specifying the download format.@@ -777,22 +780,26 @@ class table(Raises:ValueError: If format is not 'csv' or 'json'.- NotImplementedError: If download is not supported for cell selection."""+ # For cell-selection modes, ignore selection and download from the+ # searched/filtered view. For row-selection modes, preserve existing+ # behavior: download selected rows if any, otherwise the searched view.+ manager_candidate: Union[TableManager[Any], list[TableCell]]if self._selection in ["single-cell", "multi-cell"]:- raise NotImplementedError(- "Download is not supported for cell selection."+ LOGGER.info(+ "Cell selection downloads aren't supported; downloading all data."+ )+ manager_candidate = self._searched_manager+ else:+ manager_candidate = (+ self._selected_manager+ if self._selected_manager and self._has_any_selection+ else self._searched_manager)-- manager = (- self._selected_manager- if self._selected_manager and self._has_any_selection- else self._searched_manager- )# Remove the selection column before downloading- if isinstance(manager, TableManager):- manager = manager.drop_columns([INDEX_COLUMN_NAME])+ if isinstance(manager_candidate, TableManager):+ manager = manager_candidate.drop_columns([INDEX_COLUMN_NAME])ext = args.formatif ext == "csv":diff --git a/tests/_plugins/ui/_impl/test_table.py b/tests/_plugins/ui/_impl/test_table.pyindex 312167ab8b..7ff3d45812 100644--- a/tests/_plugins/ui/_impl/test_table.py+++ b/tests/_plugins/ui/_impl/test_table.py@@ -1257,10 +1257,11 @@ def test_download_as_pandas() -> None:assert len(filtered_df) == 2assert all(filtered_df["cities"].isin(["Newark", "New York"]))- # Test downloads with selection (includes search from before)- table._convert_value(["1"])+ # Test downloads with row selection (includes search from before)+ table._convert_value(["1"]) # select one row of the filtered viewfor format_type in DOWNLOAD_FORMATS:selected_df = download_and_convert(format_type, table)+ # For row selection, selection is respected (single row)assert len(selected_df) == 1assert selected_df["cities"].iloc[0] == "New York"@@ -1305,19 +1306,29 @@ def test_download_as_polars() -> None:assert len(filtered_df) == 2assert all(filtered_df["cities"].is_in(["Newark", "New York"]))- # Test downloads with selection (includes search from before)- table._convert_value(["1"])+ # Test downloads with row selection (includes search from before)+ table._convert_value(["1"]) # select one row of the filtered viewfor format_type in DOWNLOAD_FORMATS:selected_df = download_and_convert(format_type, table)+ # For row selection, selection is respected (single row)assert len(selected_df) == 1assert selected_df["cities"][0] == "New York"-def test_download_as_for_unsupported_cell_selection() -> None:- for selection in ["single-cell", "multi-cell"]:- table = ui.table(data=[], selection=selection)- with pytest.raises(NotImplementedError):- table._download_as(DownloadAsArgs(format="csv"))+def test_download_as_ignores_cell_selection() -> None:+ # Download should ignore selection when in cell selection modes+ data = {"a": [1, 2, 3]}+ table = ui.table(data, selection="multi-cell")+ table._search(SearchTableArgs(query="2", page_size=10, page_number=0))+ # Make a cell selection; download should still include the filtered view+ table._convert_value([{"rowId": "0", "columnName": "a"}])+ # Use JSON format to avoid optional dependencies+ url = table._download_as(DownloadAsArgs(format="json"))+ data_bytes = from_data_uri(url)[1]+ rows = json.loads(data_bytes)+ assert isinstance(rows, list)+ assert len(rows) == 1+ assert int(rows[0]["a"]) == 2@pytest.mark.skipif(