Skip to main content

A module containing helpers for paginating responses with FastAPI

Project description

FastAPI Pagination Utilities

This Python package contains utilities to aid in paginating responses from FastAPI applications.


Install fastapi-pagination-utilities using pip:

pip install fastapi-pagination-utilities

The module can then be used as fastapi_pagination:

from fastapi import FastAPI
from fastapi_pagination import (
    CURSOR_QUERY, PAGE_SIZE_QUERY, PaginationDetails, PaginatedResults
from pydantic import BaseModel

app = FastAPI()

class Widget(BaseModel):
    name: str

    summary="List widgets",
    description="List all the widgers which are available."
def list_widgets(
        request: Request,
        cursor: Optional[str] = CURSOR_QUERY,
        page_size: Optional[int] = PAGE_SIZE_QUERY):

    # Get pagination.
    pagination = PaginationDetails(cursor, page_size)

    # list_widgets() should take a page size and offset and return a list of
    # results.
    results, has_more = list_widgets(pagination.offset, pagination.page_size)

    return PaginatedResults(
            if has_more else None
            if pagination.previous_cursor() else None

Developer quickstart

This project contains a dockerized testing environment which wraps tox.

Tests can be run using the ./ command:

# Run all PyTest tests and Flake8 checks
$ ./

# Run just PyTest
$ ./ -e py3

# Run a single test file within PyTest
$ ./ -e py3 -- tests/

# Run a single test file within PyTest with verbose logging
$ ./ -e py3 -- tests/ -vvv

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi-pagination-utilities-0.9.1.tar.gz (4.7 kB view hashes)

Uploaded Source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page