PAGINATION USING PYTHON, REST API, FLASK & MONGODB

I was recently involved in a project that required me to implement pagination from the backend and I would like to share the easy way I did it through this post.
Now just like I initially did, you might have checked online for a python-mongo module tha…

I was recently involved in a project that required me to implement pagination from the backend and I would like to share the easy way I did it through this post.
Now just like I initially did, you might have checked online for a python-mongo module that could help achieve this but I assure you that this article would be easier and more light-weight.

For this post, I will be using a simple books API that returns a passed in number of books requested from the database. We will be able to pass in the page number and amount(limit) of books to be returned.

Please note that this article was written with Windows users in mind. If you are on another OS, you could check out the commands to perform similar tasks online.

File Structure

First, let me describe how my file structure is:

-> Books_api
----> app.py
----> venv
----> books_api
--------> \_\_init\_\_.py
--------> books.py

I have a venv (virtual environment) folder where all modules required for this project are installed. You can quickly do that by following these steps:

  1. Open your terminal (command prompt) in the books_api folder,

  2. If you don’t already have the virtual environment package installed on your pc, you should follow step 3, else you can skip to step 4,

  3. run pip install virtualenv. This should install packages required for creating a virtual environment,

  4. run virtualenv venv, where venv is the name of the virtual environment. This can be named anything, but we like to keep it simple,

  5. run venv\scripts\activate to activate the virtual environment in the books_api folder.

Installations
Just before we kick off, if you don’t already have these modules, then you can quickly install them using pip install in your already activated virtual environment.

pip install flask flask-restful pymongo

Setting up our Resources
I am going to be setting up Resource for flask-restful in the __init__.py file.

__init__.py

from flask import Flask
from pymongo import MongoClient
from flask_restful import Api

app = Flask(__name__)
api = Api(app)

# Connecting to local host 
client = MongoClient('mongodb://localhost:27017/')

# Get the books_db and store in db
db = client.books_db

from .books import Books

api.add_resource(Books, /books)

books.py

from flask_restful import Resource, reqparse
from . import db
from bson import json_util
import json

books_data = reqparse.RequestParser()
books_data.add_argument(page, type=int, required=True, help=Page number is required)
books_data.add_argument(limit, type=int, required=True, help=Limit is required)

class Books(Resource):
    '''
        Methods related to the Books endpoint are written 
        here.

    GET:
       Input: {'page':Int, 'limit':Int}
       Output: {'total_number':Int, 'page':Int, 
       'showing':Int, books:List}
    '''

    def get(self):
        data = books_data.parse_args()

        page = data['page']
        page_limit = data['limit']

        # Fetching books data
        fetch_all_books = db.books.find()

        # Total number of books
        books_count = fetch_all_books.count()

        # Total number of books
        books_count = db.books.count_documents({})

        # Fetching books data and paginating
        fetch_books = db.books.find().sort('_id', -1).skip(page_limit * (page - 1)).limit(page_limit)

        books_fetched = list(json.loads(json_util.dumps(fetch_books)))

        response = {'total_number': books_count, 'page': page, 'showing': page_limit, 'books': books_fetched}

        return response

app.py

from books_api import app

if __name__ == __main__:
    app.run()

To run this application, open the activated venv in your terminal and run python app.py

Conclusion
This post has been made very basic for easy understanding and implementation. You don’t really need to have this exact project. You just need to understand the concept and you’re good to go. All the code has been uploaded to GitHub, you can view it here:

Books_Api

Hope I was able to help you through this post. I am open to answering questions in the comment section below.

Stay coded! 🙂


Print Share Comment Cite Upload Translate
APA
Metete Welete | Sciencx (2024-03-28T19:21:08+00:00) » PAGINATION USING PYTHON, REST API, FLASK & MONGODB. Retrieved from https://www.scien.cx/2022/01/19/pagination-using-python-rest-api-flask-mongodb/.
MLA
" » PAGINATION USING PYTHON, REST API, FLASK & MONGODB." Metete Welete | Sciencx - Wednesday January 19, 2022, https://www.scien.cx/2022/01/19/pagination-using-python-rest-api-flask-mongodb/
HARVARD
Metete Welete | Sciencx Wednesday January 19, 2022 » PAGINATION USING PYTHON, REST API, FLASK & MONGODB., viewed 2024-03-28T19:21:08+00:00,<https://www.scien.cx/2022/01/19/pagination-using-python-rest-api-flask-mongodb/>
VANCOUVER
Metete Welete | Sciencx - » PAGINATION USING PYTHON, REST API, FLASK & MONGODB. [Internet]. [Accessed 2024-03-28T19:21:08+00:00]. Available from: https://www.scien.cx/2022/01/19/pagination-using-python-rest-api-flask-mongodb/
CHICAGO
" » PAGINATION USING PYTHON, REST API, FLASK & MONGODB." Metete Welete | Sciencx - Accessed 2024-03-28T19:21:08+00:00. https://www.scien.cx/2022/01/19/pagination-using-python-rest-api-flask-mongodb/
IEEE
" » PAGINATION USING PYTHON, REST API, FLASK & MONGODB." Metete Welete | Sciencx [Online]. Available: https://www.scien.cx/2022/01/19/pagination-using-python-rest-api-flask-mongodb/. [Accessed: 2024-03-28T19:21:08+00:00]
rf:citation
» PAGINATION USING PYTHON, REST API, FLASK & MONGODB | Metete Welete | Sciencx | https://www.scien.cx/2022/01/19/pagination-using-python-rest-api-flask-mongodb/ | 2024-03-28T19:21:08+00:00
https://github.com/addpipe/simple-recorderjs-demo