DEV Community

Cover image for PAGINATION USING PYTHON, REST API, FLASK & MONGODB
Metete Welete
Metete Welete

Posted on

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 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:

file structure of api

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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']

        # 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

Enter fullscreen mode Exit fullscreen mode

app.py

from books_api import app

if __name__ == __main__:
    app.run()
Enter fullscreen mode Exit fullscreen mode

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! :)

Top comments (2)

Collapse
 
marybngozi profile image
MaryBlessing

Simple and straight, a nice write-up

Collapse
 
metete_welete profile image
Metete Welete

Thank you