Skip to content

Paginated Module Reference

paginated is a utility module for offset pagination related functions.

Function Definition

compute_offset(page, items_per_page)

Calculate the offset for pagination based on the given page number and items per page.

The offset represents the starting point in a dataset for the items on a given page. For example, if each page displays 10 items and you want to display page 3, the offset will be 20, meaning the display should start with the 21st item.

Parameters:

Name Type Description Default
page int

The current page number. Page numbers should start from 1.

required
items_per_page int

The number of items to be displayed on each page.

required

Returns:

Type Description
int

The calculated offset.

Examples:

>>> offset(1, 10)
0
>>> offset(3, 10)
20
Source code in fastcrud/paginated/helper.py
def compute_offset(page: int, items_per_page: int) -> int:
    """Calculate the offset for pagination based on the given page number and items per page.

    The offset represents the starting point in a dataset for the items on a given page.
    For example, if each page displays 10 items and you want to display page 3, the offset will be 20,
    meaning the display should start with the 21st item.

    Args:
        page: The current page number. Page numbers should start from 1.
        items_per_page: The number of items to be displayed on each page.

    Returns:
        The calculated offset.

    Examples:
        >>> offset(1, 10)
        0
        >>> offset(3, 10)
        20
    """
    return (page - 1) * items_per_page

paginated_response(crud_data, page, items_per_page)

Create a paginated response based on the provided data and pagination parameters.

Parameters:

Name Type Description Default
crud_data dict

Data to be paginated, including the list of items and total count.

required
page int

Current page number.

required
items_per_page int

Number of items per page.

required

Returns:

Type Description
dict[str, Any]

A structured paginated response dict containing the list of items, total count, pagination flags, and numbers.

Note

The function does not actually paginate the data but formats the response to indicate pagination metadata.

Source code in fastcrud/paginated/response.py
def paginated_response(
    crud_data: dict, page: int, items_per_page: int
) -> dict[str, Any]:
    """Create a paginated response based on the provided data and pagination parameters.

    Args:
        crud_data: Data to be paginated, including the list of items and total count.
        page: Current page number.
        items_per_page: Number of items per page.

    Returns:
        A structured paginated response dict containing the list of items, total count, pagination flags, and numbers.

    Note:
        The function does not actually paginate the data but formats the response to indicate pagination metadata.
    """
    return {
        "data": crud_data["data"],
        "total_count": crud_data["total_count"],
        "has_more": (page * items_per_page) < crud_data["total_count"],
        "page": page,
        "items_per_page": items_per_page,
    }