Skip to content

table_model

make_hierarchical_test_fetch(data)

Create a fetch_page callback from hierarchical test data.

Parameters:

Name Type Description Default
data dict[str | None, list[dict[str, Any]]]

Mapping of parent_id -> list[row_dict]. None key holds the root-level rows.

required

Returns:

Type Description
Callable[[int, int, str | None, bool, str | None], list[dict[str, Any]]]

A callable suitable for PaginatedTableModel in tree mode.

Source code in ui_preview/table_model.py
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
def make_hierarchical_test_fetch(
    data: dict[str | None, list[dict[str, Any]]],
) -> Callable[[int, int, str | None, bool, str | None], list[dict[str, Any]]]:
    """Create a fetch_page callback from hierarchical test data.

    Args:
        data: Mapping of parent_id -> list[row_dict].
              ``None`` key holds the root-level rows.

    Returns:
        A callable suitable for PaginatedTableModel in tree mode.
    """

    def _fetch(
        page: int,
        page_size: int,
        sort_key: str | None,
        descending: bool,
        parent_id: str | None = None,
    ) -> list[dict[str, Any]]:
        print(
            f"[test]  Fetching page {page} (page_size={page_size}, "
            f"sort_key={sort_key!r}, descending={descending}, "
            f"parent_id={parent_id!r})"
        )
        rows = list(data.get(parent_id, []))
        if sort_key:
            rows = sorted(
                rows,
                key=lambda r: (
                    r.get(sort_key) is None,
                    str(r.get(sort_key, "")),
                ),
                reverse=descending,
            )
        start = page * page_size
        end = start + page_size
        return rows[start:end]

    return _fetch

make_test_fetch(data)

Create a flat fetch_page callback from static data.

parent_id is accepted but ignored — all data lives at root level.

Parameters:

Name Type Description Default
data list[dict[str, Any]]

The full dataset to paginate.

required

Returns:

Type Description
Callable[[int, int, str | None, bool, str | None], list[dict[str, Any]]]

A callable suitable for PaginatedTableModel.

Source code in ui_preview/table_model.py
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def make_test_fetch(
    data: list[dict[str, Any]],
) -> Callable[[int, int, str | None, bool, str | None], list[dict[str, Any]]]:
    """Create a flat fetch_page callback from static data.

    ``parent_id`` is accepted but ignored — all data lives at root level.

    Args:
        data: The full dataset to paginate.

    Returns:
        A callable suitable for PaginatedTableModel.
    """

    def _fetch(
        page: int,
        page_size: int,
        sort_key: str | None,
        descending: bool,
        parent_id: str | None = None,  # noqa: ARG001
    ) -> list[dict[str, Any]]:
        print(
            f"[test]  Fetching page {page} (page_size={page_size}, "
            f"sort_key={sort_key!r}, descending={descending})"
        )
        rows = data
        if sort_key:
            rows = sorted(
                data,
                key=lambda r: (
                    r.get(sort_key) is None,
                    str(r.get(sort_key, "")),
                ),
                reverse=descending,
            )
        start = page * page_size
        end = start + page_size
        return rows[start:end]

    return _fetch