add_pagination is a function that allows to add pagination support to a FastAPI application.
It accepts a FastAPI application instance as an argument and returns the same instance with pagination support.
It will modify all routes that return a Page instance to return a paginated response.
fromfastapiimportFastAPIfromfastapi_pagination.apiimportadd_paginationapp=FastAPI()add_pagination(app)# that's all folks!
pagination_ctx is a function that allows to set a page context for route, it accepts page type, params type and
items transformer as arguments.
fromfastapiimportFastAPI,Dependsfromfastapi_paginationimportPage,Params,paginatefromfastapi_pagination.apiimportpagination_ctxapp=FastAPI()# req: GET /users?page=2&size=10@app.get("/users",dependencies=[Depends(pagination_ctx(page=Page[int],params=Params,transformer=lambdaitems:[x*2forxinitems],),),])asyncdefget_users():returnpaginate(range(100))
set_page is a function that allows to set a page type for pagination.
It also can be used as a context manager to set a page type for a specific context.
fromfastapi_paginationimportPagefromfastapi_pagination.apiimportset_page# set global page typeset_page(Page[int])# set page type for a specific contextwithset_page(Page[str]):pass
create_page is a function that allows to create a new page instance. It will take current page type from set_page
function or use Page class as a default page type. create_page function accepts 1 required argument items,
1 required keyword argument params and 1 optional keyword argument total.
pagination_items is a function that allows to get current pagination items. It can be useful when you need to get
current items in a specific context. Here is an example of how it can be used:
from__future__importannotationsfromtypingimportTypeVar,Generic,Sequence,Optional,AnyfromfastapiimportFastAPIfromfastapi_paginationimportParams,add_pagination,paginatefromfastapi_pagination.basesimportAbstractParams,AbstractPagefromfastapi_pagination.apiimportpagination_itemsfrompydanticimportBaseModel,FieldT=TypeVar("T")classInnerModel(BaseModel,Generic[T]):results:list[T]=Field(default_factory=pagination_items)classMyPageResponse(AbstractPage[T]):inner:InnerModel[T]__params_type__=Params@classmethoddefcreate(cls,items:Sequence[T],params:AbstractParams,*,total:Optional[int]=None,**kwargs:Any,)->MyPageResponse[T]:returncls(inner={})app=FastAPI()add_pagination(app)# req: GET /nums?page=2&size=10@app.get("/nums")asyncdefroute()->MyPageResponse[int]:returnpaginate([*range(100)])
set_params is a function that allows to set current pagination params.
It also can be used as a context manager to set params for a specific context.
fromfastapi_paginationimportParamsfromfastapi_pagination.apiimportset_params# set global paramsset_params(Params(page=2,size=10))# set params for a specific contextwithset_params(Params(page=3,size=20)):pass
resolve_params is a function that allows to get current pagination params.
In case if set_params was called before, then resolve_params will return the same params as set_params was called with.
If resolve_params was called with param params argument, then this argument will be returned.
response and request are functions that allow to get current response and request objects.
It can be useful when you need to add information some information to response or get information about request.
fromfastapiimportFastAPIfromfastapi_paginationimportPage,add_pagination,paginatefromfastapi_pagination.apiimportresponse,requestapp=FastAPI()add_pagination(app)# req: GET /nums?page=2&size=10@app.get("/nums")asyncdefget_nums()->Page[int]:print({**request().headers})response().status_code=201returnpaginate(range(100))