PAGINATION USING PYTHON, REST API, FLASK & MONGODB
Tags: python, flask, mongodb, api
Published: 2022-01-19 21:17:53

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dub7wh1in5d4oz5yqc1x.png)

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.

```bash
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**

```python
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**

```python
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

```

**app.py**

```python
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](https://github.com/nodebe/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! :)