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
feat(sql): respect display.dataframes config setting (#8053)
chrisflath190ca31964bf
diff --git a/marimo/_sql/sql.py b/marimo/_sql/sql.pyindex a600aaf95f..8603faaac9 100644--- a/marimo/_sql/sql.py+++ b/marimo/_sql/sql.py@@ -127,18 +127,23 @@ def sql(raise_df_import_error("polars[pyarrow]")if output:+ from marimo._output.formatters.df_formatters import include_opinionated+ from marimo._output.formatting import plainfrom marimo._plugins.stateless.plain_text import plain_textfrom marimo._plugins.ui._impl import tableif isinstance(sql_engine, DuckDBEngine) and is_explain_query(query):# For EXPLAIN queries in DuckDB, display plain output to preserve box drawingstext_output = extract_explain_content(df)- t = plain_text(text_output)+ replace(plain_text(text_output))+ elif not include_opinionated():+ # Respect display.dataframes config - use plain formatting+ replace(plain(df))elif can_narwhalify_lazyframe(df):# For pl.LazyFrame and DuckDBRelation, we only show the first few rows# to avoid loading all the data into memory.# Also preload the first page of data without user confirmation.- t = table.table.lazy(df, preload=True)+ replace(table.table.lazy(df, preload=True))else:# df may be a cursor result from an SQL Engine# In this case, we need to convert it to a DataFrame@@ -148,13 +153,14 @@ def sql(elif DBAPIEngine.is_dbapi_cursor(df):display_df = DBAPIEngine.get_cursor_metadata(df)- t = table.table(- display_df,- selection=None,- pagination=True,- _internal_total_rows=custom_total_count,+ replace(+ table.table(+ display_df,+ selection=None,+ pagination=True,+ _internal_total_rows=custom_total_count,+ ))- replace(t)return dfdiff --git a/tests/_sql/test_sql.py b/tests/_sql/test_sql.pyindex a85aa2ba43..25bc4c2af5 100644--- a/tests/_sql/test_sql.py+++ b/tests/_sql/test_sql.py@@ -629,3 +629,64 @@ SELECT * FROM users WHERE^Expected 'FROM' keyword"""assert strip_explain_from_error_message(multiline_error) == expected+++class TestDisplayConfigBehavior:+ """Test that sql() respects the display.dataframes config setting."""++ @patch("marimo._sql.sql.replace")+ @patch("marimo._output.formatters.df_formatters.include_opinionated")+ @pytest.mark.skipif(+ not HAS_POLARS or not HAS_DUCKDB, reason="polars and duckdb required"+ )+ def test_sql_plain_output_when_not_opinionated(+ self, mock_include_opinionated, mock_replace+ ):+ """Test that SQL uses plain() when include_opinionated returns False."""+ mock_include_opinionated.return_value = False+ import polars as pl++ from marimo._output.formatting import Plain++ # Test query+ result = sql("SELECT 1")+ assert isinstance(result, pl.DataFrame)++ # Should call replace with Plain object+ mock_replace.assert_called_once()+ call_args = mock_replace.call_args[0][0]++ # The call should be a Plain object (not a table)+ assert isinstance(call_args, Plain)++ @pytest.mark.skipif(+ not HAS_POLARS or not HAS_DUCKDB, reason="polars and duckdb required"+ )+ @patch("marimo._sql.sql.replace")+ def test_sql_rich_output_when_opinionated(self, mock_replace):+ """Test that SQL uses table() when include_opinionated returns True."""+ import duckdb+ import polars as pl++ # Create a test table+ duckdb.sql(+ "CREATE OR REPLACE TABLE test_rich AS SELECT * FROM range(5)"+ )++ with patch(+ "marimo._output.formatters.df_formatters.include_opinionated",+ return_value=True,+ ):+ # Test query+ result = sql("SELECT * FROM test_rich")+ assert isinstance(result, pl.DataFrame)++ # Should call replace with table (not Plain)+ mock_replace.assert_called_once()+ call_args = mock_replace.call_args[0][0]++ # The call should be a table object+ assert isinstance(call_args, ui.table)++ # Clean up+ duckdb.sql("DROP TABLE test_rich")