Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

FastAPI pagination 📖

License

NotificationsYou must be signed in to change notification settings

uriyyo/fastapi-pagination

Repository files navigation

logo

licensetestcodecovdownloadspypi

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 queriesand return paginated responses to your clients.

Withfastapi-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 popularfastapi library, and it works with a wide rangeof SQL and NoSQL databases frameworks. It also supports async/await syntax and is compatible with Python 3.9 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, includingSQLAlchemy,Tortoise ORM, andPyMongo.
  • Supports async/await syntax.
  • Compatible with Python 3.9 and higher.

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


Installation

pip install fastapi-pagination

Quickstart

All you need to do is to usePage class as a return type for your endpoint and callpaginate functionon data you want to paginate.

fromfastapiimportFastAPIfrompydanticimportBaseModel,Field# import all you need from fastapi-paginationfromfastapi_paginationimportPage,add_pagination,paginateapp=FastAPI()# create FastAPI appadd_pagination(app)# important! add pagination to your appclassUserOut(BaseModel):# define your modelname:str=Field(...,example="Steve")surname:str=Field(...,example="Rogers")users= [# create some dataUserOut(name="Steve",surname="Rogers"),# ...]# req: GET /users@app.get("/users")asyncdefget_users()->Page[UserOut]:# use Page[UserOut] as return type annotationreturnpaginate(users)# use paginate function to paginate your data

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

For instance, if you useSQLAlchemy you can usepaginate fromfastapi_pagination.ext.sqlalchemy module.

fromsqlalchemyimportselectfromfastapi_pagination.ext.sqlalchemyimportpaginate@app.get("/users")defget_users(db:Session=Depends(get_db))->Page[UserOut]:returnpaginate(db,select(User).order_by(User.created_at))

Code fromQuickstart will generate OpenAPI schema as bellow:

app-example

[8]ページ先頭

©2009-2025 Movatter.jp