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: mo.ui.dataframe with ibis and polars backend (#2647)

Myles Scolnick7d713c24ab2e
diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py b/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
index d456eb0f4d..f6afef659b 100644
--- a/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
+++ b/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
@@ -630,10 +630,14 @@ class IbisTransformHandler(TransformHandler["ibis.Table"]):
# )
@staticmethod
- def as_sql_code(transformed_df: "ibis.Table") -> str:
+ def as_sql_code(transformed_df: "ibis.Table") -> str | None:
import ibis
- return str(ibis.to_sql(transformed_df))
+ try:
+ return str(ibis.to_sql(transformed_df))
+ except Exception:
+ # In case it is not a SQL backend
+ return None
def _coerce_value(dtype: Any, value: Any) -> Any:
diff --git a/tests/_plugins/ui/_impl/dataframes/test_dataframe.py b/tests/_plugins/ui/_impl/dataframes/test_dataframe.py
index 02e4a984fe..e179679e4b 100644
--- a/tests/_plugins/ui/_impl/dataframes/test_dataframe.py
+++ b/tests/_plugins/ui/_impl/dataframes/test_dataframe.py
@@ -22,6 +22,9 @@ HAS_DEPS = (
and DependencyManager.polars.has()
)
+HAS_IBIS = DependencyManager.ibis.has()
+HAS_POLARS = DependencyManager.polars.has()
+
if HAS_DEPS:
import pandas as pd
import polars as pl
@@ -222,3 +225,27 @@ class TestDataframes:
# Test ColumnNotFound error
with pytest.raises(ColumnNotFound):
subject.get_column_values(GetColumnValuesArgs(column="C"))
+
+
+ not HAS_IBIS or not HAS_POLARS,
+ reason="optional dependencies not installed",
+)
+def test_ibis_with_polars_backend() -> None:
+ import ibis
+
+ import marimo as mo
+
+ prev_backend = ibis.get_backend()
+ ibis.set_backend("polars")
+
+ data = {
+ "a": [1, 2, 3],
+ "b": [22.5, 23.0, 21.5],
+ }
+ memtable = ibis.memtable(data)
+ dataframe = mo.ui.dataframe(memtable)
+ assert dataframe is not None
+ assert dataframe.get_dataframe(EmptyArgs()).total_rows == 3
+ assert dataframe.get_dataframe(EmptyArgs()).sql_code is None
+ ibis.set_backend(prev_backend)