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: max_columns in tables (#4945)
Shahmir Varqha8ad9b18b9c31
diff --git a/frontend/src/components/data-table/columns.tsx b/frontend/src/components/data-table/columns.tsxindex a11261bfba..31a8606084 100644--- a/frontend/src/components/data-table/columns.tsx+++ b/frontend/src/components/data-table/columns.tsx@@ -31,7 +31,6 @@ import type { CalculateTopKRows } from "@/plugins/impl/DataTablePlugin";// Artificial limit to display long stringsconst MAX_STRING_LENGTH = 50;-export const MAX_COLUMNS = 50;function inferDataType(value: unknown): [type: DataType, displayType: string] {if (typeof value === "string") {diff --git a/frontend/src/plugins/impl/DataTablePlugin.tsx b/frontend/src/plugins/impl/DataTablePlugin.tsxindex 42c5ef678d..907d55769f 100644--- a/frontend/src/plugins/impl/DataTablePlugin.tsx+++ b/frontend/src/plugins/impl/DataTablePlugin.tsx@@ -5,7 +5,6 @@ import { DataTable } from "../../components/data-table/data-table";import {generateColumns,inferFieldTypes,- MAX_COLUMNS,} from "../../components/data-table/columns";import { Labeled } from "./common/labeled";import { Alert, AlertTitle } from "@/components/ui/alert";@@ -116,6 +115,7 @@ interface Data<T> {textJustifyColumns?: Record<string, "left" | "center" | "right">;wrappedColumns?: string[];totalColumns: number;+ maxColumns: number | "all";hasStableRowId: boolean;lazy: boolean;}@@ -183,6 +183,7 @@ export const DataTablePlugin = createPlugin<S>("marimo-table")).nullish(),totalColumns: z.number(),+ maxColumns: z.union([z.number(), z.literal("all")]),hasStableRowId: z.boolean().default(false),cellStyles: z.record(z.record(z.object({}).passthrough())).optional(),// Whether to load the data lazily.@@ -606,6 +607,7 @@ const DataTableComponent = ({label,data,totalRows,+ maxColumns,pagination,selection,value,@@ -674,10 +676,13 @@ const DataTableComponent = ({const memoizedUnclampedFieldTypes =useDeepCompareMemoize(fieldTypesOrInferred);- const memoizedClampedFieldTypes = useMemo(- () => memoizedUnclampedFieldTypes.slice(0, MAX_COLUMNS),- [memoizedUnclampedFieldTypes],- );++ const memoizedClampedFieldTypes = useMemo(() => {+ if (maxColumns === "all") {+ return memoizedUnclampedFieldTypes;+ }+ return memoizedUnclampedFieldTypes.slice(0, maxColumns);+ }, [maxColumns, memoizedUnclampedFieldTypes]);const memoizedRowHeaders = useDeepCompareMemoize(rowHeaders);const memoizedTextJustifyColumns = useDeepCompareMemoize(textJustifyColumns);diff --git a/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx b/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsxindex 62bd63b6f6..d9349fe7bc 100644--- a/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx+++ b/frontend/src/plugins/impl/data-frames/DataFramePlugin.tsx@@ -267,6 +267,7 @@ export const DataFrameComponent = memo(hasStableRowId={false}totalRows={total_rows ?? 0}totalColumns={Object.keys(columns).length}+ maxColumns="all"pageSize={pageSize}pagination={true}fieldTypes={field_types}diff --git a/marimo/_plugins/ui/_impl/table.py b/marimo/_plugins/ui/_impl/table.pyindex 7a38b5bd3b..d4e975b4bd 100644--- a/marimo/_plugins/ui/_impl/table.py+++ b/marimo/_plugins/ui/_impl/table.py@@ -99,8 +99,8 @@ class ColumnSummaries:DEFAULT_MAX_COLUMNS = 50-MaxColumnsNotProvided = Literal["max_columns_not_provided"]-MAX_COLUMNS_NOT_PROVIDED: MaxColumnsNotProvided = "max_columns_not_provided"+MaxColumnsNotProvided = Literal["inherit"]+MAX_COLUMNS_NOT_PROVIDED: MaxColumnsNotProvided = "inherit"@dataclass(frozen=True)@@ -440,6 +440,7 @@ class table(# Holds the original dataself._manager = get_table_manager(data)self._max_columns = max_columns+ max_columns_arg = "all" if max_columns is None else max_columnsif _internal_total_rows is not None:total_rows = _internal_total_rows@@ -595,6 +596,7 @@ class table("data": search_result_data,"total-rows": total_rows,"total-columns": num_columns,+ "max-columns": max_columns_arg,"banner-text": self._get_banner_text(),"pagination": pagination,"page-size": page_size,@@ -960,8 +962,8 @@ class table(- sort: Optional sorting configuration- filters: Optional list of filter conditions- limit: Optional row limit- - max_columns: Optional max number of columns to display. If- `MAX_COLUMNS_NOT_PROVIDED`, default to table's max columns.+ - max_columns: Optional max number of columns. None means show all columns,+ MAX_COLUMNS_NOT_PROVIDED means use the table's max_columns setting.Returns:SearchTableResponse: Response containing:diff --git a/tests/_plugins/ui/_impl/test_table.py b/tests/_plugins/ui/_impl/test_table.pyindex c5f0d7100f..d71a9ff882 100644--- a/tests/_plugins/ui/_impl/test_table.py+++ b/tests/_plugins/ui/_impl/test_table.py@@ -11,6 +11,8 @@ from marimo._dependencies.dependencies import DependencyManagerfrom marimo._plugins import uifrom marimo._plugins.ui._impl.dataframes.transforms.types import Conditionfrom marimo._plugins.ui._impl.table import (+ DEFAULT_MAX_COLUMNS,+ MAX_COLUMNS_NOT_PROVIDED,CalculateTopKRowsArgs,CalculateTopKRowsResponse,DownloadAsArgs,@@ -1283,6 +1285,7 @@ def test_column_clamping():table = ui.table(data)assert len(table._manager.get_column_names()) == 100assert table._component_args["total-columns"] == 100+ assert table._component_args["max-columns"] == DEFAULT_MAX_COLUMNSassert len(json.loads(table._component_args["data"])[0].keys()) == 50assert table._component_args["field-types"] is None@@ -1290,6 +1293,7 @@ def test_column_clamping():table = ui.table(data, max_columns=20)assert len(table._manager.get_column_names()) == 100assert table._component_args["total-columns"] == 100+ assert table._component_args["max-columns"] == 20assert len(json.loads(table._component_args["data"])[0].keys()) == 20assert table._component_args["field-types"] is None@@ -1297,6 +1301,7 @@ def test_column_clamping():table = ui.table(data, max_columns=None)assert len(table._manager.get_column_names()) == 100assert table._component_args["total-columns"] == 100+ assert table._component_args["max-columns"] == "all"assert len(json.loads(table._component_args["data"])[0].keys()) == 100assert table._component_args["field-types"] is None@@ -1308,6 +1313,7 @@ def test_column_clamping_with_small_data():table = ui.table(data)assert len(table._manager.get_column_names()) == 10assert table._component_args["total-columns"] == 10+ assert table._component_args["max-columns"] == DEFAULT_MAX_COLUMNSassert len(json.loads(table._component_args["data"])[0].keys()) == 10assert table._component_args["field-types"] is None@@ -1374,6 +1380,7 @@ def test_column_clamping_with_exact_max_columns():# Check that the table is not clampedassert len(table._manager.get_column_names()) == 50assert table._component_args["total-columns"] == 50+ assert table._component_args["max-columns"] == DEFAULT_MAX_COLUMNSassert len(json.loads(table._component_args["data"])[0].keys()) == 50assert table._component_args["field-types"] is None@@ -1385,6 +1392,7 @@ def test_column_clamping_with_more_than_max_columns():# Check that the table is clampedassert len(table._manager.get_column_names()) == 60assert table._component_args["total-columns"] == 60+ assert table._component_args["max-columns"] == DEFAULT_MAX_COLUMNSassert len(json.loads(table._component_args["data"])[0].keys()) == 50assert table._component_args["field-types"] is None@@ -1395,6 +1403,7 @@ def test_column_clamping_with_no_columns():# Check that the table handles no columns gracefullyassert len(table._manager.get_column_names()) == 1assert table._component_args["total-columns"] == 1+ assert table._component_args["max-columns"] == DEFAULT_MAX_COLUMNSassert len(json.loads(table._component_args["data"])) == 0assert table._component_args["field-types"] is None@@ -1406,6 +1415,7 @@ def test_column_clamping_with_single_column():# Check that the table handles a single column gracefullyassert len(table._manager.get_column_names()) == 1assert table._component_args["total-columns"] == 1+ assert table._component_args["max-columns"] == DEFAULT_MAX_COLUMNSassert len(json.loads(table._component_args["data"])[0].keys()) == 1assert table._component_args["field-types"] is None@@ -1422,6 +1432,7 @@ def test_column_clamping_with_polars():# Check that the table is clampedassert len(table._manager.get_column_names()) == 60assert table._component_args["total-columns"] == 60+ assert table._component_args["max-columns"] == DEFAULT_MAX_COLUMNSjson_data = json.loads(table._component_args["data"])headers = json_data[0].keys()assert len(headers) == 50 # 50 columns@@ -1433,6 +1444,7 @@ def test_column_clamping_with_polars():# Check that the table is clampedassert len(table._manager.get_column_names()) == 60assert table._component_args["total-columns"] == 60+ assert table._component_args["max-columns"] == 40json_data = json.loads(table._component_args["data"])headers = json_data[0].keys()assert len(headers) == 40 # 40 columns@@ -1444,6 +1456,7 @@ def test_column_clamping_with_polars():# Check that the table is not clampedassert len(table._manager.get_column_names()) == 60assert table._component_args["total-columns"] == 60+ assert table._component_args["max-columns"] == "all"json_data = json.loads(table._component_args["data"])headers = json_data[0].keys()@@ -1469,6 +1482,7 @@ def test_dataframe_with_int_column_names():# Check that the table handles integer column names correctlyassert table._manager.get_column_names() == [0, 1, 2]assert table._component_args["total-columns"] == 3+ assert table._component_args["max-columns"] == DEFAULT_MAX_COLUMNSdef test_cell_initial_style():@@ -1640,6 +1654,7 @@ def test_lazy_dataframe() -> None:assert table._component_args["pagination"] is Falseassert table._component_args["data"] == []assert table._component_args["total-columns"] == 0+ assert table._component_args["max-columns"] == DEFAULT_MAX_COLUMNSassert table._component_args["field-types"] is None# Verify that search response indicates "too_many" for total_rows@@ -1763,3 +1778,121 @@ def _convert_data_bytes_to_pandas_df(return pd.read_parquet(io.BytesIO(data_bytes))else:raise ValueError(f"Unsupported data_format: {data_format}")+++def test_max_columns_not_provided():+ # Create data with many columns+ data = {f"col{i}": [1, 2, 3] for i in range(100)}+ table = ui.table(data)++ # Test default behavior (should use DEFAULT_MAX_COLUMNS=50)+ search_args = SearchTableArgs(+ page_size=10, page_number=0, max_columns=MAX_COLUMNS_NOT_PROVIDED+ )+ response = table._search(search_args)+ result_data = json.loads(response.data)+ assert len(result_data[0].keys()) == 50++ # Test when not set (uses MAX_COLUMNS_NOT_PROVIDED as the default)+ search_args = SearchTableArgs(page_size=10, page_number=0)+ response = table._search(search_args)+ result_data = json.loads(response.data)+ assert len(result_data[0].keys()) == 50++ # Test with explicit max_columns+ search_args = SearchTableArgs(page_size=10, page_number=0, max_columns=20)+ response = table._search(search_args)+ result_data = json.loads(response.data)+ assert len(result_data[0].keys()) == 20++ # Test with max_columns=None (show all columns)+ search_args = SearchTableArgs(+ page_size=10, page_number=0, max_columns=None+ )+ response = table._search(search_args)+ result_data = json.loads(response.data)+ assert len(result_data[0].keys()) == 100+++def test_max_columns_not_provided_with_sort():+ # Create data with many columns+ data = {f"col{i}": [1, 2, 3] for i in range(100)}+ table = ui.table(data)++ # Test sort with default max_columns+ search_args = SearchTableArgs(+ page_size=10,+ page_number=0,+ sort=SortArgs(by="col0", descending=True),+ max_columns=MAX_COLUMNS_NOT_PROVIDED,+ )+ response = table._search(search_args)+ result_data = json.loads(response.data)+ assert len(result_data[0].keys()) == 50++ # Test sort with explicit max_columns+ search_args = SearchTableArgs(+ page_size=10,+ page_number=0,+ sort=SortArgs(by="col0", descending=True),+ max_columns=20,+ )+ response = table._search(search_args)+ result_data = json.loads(response.data)+ assert len(result_data[0].keys()) == 20++ # Test sort with max_columns=None+ search_args = SearchTableArgs(+ page_size=10,+ page_number=0,+ sort=SortArgs(by="col0", descending=True),+ max_columns=None,+ )+ response = table._search(search_args)+ result_data = json.loads(response.data)+ assert len(result_data[0].keys()) == 100+++ not DependencyManager.polars.has(),+ reason="Pandas not installed",+)+def test_max_columns_not_provided_with_filters():+ # Create data with many columns+ import polars as pl++ data = pl.DataFrame({f"col{i}": [1, 2, 3] for i in range(100)})+ table = ui.table(data)++ # Test filters with default max_columns+ search_args = SearchTableArgs(+ page_size=10,+ page_number=0,+ filters=[Condition(column_id="col0", operator="==", value=1)],+ max_columns=MAX_COLUMNS_NOT_PROVIDED,+ )+ response = table._search(search_args)+ result_data = json.loads(response.data)+ assert len(result_data[0].keys()) == 50++ # Test filters with explicit max_columns+ search_args = SearchTableArgs(+ page_size=10,+ page_number=0,+ filters=[Condition(column_id="col0", operator="==", value=1)],+ max_columns=20,+ )+ response = table._search(search_args)+ result_data = json.loads(response.data)+ assert len(result_data[0].keys()) == 20++ # Test filters with max_columns=None+ search_args = SearchTableArgs(+ page_size=10,+ page_number=0,+ filters=[Condition(column_id="col0", operator="==", value=1)],+ max_columns=None,+ )+ response = table._search(search_args)+ result_data = json.loads(response.data)+ assert len(result_data[0].keys()) == 101 # +1 for marimo_row_id