Skip to content

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.

fix(table): contain upstream export failures (#11198)

Shahmir Varqha7d11995c6b86
diff --git a/marimo/_plugins/ui/_impl/table.py b/marimo/_plugins/ui/_impl/table.py
index 42e2fa8a8c..07e711f801 100644
--- a/marimo/_plugins/ui/_impl/table.py
+++ b/marimo/_plugins/ui/_impl/table.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import functools
+from asyncio import CancelledError
from dataclasses import dataclass
from typing import (
TYPE_CHECKING,
@@ -1195,6 +1196,19 @@ class table(
column=e.column,
missing_packages=e.missing_packages,
)
+ except BaseException as error:
+ if isinstance(
+ error,
+ (
+ KeyboardInterrupt,
+ SystemExit,
+ GeneratorExit,
+ CancelledError,
+ ),
+ ):
+ raise
+ LOGGER.exception("Table download failed")
+ return DownloadAsResponse(error=str(error))
return DownloadAsResponse(url=url, filename=filename)
else:
raise NotImplementedError(
@@ -1456,8 +1470,13 @@ class table(
return data_url, "csv"
except (ValueError, NotImplementedError):
LOGGER.debug("CSV export failed, falling back to JSON.")
- except Exception as e:
- LOGGER.error("Unexpected error exporting CSV: %s", e)
+ except BaseException as error:
+ if isinstance(
+ error,
+ (KeyboardInterrupt, SystemExit, GeneratorExit, CancelledError),
+ ):
+ raise
+ LOGGER.exception("Unexpected error exporting CSV")
# Fallback to JSON
try:
diff --git a/tests/_plugins/ui/_impl/test_table.py b/tests/_plugins/ui/_impl/test_table.py
index 7f2ef95e33..911a96f991 100644
--- a/tests/_plugins/ui/_impl/test_table.py
+++ b/tests/_plugins/ui/_impl/test_table.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import csv
import io
import json
+from asyncio import CancelledError
from datetime import date, timedelta
from enum import Enum
from typing import TYPE_CHECKING, Any
@@ -27,6 +28,7 @@ from marimo._plugins.ui._impl.table import (
ColumnSummariesArgs,
DownloadAsArgs,
DownloadAsOptions,
+ DownloadAsResponse,
SearchTableArgs,
SortArgs,
TableSearchError,
@@ -1965,7 +1967,7 @@ def test_geometry_text_download_preserves_row_selection() -> None:
@pytest.mark.requires("pyarrow", "shapely")
def test_invalid_wkb_does_not_publish_an_artifact() -> None:
import pyarrow as pa
- from shapely.errors import GEOSException
+ import shapely
metadata = {b"ARROW:extension:name": b"geoarrow.wkb"}
field = pa.field("geometry", pa.binary(), metadata=metadata)
@@ -1979,14 +1981,14 @@ def test_invalid_wkb_does_not_publish_an_artifact() -> None:
schema=pa.schema([field]),
)
- with (
- patch(
- "marimo._plugins.ui._impl.utils.dataframe.mo_data.any_data"
- ) as publish,
- pytest.raises(GEOSException),
- ):
- ui.table(source)._download_as(DownloadAsArgs(format="json"))
+ with pytest.raises(shapely.errors.GEOSException) as expected:
+ shapely.from_wkb(b"invalid")
+ with patch(
+ "marimo._plugins.ui._impl.utils.dataframe.mo_data.any_data"
+ ) as publish:
+ response = ui.table(source)._download_as(DownloadAsArgs(format="json"))
+ assert response == DownloadAsResponse(error=str(expected.value))
publish.assert_not_called()
@@ -3806,3 +3808,85 @@ def test_column_widths_unknown_column():
def test_column_widths_non_positive():
with pytest.raises(ValueError, match="positive integer"):
ui.table({"a": [1, 2]}, column_widths={"a": 0})
+
+
+ "error",
+ [KeyboardInterrupt(), SystemExit(), GeneratorExit(), CancelledError()],
+)
+def test_download_preserves_control_flow(error: BaseException) -> None:
+ table = ui.table({"a": [1]})
+ with patch(
+ "marimo._plugins.ui._impl.table.download_as", side_effect=error
+ ):
+ with pytest.raises(type(error)):
+ table._download_as(DownloadAsArgs(format="csv"))
+
+
+ "error",
+ [KeyboardInterrupt(), SystemExit(), GeneratorExit(), CancelledError()],
+)
+def test_chart_csv_preserves_control_flow(error: BaseException) -> None:
+ table = ui.table({"a": [1]})
+ manager = table._searched_manager
+ with (
+ patch.object(DependencyManager.pyarrow, "has", return_value=False),
+ patch.object(type(manager), "to_csv", side_effect=error),
+ ):
+ with pytest.raises(type(error)):
+ table._to_chart_data_url(manager)
+
+
[email protected]("lazy", [False, True])
[email protected]("container", ["map", "struct"])
+def test_binary_export_failure_is_contained(
+ lazy: bool, container: str
+) -> None:
+ import polars as pl
+
+ from marimo._plugins.ui._impl.tables.polars_table import (
+ PolarsTableManagerFactory,
+ )
+
+ map_dtype = getattr(pl, "Map", None)
+ if container == "map":
+ if map_dtype is None:
+ pytest.skip("Map dtype requires Polars 2")
+ values = [{1: b"abc"}, {}, None]
+ dtype = map_dtype(pl.Int64, pl.Binary)
+ expected_error = pl.exceptions.ComputeError
+ expected_json = [
+ {"value": {"1": "abc"}},
+ {"value": {}},
+ {"value": None},
+ ]
+ else:
+ values = [{"binary": b"abc"}, {"binary": None}, None]
+ dtype = pl.Struct({"binary": pl.Binary})
+ expected_error = pl.exceptions.PanicException
+ expected_json = [
+ {"value": {"binary": "abc"}},
+ {"value": {"binary": None}},
+ {"value": None},
+ ]
+ source = pl.DataFrame({"value": pl.Series(values, dtype=dtype)})
+ data = source.lazy() if lazy else source
+ manager = PolarsTableManagerFactory.create()(data)
+ with pytest.raises(expected_error) as original:
+ manager.to_csv_str()
+
+ table = ui.table(data)
+ with patch(
+ "marimo._plugins.ui._impl.utils.dataframe.mo_data.any_data"
+ ) as publish:
+ response = table._download_as(DownloadAsArgs(format="csv"))
+ assert response == DownloadAsResponse(error=str(original.value))
+ publish.assert_not_called()
+
+ with patch.object(DependencyManager.pyarrow, "has", return_value=False):
+ url, data_format = table._to_chart_data_url(manager)
+ assert data_format == "json"
+ assert json.loads(from_data_uri(url)[1]) == expected_json
+ assert manager.collect().equals(source)