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: table pagination for next page (#2790)
Myles Scolnick6627d5331697
diff --git a/marimo/_output/data/data.py b/marimo/_output/data/data.pyindex d35815d352..b9f3a91822 100644--- a/marimo/_output/data/data.py+++ b/marimo/_output/data/data.py@@ -1,4 +1,6 @@# Copyright 2024 Marimo. All rights reserved.+from __future__ import annotations+import base64import iofrom typing import Union@@ -171,3 +173,13 @@ def any_data(data: Union[str, bytes, io.BytesIO], ext: str) -> VirtualFile:return item.virtual_fileraise ValueError(f"Unsupported data type: {type(data)}")+++# Format: data:mime_type;base64,data+def from_data_uri(data: str) -> tuple[str, bytes]:+ assert isinstance(data, str)+ assert data.startswith("data:")+ mime_type, data = data.split(",", 1)+ # strip data: and ;base64+ mime_type = mime_type.split(";")[0][5:]+ return mime_type, base64.b64decode(data)diff --git a/marimo/_plugins/ui/_impl/tables/narwhals_table.py b/marimo/_plugins/ui/_impl/tables/narwhals_table.pyindex 5906f5388f..779e90bbda 100644--- a/marimo/_plugins/ui/_impl/tables/narwhals_table.py+++ b/marimo/_plugins/ui/_impl/tables/narwhals_table.py@@ -127,7 +127,7 @@ class NarwhalsTableManager(raise ValueError("Count must be a positive integer")if offset < 0:raise ValueError("Offset must be a non-negative integer")- return self.with_new_data(self.data[offset:count])+ return self.with_new_data(self.data[offset : offset + count])def search(self, query: str) -> TableManager[Any]:query = query.lower()diff --git a/tests/_plugins/ui/_impl/tables/test_ibis_table.py b/tests/_plugins/ui/_impl/tables/test_ibis_table.pyindex 03e912b0d0..68700f6595 100644--- a/tests/_plugins/ui/_impl/tables/test_ibis_table.py+++ b/tests/_plugins/ui/_impl/tables/test_ibis_table.py@@ -137,6 +137,44 @@ class TestIbisTableManagerFactory(unittest.TestCase):expected_data.to_pandas())+ def test_take(self) -> None:+ assert (+ self.manager.take(1, 0).select_columns(["A"]).to_json()+ == b'[{"A":1}]'+ )+ assert (+ self.manager.take(2, 0).select_columns(["A"]).to_json()+ == b'[{"A":1},{"A":2}]'+ )+ assert (+ self.manager.take(2, 1).select_columns(["A"]).to_json()+ == b'[{"A":2},{"A":3}]'+ )+ assert (+ self.manager.take(2, 2).select_columns(["A"]).to_json()+ == b'[{"A":3}]'+ )++ def test_take_zero(self) -> None:+ limited_manager = self.manager.take(0, 0)+ assert limited_manager.data.count().execute() == 0++ def test_take_negative(self) -> None:+ with pytest.raises(ValueError):+ self.manager.take(-1, 0)++ def test_take_negative_offset(self) -> None:+ with pytest.raises(ValueError):+ self.manager.take(1, -1)++ def test_take_out_of_bounds(self) -> None:+ # Too large of page+ assert self.manager.take(10, 0).data.count().execute() == 3+ assert self.data.count().execute() == 3++ # Too large of page and offset+ assert self.manager.take(10, 10).data.count().execute() == 0+def test_summary_integer(self) -> None:column = "A"summary = self.manager.get_summary(column)diff --git a/tests/_plugins/ui/_impl/tables/test_narwhals.py b/tests/_plugins/ui/_impl/tables/test_narwhals.pyindex 508dc4c7ae..2a094cc9e4 100644--- a/tests/_plugins/ui/_impl/tables/test_narwhals.py+++ b/tests/_plugins/ui/_impl/tables/test_narwhals.py@@ -240,6 +240,24 @@ class TestNarwhalsTableManagerFactory(unittest.TestCase):expected_data = self.data.head(1)assert_frame_equal(limited_manager.data, expected_data)+ def test_take(self) -> None:+ assert self.manager.take(1, 0).data["A"].to_list() == [1]+ assert self.manager.take(2, 0).data["A"].to_list() == [1, 2]+ assert self.manager.take(2, 1).data["A"].to_list() == [2, 3]+ assert self.manager.take(2, 2).data["A"].to_list() == [3]++ def test_take_zero(self) -> None:+ limited_manager = self.manager.take(0, 0)+ assert limited_manager.data.is_empty()++ def test_take_negative(self) -> None:+ with pytest.raises(ValueError):+ self.manager.take(-1, 0)++ def test_take_negative_offset(self) -> None:+ with pytest.raises(ValueError):+ self.manager.take(1, -1)+def test_take_out_of_bounds(self) -> None:# Too large of pageassert len(self.manager.take(10, 0).data) == 3diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.pyindex fa14129fa3..d39640ae59 100644--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py@@ -318,6 +318,24 @@ class TestPandasTableManager(unittest.TestCase):expected_data = self.data.head(limit)assert_frame_equal(limited_manager.data, expected_data)+ def test_take(self) -> None:+ assert self.manager.take(1, 0).data["A"].to_list() == [1]+ assert self.manager.take(2, 0).data["A"].to_list() == [1, 2]+ assert self.manager.take(2, 1).data["A"].to_list() == [2, 3]+ assert self.manager.take(2, 2).data["A"].to_list() == [3]++ def test_take_zero(self) -> None:+ limited_manager = self.manager.take(0, 0)+ assert limited_manager.data.is_empty()++ def test_take_negative(self) -> None:+ with pytest.raises(ValueError):+ self.manager.take(-1, 0)++ def test_take_negative_offset(self) -> None:+ with pytest.raises(ValueError):+ self.manager.take(1, -1)+def test_take_out_of_bounds(self) -> None:# Too large of pageassert len(self.manager.take(10, 0).data) == 3diff --git a/tests/_plugins/ui/_impl/tables/test_polars_table.py b/tests/_plugins/ui/_impl/tables/test_polars_table.pyindex 5fda05333d..bbac0d624c 100644--- a/tests/_plugins/ui/_impl/tables/test_polars_table.py+++ b/tests/_plugins/ui/_impl/tables/test_polars_table.py@@ -265,6 +265,24 @@ class TestPolarsTableManagerFactory(unittest.TestCase):assert "PolarsTableManager" in str(type(limited_manager))assert assert_frame_equal(limited_manager.data, expected_data)+ def test_take(self) -> None:+ assert self.manager.take(1, 0).data["A"].to_list() == [1]+ assert self.manager.take(2, 0).data["A"].to_list() == [1, 2]+ assert self.manager.take(2, 1).data["A"].to_list() == [2, 3]+ assert self.manager.take(2, 2).data["A"].to_list() == [3]++ def test_take_zero(self) -> None:+ limited_manager = self.manager.take(0, 0)+ assert limited_manager.data.is_empty()++ def test_take_negative(self) -> None:+ with pytest.raises(ValueError):+ self.manager.take(-1, 0)++ def test_take_negative_offset(self) -> None:+ with pytest.raises(ValueError):+ self.manager.take(1, -1)+def test_take_out_of_bounds(self) -> None:# Too large of pageassert len(self.manager.take(10, 0).data) == 3diff --git a/tests/_plugins/ui/_impl/test_table.py b/tests/_plugins/ui/_impl/test_table.pyindex c823dc5266..4eeb8e59a7 100644--- a/tests/_plugins/ui/_impl/test_table.py+++ b/tests/_plugins/ui/_impl/test_table.py@@ -7,6 +7,7 @@ from typing import Anyimport pytestfrom marimo._dependencies.dependencies import DependencyManager+from marimo._output.data.data import from_data_urifrom marimo._plugins import uifrom marimo._plugins.ui._impl.dataframes.transforms.types import Conditionfrom marimo._plugins.ui._impl.table import SearchTableArgs, SortArgs@@ -14,6 +15,7 @@ from marimo._plugins.ui._impl.tables.default_table import DefaultTableManagerfrom marimo._plugins.ui._impl.utils.dataframe import TableDatafrom marimo._runtime.functions import EmptyArgsfrom marimo._runtime.runtime import Kernel+from tests._data.mocks import create_dataframes@pytest.fixture@@ -211,24 +213,18 @@ def test_sort_dict_of_tuples(dtm: DefaultTableManager) -> None:def test_value() -> None:data = ["banana", "apple", "cherry", "date", "elderberry"]- data = _normalize_data(data)table = ui.table(data)assert list(table.value) == []def test_value_with_selection() -> None:data = ["banana", "apple", "cherry", "date", "elderberry"]- data = _normalize_data(data)table = ui.table(data)- assert list(table._convert_value(["0", "2"])) == [- {"value": "banana"},- {"value": "cherry"},- ]+ assert list(table._convert_value(["0", "2"])) == ["banana", "cherry"]def test_value_with_sorting_then_selection() -> None:data = ["banana", "apple", "cherry", "date", "elderberry"]- data = _normalize_data(data)table = ui.table(data)table.search(@@ -257,9 +253,37 @@ def test_value_with_sorting_then_selection() -> None:]+ "df",+ create_dataframes(+ {"a": ["x", "z", "y"]}, exclude=["ibis", "duckdb", "pyarrow"]+ ),+)+def test_value_with_sorting_then_selection_dfs(df: Any) -> None:+ import narwhals as nw++ table = ui.table(df)+ table.search(+ SearchTableArgs(+ sort=SortArgs("a", descending=True),+ page_size=10,+ page_number=0,+ )+ )+ assert nw.from_native(table._convert_value(["0"]))["a"][0] == "z"++ table.search(+ SearchTableArgs(+ sort=SortArgs("a", descending=False),+ page_size=10,+ page_number=0,+ )+ )+ assert nw.from_native(table._convert_value(["0"]))["a"][0] == "x"++def test_value_with_search_then_selection() -> None:data = ["banana", "apple", "cherry", "date", "elderberry"]- data = _normalize_data(data)table = ui.table(data)table.search(@@ -291,7 +315,45 @@ def test_value_with_search_then_selection() -> None:page_number=0,))- assert list(table._convert_value(["2"])) == [{"value": "cherry"}]+ assert list(table._convert_value(["2"])) == ["cherry"]+++ "df",+ create_dataframes(+ {"a": ["foo", "bar", "baz"]}, exclude=["ibis", "duckdb", "pyarrow"]+ ),+)+def test_value_with_search_then_selection_dfs(df: Any) -> None:+ import narwhals as nw++ table = ui.table(df)+ table.search(+ SearchTableArgs(+ query="bar",+ page_size=10,+ page_number=0,+ )+ )+ assert nw.from_native(table._convert_value(["0"]))["a"][0] == "bar"++ table.search(+ SearchTableArgs(+ query="foo",+ page_size=10,+ page_number=0,+ )+ )+ assert nw.from_native(table._convert_value(["0"]))["a"][0] == "foo"++ # empty search+ table.search(+ SearchTableArgs(+ page_size=10,+ page_number=0,+ )+ )+ assert nw.from_native(table._convert_value(["2"]))["a"][0] == "baz"def test_table_with_too_many_columns_passes() -> None:@@ -337,6 +399,29 @@ def test_can_get_second_page_with_search() -> None:assert result.data[-1]["a"] == 27+ "df",+ create_dataframes({"a": list(range(40))}, ["ibis"]),+)+def test_can_get_second_page_with_search_df(df: Any) -> None:+ import polars as pl++ table = ui.table(df)+ result = table.search(+ SearchTableArgs(+ query="2",+ page_size=5,+ page_number=1,+ )+ )+ mime_type, data = from_data_uri(result.data)+ assert mime_type == "text/csv"+ data = pl.read_csv(data)+ assert len(data) == 5+ assert int(data["a"][0]) == 23+ assert int(data["a"][-1]) == 27++def test_with_no_pagination() -> None:data = {"a": list(range(20))}table = ui.table(data, pagination=False)