In this post I will show you how to set up API pagination in Flask, thanks primarily to Flask-SqlAlchemy’s paginate method.
That’s it! The paginate method on our sqlalchemy query is giving us a lot of flexibility with very little code, returning a limited result set, displaying total records available, while also calculating remaining pages based on our per_page setting.
Structuring our API this way we allows us to manage performance and return small chunks of data at a time, while allowing other systems to page through all the records if needed.
The pagination format returned in JSON is a simplified version of the White House API standards. While the White House prefers offset and limit for pagination, I think page and per_page is more intuitive.