Skip to main content

FastAPI pagination

Project description

logo

license test codecov downloads pypi

Introduction

fastapi-pagination is a Python library designed to simplify pagination in FastAPI applications. It provides a set of utility functions and data models to help you paginate your database queries and return paginated responses to your clients.

With fastapi-pagination, you can easily define pagination parameters in your FastAPI endpoint functions, and use them to generate paginated responses that include the requested subset of your data. The library supports a variety of pagination strategies, including cursor-based pagination and page-based pagination.

fastapi-pagination is built on top of the popular fastapi library, and it works with a wide range of SQL and NoSQL database frameworks. It also supports async/await syntax and is compatible with Python 3.10 and higher.

Features:

  • Simplifies pagination in FastAPI applications.
  • Supports a variety of pagination strategies, including cursor-based pagination and page-based pagination
  • Works with a wide range of SQL and NoSQL database frameworks, including SQLAlchemy, Tortoise ORM, and PyMongo.
  • Supports async/await syntax.
  • Compatible with Python 3.10 and higher.

For more information on how to use fastapi-pagination, please refer to the official documentation.


Installation

pip install fastapi-pagination

Quickstart

All you need to do is to use Page class as a return type for your endpoint and call paginate function on data you want to paginate.

from fastapi import FastAPI
from pydantic import BaseModel, Field

# import all you need from fastapi-pagination
from fastapi_pagination import Page, add_pagination, paginate

app = FastAPI()  # create FastAPI app
add_pagination(app)  # important! add pagination to your app


class UserOut(BaseModel):  # define your model
    name: str = Field(..., example="Steve")
    surname: str = Field(..., example="Rogers")


users = [  # create some data
    UserOut(name="Steve", surname="Rogers"),
    # ...
]


# req: GET /users
@app.get("/users")
async def get_users() -> Page[UserOut]:
    # use Page[UserOut] as return type annotation
    return paginate(users)  # use paginate function to paginate your data

Please, be careful when you work with databases, because default paginate will require to load all data in memory.

For instance, if you use SQLAlchemy you can use paginate from fastapi_pagination.ext.sqlalchemy module.

from sqlalchemy import select
from fastapi_pagination.ext.sqlalchemy import paginate


@app.get("/users")
def get_users(db: Session = Depends(get_db)) -> Page[UserOut]:
    return paginate(db, select(User).order_by(User.created_at))

Code from Quickstart will generate OpenAPI schema as below:

app-example

Project details


Release history Release notifications | RSS feed

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-0.15.13.tar.gz (606.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_pagination-0.15.13-py3-none-any.whl (65.4 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_pagination-0.15.13.tar.gz.

File metadata

  • Download URL: fastapi_pagination-0.15.13.tar.gz
  • Upload date:
  • Size: 606.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastapi_pagination-0.15.13.tar.gz
Algorithm Hash digest
SHA256 2ed92e90085fdeb818fa88432e1b2d1b66aa3240b7c09f1eec7ba55e069a86ca
MD5 2f778d0ecafaaf5e7884f24f720b7844
BLAKE2b-256 f9d533326364b30976148e92baad853b8fbe34f3a33f2dd40ed1fd70d17b1d3b

See more details on using hashes here.

File details

Details for the file fastapi_pagination-0.15.13-py3-none-any.whl.

File metadata

  • Download URL: fastapi_pagination-0.15.13-py3-none-any.whl
  • Upload date:
  • Size: 65.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for fastapi_pagination-0.15.13-py3-none-any.whl
Algorithm Hash digest
SHA256 95c6733f7c93280ccfa551c3a76722ccea2ffb3ad6a5e4a733cbbf03df4c6b30
MD5 02853a46ce724038af72d9b28d67115a
BLAKE2b-256 2adc5f770e102a3500a1a363e72d9f2b1f05e37867a5ef1c625ff23a566e6143

See more details on using hashes here.

Supported by

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