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: download for tables (#4125)
Shahmir Varqha010d7b7fbccd
diff --git a/marimo/_plugins/ui/_impl/table.py b/marimo/_plugins/ui/_impl/table.pyindex 905b175ae9..b8f920ba1b 100644--- a/marimo/_plugins/ui/_impl/table.py+++ b/marimo/_plugins/ui/_impl/table.py@@ -325,6 +325,7 @@ class table(self._selected_manager: Optional[TableManager[Any]] = Noneself._selection = selection+ self._has_any_selection = Falseinitial_value = []if initial_selection and self._manager.supports_selection():if selection == "single" and len(initial_selection) > 1:@@ -476,6 +477,7 @@ class table(for v in valueif isinstance(v, int) or isinstance(v, str)]+ self._has_any_selection = len(indices) > 0if self._has_stable_row_id:# Search across the original dataself._selected_manager = self._manager.select_rows(indices)@@ -483,7 +485,6 @@ class table(self._selected_manager = self._searched_manager.select_rows(indices)- self._has_any_selection = len(indices) > 0return unwrap_narwhals_dataframe(self._selected_manager.data) # type: ignore[no-any-return]def _download_as(self, args: DownloadAsArgs) -> str:diff --git a/tests/_plugins/ui/_impl/test_table.py b/tests/_plugins/ui/_impl/test_table.pyindex cf387fd91e..c06d629927 100644--- a/tests/_plugins/ui/_impl/test_table.py+++ b/tests/_plugins/ui/_impl/test_table.py@@ -9,7 +9,11 @@ import pytestfrom marimo._dependencies.dependencies import DependencyManagerfrom marimo._plugins import uifrom marimo._plugins.ui._impl.dataframes.transforms.types import Condition-from marimo._plugins.ui._impl.table import SearchTableArgs, SortArgs+from marimo._plugins.ui._impl.table import (+ DownloadAsArgs,+ SearchTableArgs,+ SortArgs,+)from marimo._plugins.ui._impl.tables.default_table import DefaultTableManagerfrom marimo._plugins.ui._impl.tables.table_manager import TableCellfrom marimo._plugins.ui._impl.utils.dataframe import TableData@@ -976,6 +980,96 @@ def test_show_download():assert table_false._component_args["show-download"] is False+ not DependencyManager.pandas.has(), reason="Pandas not installed"+)+def test_download_as_pandas() -> None:+ """Test downloading table data as different formats with pandas DataFrame."""+ import io++ import pandas as pd+ from pandas.testing import assert_frame_equal++ data = pd.DataFrame({"cities": ["Newark", "New York", "Los Angeles"]})+ table = ui.table(data)++ def download_and_convert(+ format_type: str, table_instance: ui.table+ ) -> pd.DataFrame:+ """Helper to download and convert table data to DataFrame."""+ download_str = table_instance._download_as(+ DownloadAsArgs(format=format_type)+ )+ data_bytes = from_data_uri(download_str)[1]++ if format_type == "json":+ return pd.read_json(io.BytesIO(data_bytes))+ return pd.read_csv(io.BytesIO(data_bytes))++ # Test base downloads (full data)+ for format_type in ["csv", "json"]:+ downloaded_df = download_and_convert(format_type, table)+ assert_frame_equal(data, downloaded_df)++ # Test downloads with search filter+ table._search(SearchTableArgs(query="New", page_size=10, page_number=0))+ for format_type in ["csv", "json"]:+ filtered_df = download_and_convert(format_type, table)+ assert len(filtered_df) == 2+ assert all(filtered_df["cities"].isin(["Newark", "New York"]))++ # Test downloads with selection (includes search from before)+ table._convert_value(["1"])+ for format_type in ["csv", "json"]:+ selected_df = download_and_convert(format_type, table)+ assert len(selected_df) == 1+ assert selected_df["cities"].iloc[0] == "New York"+++ not DependencyManager.polars.has(), reason="Polars not installed"+)+def test_download_as_polars() -> None:+ """Test downloading table data as different formats with polars DataFrame."""+ import polars as pl+ from polars.testing import assert_frame_equal++ data = pl.DataFrame({"cities": ["Newark", "New York", "Los Angeles"]})+ table = ui.table(data)++ def download_and_convert(+ format_type: str, table_instance: ui.table+ ) -> pl.DataFrame:+ """Helper to download and convert table data to DataFrame."""+ download_str = table_instance._download_as(+ DownloadAsArgs(format=format_type)+ )+ data_bytes = from_data_uri(download_str)[1]++ if format_type == "json":+ return pl.read_json(data_bytes)+ return pl.read_csv(data_bytes)++ # Test base downloads (full data)+ for format_type in ["csv", "json"]:+ downloaded_df = download_and_convert(format_type, table)+ assert_frame_equal(data, downloaded_df)++ # Test downloads with search filter+ table._search(SearchTableArgs(query="New", page_size=10, page_number=0))+ for format_type in ["csv", "json"]:+ filtered_df = download_and_convert(format_type, table)+ assert len(filtered_df) == 2+ assert all(filtered_df["cities"].is_in(["Newark", "New York"]))++ # Test downloads with selection (includes search from before)+ table._convert_value(["1"])+ for format_type in ["csv", "json"]:+ selected_df = download_and_convert(format_type, table)+ assert len(selected_df) == 1+ assert selected_df["cities"][0] == "New York"++def test_pagination_behavior() -> None:# Test with default page_size=10data = {"a": list(range(8))}