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 databases frameworks. It also supports async/await syntax and is compatible with Python 3.8 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 databases frameworks, including SQLAlchemy, Tortoise ORM, and PyMongo.
  • Supports async/await syntax.
  • Compatible with Python 3.8 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 bellow:

app-example

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

Uploaded Source

Built Distribution

fastapi_pagination-0.12.32-py3-none-any.whl (42.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_pagination-0.12.32.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.8.18 Linux/6.5.0-1025-azure

File hashes

Hashes for fastapi_pagination-0.12.32.tar.gz
Algorithm Hash digest
SHA256 b808b5b8af493c51d96ae0091b60532b25688cbca1350f39cb72f10d4d69a6ab
MD5 49197bfabcb8ca34d6502564a5fb86cd
BLAKE2b-256 56b6b431ddef4bec5011231ac3deb2da3a334207ef110987bc4971ac40f59865

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_pagination-0.12.32-py3-none-any.whl
Algorithm Hash digest
SHA256 38e7e72abf252cbebbc1beff9081e4929762756c04959c471b2a5866bb7f0aaf
MD5 8272a5cac8a8d9d1eb84e84df111d4b6
BLAKE2b-256 83230d57a0a67e3bf51b5d2d91a52c7bb7421bd8006aea694be01e37e9830901

See more details on using hashes here.

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