FastAPI pagination
Project description
Introduction
fastapi-pagination
is a library that provides pagination feature for FastAPI
applications.
For more information about library please see 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 fastapi_pagination.ext.sqlalchemy import paginate
@app.get('/users', response_model=Page[UserOut])
def get_users(db: Session = Depends(get_db)):
return paginate(db.query(User).order_by(User.created_at))
For SQLAlchemy 2.0 style
you can use paginate
from fastapi_pagination.ext.sqlalchemy_future
module.
from sqlalchemy import select
from fastapi_pagination.ext.sqlalchemy_future 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))
Currently, fastapi-pagination
supports:
Library | paginate function |
---|---|
SQLAlchemy | fastapi_pagination.ext.sqlalchemy.paginate |
SQLAlchemy 2.0 style | fastapi_pagination.ext.sqlalchemy_future.paginate |
Async SQLAlchemy 2.0 style | fastapi_pagination.ext.async_sqlalchemy.paginate |
SQLModel | fastapi_pagination.ext.sqlmodel.paginate |
Async SQLModel | fastapi_pagination.ext.async_sqlmodel.paginate |
AsyncPG | fastapi_pagination.ext.asyncpg.paginate |
Databases | fastapi_pagination.ext.databases.paginate |
Django ORM | fastapi_pagination.ext.django.paginate |
GINO | fastapi_pagination.ext.gino.paginate |
ORM | fastapi_pagination.ext.orm.paginate |
ormar | fastapi_pagination.ext.ormar.paginate |
Piccolo | fastapi_pagination.ext.piccolo.paginate |
Pony ORM | fastapi_pagination.ext.pony.paginate |
Tortoise ORM | fastapi_pagination.ext.tortoise.paginate |
Beanie | fastapi_pagination.ext.beanie.paginate |
PyMongo | fastapi_pagination.ext.pymongo.paginate |
MongoEngine | fastapi_pagination.ext.mongoengine.paginate |
Motor | fastapi_pagination.ext.motor.paginate |
Code from Quickstart
will generate OpenAPI schema as bellow:
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
Built Distribution
Hashes for fastapi_pagination-0.11.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9457b78cd5d0be590cbda506d5913f9d196a7b50768b7943cd0ad6a5f5aa090c |
|
MD5 | 06996990a98213a001c1d1f6d06fb6ff |
|
BLAKE2b-256 | 5824e668ccd75930e2a69aa89eebfa2ff85579ed5d09ef599d4b10f0aca06228 |
Hashes for fastapi_pagination-0.11.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d3e0d52e510cd58e68fa4699999e06abaf7062762af08dc3ce49939924cebbd |
|
MD5 | e3673e1f8789899e8b50b11e1b91c4b6 |
|
BLAKE2b-256 | 7152edf76f92b80865bd1529b6f49a698f192a657cf85e4c91a043f0e7aa4c47 |