Skip to main content

FastAPI pagination

Project description

logo

license test codecov downloads pypi black

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


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


users = [  # create some data
    # ...
]


@app.get('/users', response_model=Page[UserOut])  # use Page[UserOut] as response model
async def get_users():
    return paginate(users)  # use paginate function to paginate your data


add_pagination(app)  # important! add pagination to your app

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', response_model=Page[UserOut])
def get_users(db: Session = Depends(get_db)):
    return paginate(db, select(User).order_by(User.created_at))

Code from Quickstart will generate OpenAPI schema as bellow:

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.12.8.tar.gz (21.2 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.12.8-py3-none-any.whl (35.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_pagination-0.12.8.tar.gz
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.8.17 Linux/5.15.0-1042-azure

File hashes

Hashes for fastapi_pagination-0.12.8.tar.gz
Algorithm Hash digest
SHA256 7f740346e3b40038de13eb59941c47d7fbd1ea8f99346b2f571d5c7759400aa2
MD5 0392dea91fa7c8405d4131a8f7daeb0e
BLAKE2b-256 caffbcb724eef81f83f424b88fef607adf5b5124c4a9938c6824f375ca3988f3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_pagination-0.12.8-py3-none-any.whl
  • Upload date:
  • Size: 35.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.8.17 Linux/5.15.0-1042-azure

File hashes

Hashes for fastapi_pagination-0.12.8-py3-none-any.whl
Algorithm Hash digest
SHA256 d162fe05d5725301229d47b773eff62f578bdd4db9a97b521151a656e5d0b489
MD5 f43ff005dcde6c81c881b6b7701ddee4
BLAKE2b-256 7eae099edde1b9f0146b88e4689cc3d26f1cd98a493c70ffdcac5ac2b431857a

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