This content originally appeared on DEV Community and was authored by Metete Welete
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:
Open your terminal (command prompt) in the books_api folder,
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,
run
pip install virtualenv
. This should install packages required for creating a virtual environment,run
virtualenv venv
, wherevenv
is the name of the virtual environment. This can be named anything, but we like to keep it simple,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:
Hope I was able to help you through this post. I am open to answering questions in the comment section below.
Stay coded! :)
This content originally appeared on DEV Community and was authored by Metete Welete
Metete Welete | Sciencx (2022-01-19T21:17:53+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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.