FastAPI cursor based pagination example

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI

def get_all_with_pagination(*, dB: Session = Depends(database.get_db), page_number: int=1, page_size: int = 70):
  return engine_service.get_with_pagination(
 dB, page_number, page_size
)

Description

I would like to have an example of how to implement a cursor based pagination in FastAPI. Any help would be very welcome. I managed to implement offset based pagination like the one shown in the docs but I read on the internet that offset based paginations are inefficient. Then I would like to see how to do cursor based ones in FastAPI + Sqlalchemy + PostgreSQL. The frontend is using datatable.net, how to put everything together to have a full working pagination system. Thank you for your help in advance

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.75.0

Python Version

3.10.4

Additional Context

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
shellking4commented, Jun 8, 2022

We use the offset based pagination:

https://github.com/tiangolo/full-stack-fastapi-postgresql/blob/490c554e23343eec0736b06e59b2108fdd057fdc/{{cookiecutter.project_slug}}/backend/app/app/crud/base.py#L29

Satisfied about performance etc… (even extended it with filters/search)

Thank you very much sir. I think I’ll go for the offset one for my current project. Thank you for your reply

1reaction
acidjunkcommented, Jun 8, 2022

We use the offset based pagination:

https://github.com/tiangolo/full-stack-fastapi-postgresql/blob/490c554e23343eec0736b06e59b2108fdd057fdc/{{cookiecutter.project_slug}}/backend/app/app/crud/base.py#L29

Satisfied about performance etc… (even extended it with filters/search)

Read more comments on GitHub >

github_iconTop Results From Across the Web

fastapi-pagination-utilities - PyPI
This Python package contains utilities to aid in paginating responses from FastAPI applications.
Read more >
Build A Pagination API with FastAPI and Python (under 10 ...
Hey people!Today, I will show you how to build a simple and super fast Pagination API using FastAPI and Python. This is my...
Read more >
How to Implement Cursor Pagination Like a Pro - Medium
But first, let's discuss the ideal scenario. Cursor Implementation. If offset pagination is an array, then cursor pagination is a linked list.
Read more >
API Paging Built The Right Way - Mixmax
The right approach: Cursor-based paging ... The best way to build API pagination in a safe way is for the API to return...
Read more >
Evolving API Pagination at Slack
To handle this rapid growth, we've had to rethink how we paginate data — from no pagination, to offset pagination, to a new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found