Micro Services Classroom Series – 10/Dec/2021


  • Lets work on paginating recipe data.
  • In test environments we will have limited data but as the application gets deployed to production, we would have huge volumes of data and when the user asks for recipes returning all of the recipes might not be optimal
  • To solve this problem, the Pagination is introduced.
  • Pagination means instead of querying the whole population of records from database, we just query a handful of them
  • The number of records that are shown per page is limited by page’s size
  • So we need a Paginated API, When you query the API, only the data records on the current page will be returned. It also includes information such as total number of records, total number of pages and links to other pages & so on.
  • The Sample Respose
    "links": {
        "first": "http://localhost:5000/recipes?per_page=2&page=1",
        "last" : "http://localhost:5000/recipes?per_page=2&page=5",
        "prev": "http://localhost:5000/recipes?per_page=2&page=1",
        "next": "http://localhost:5000/recipes?per_page=2&page=3"
    "page": 2,
    "pages": 5,
    "per_page": 2,
    "total": 9,
    "data" : [
            //recipe data
            //recipe data


Implementing pagination

  • To create the pagination we have created a new file in schemas folder pagination.py with Pagination base class implementation
  • Then we have used this PaginationSchema class as a base class in schemas/recipe to create RecipePaginationSchema
  • Then in models/recipe.py we have changed the get_all_published method signature to implement pagination
  • In resources/recipe.py we have made changes in the get method to serialize the response to RecipePaginationSchema.
  • Note: webargs.use_kwargs is not working so for workaround we have directly parsed query parameters
  • Refer Here for the changeset

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin