Skip to content

ListDataSource

The ListDataSource works with Python lists of dictionaries. It is useful for small datasets, testing, demos, or when working with preloaded data.

Basic Usage

from ckanext.tables.shared import ListDataSource

# Sample data
users_data = [
    {"id": 1, "name": "alice", "email": "alice@example.com", "age": 30},
    {"id": 2, "name": "bob", "email": "bob@example.com", "age": 25},
    {"id": 3, "name": "charlie", "email": "charlie@example.com", "age": 35},
]

data_source = ListDataSource(users_data)

Definition

A data source that uses a list of dictionaries as the data source.

This is useful for testing and demo purposes, when you already have data on your hand.

PARAMETER DESCRIPTION
data

The list of dictionaries to use as the data source

TYPE: list[dict[str, Any]]

Source code in ckanext/tables/data_sources.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
class ListDataSource(BaseDataSource):
    """A data source that uses a list of dictionaries as the data source.

    This is useful for testing and demo purposes, when you already have data
    on your hand.

    Args:
        data: The list of dictionaries to use as the data source

    """

    def __init__(self, data: list[dict[str, Any]]):
        self.data = data
        self.filtered = data

    def filter(self, filters: list[FilterItem]) -> Self:
        self.filtered = self.data

        for filter_item in filters:
            pred = self.build_filter(filter_item.field, filter_item.operator, filter_item.value)

            if pred:
                self.filtered = [row for row in self.filtered if pred(row)]

        return self

    def build_filter(self, field: str, operator: str, value: str) -> Callable[[dict[str, Any]], bool] | None:
        string_operators: dict[str, Callable[[str, str], bool]] = {
            "=": lambda a, b: a == b,
            "!=": lambda a, b: a != b,
            "like": lambda a, b: b.lower() in a.lower(),
        }
        ordering_operators: dict[str, Callable[[Any, Any], bool]] = {
            "<": lambda a, b: a < b,
            "<=": lambda a, b: a <= b,
            ">": lambda a, b: a > b,
            ">=": lambda a, b: a >= b,
        }

        if op_func := string_operators.get(operator):
            return lambda row: op_func(str(row.get(field, "")), str(value))

        if op_func := ordering_operators.get(operator):
            return lambda row: op_func(*_numeric_or_string_pair(row.get(field, ""), value))

        return None

    def sort(self, sort_by: str | None, sort_order: str | None) -> Self:
        if not sort_by:
            return self

        self.filtered = sorted(
            self.filtered,
            # A type-stable (bool, str) key: rows missing the field, or with a None
            # value, sort together at one end instead of raising TypeError against
            # rows whose value is an int/str/float mix.
            key=lambda x: (x.get(sort_by) is None, str(x.get(sort_by, ""))),
            reverse=(sort_order or "").lower() == "desc",
        )

        return self

    def paginate(self, page: int, size: int) -> Self:
        if page and size:
            start = (page - 1) * size
            end = start + size
            self.filtered = self.filtered[start:end]
        return self

    def all(self):
        return self.filtered

    def count(self):
        return len(self.filtered)

    def get_columns(self) -> list[str]:
        return list(self.data[0].keys()) if self.data else []

Best Practices

  • Suitable for small amout of data
  • Consider caching expensive data preparation