# Python - Pagination class

I built a small flask app and a lot of routes do need some kind of pagination system. However, in some cases I would like to just display n-2, n-1, n, n+1, n+2 where n is the current page and in some others cases, probably a bigger range. Fo example, in the following picture, the range is 2. I am probably going to merge increase() and decrease() in a single method.

I am not doing any database call here, I just want to perform pagination calculation and return values in a form of a dictionary in order to be used by different flask modules.

Would appreciate a feedback! I am quite new to this industry

import math

class Pagination():
def __init__(self, total_items, items_per_page, page_range, current_page=0):
self.total_items = total_items  # total number of article to display in total
self.items_per_page = items_per_page  # maximum number of article per page
self.page_range = page_range  # number of previous and next pages to display
self.current_page = current_page
self.number_of_pages = math.ceil(
self.total_items / self.items_per_page)

# if the current_page is 7 and page_range is 3 then self.following will be [8, 9, 10] and self.previous [4, 5 ,6]
self.following = list(
range(self.current_page + 1, self.current_page + self.page_range + 1))

# empty because by default the current page is 0 so no previous
self.previous = list()

def increase(self, quantity):
if self.current_page + quantity > self.number_of_pages:
raise ValueError(
f'The maximum number of pages is {self.number_of_pages}')
else:
self.current_page += quantity
self.following = list()
self.previous = list()
for i in range(1, self.page_range + 1):
new_following_page_number = i + self.current_page
new_previous_page_number = self.current_page - i
self.previous.insert(i, new_previous_page_number)

# make sure the number does not exceed the number of pages
if new_following_page_number < self.number_of_pages:
self.following.insert(i, new_following_page_number)

def decrease(self, quantity):
if self.current_page - quantity < self.number_of_pages:
raise ValueError(f'The mimimum number of pages is 0')
else:
self.current_page -= quantity
self.following = list()
self.previous = list()
for i in range(1, self.page_range + 1):
new_following_page_number = i + self.current_page
new_previous_page_number = self.current_page - i
self.following.insert(i, new_following_page_number)
if new_previous_page_number > 0:
self.previous.insert(i, new_previous_page_number)

# method called to set the MongoDB skip amount
def number_of_previous_items(self):
if self.current_page == 0:
return 0
return self.items_per_page * self.current_page

# method called to inject variables in the pagination HTML nav
def dict(self):
return {
"current_page": self.current_page,
"next_page": self.following,
"previous_page": self.previous,
"number_of_pages": self.number_of_pages
}


tmp.py

items_pagination = Pagination(total_items=150, items_per_page=10, page_range=3)
print(render.dict())
render.increase(4)
print(render.dict())


results

{'current_page': 0, 'next_page': [1, 2, 3], 'previous_page': [], 'number_of_pages': 15}
{'current_page': 4, 'next_page': [5, 6, 7], 'previous_page': [3, 2, 1], 'number_of_pages': 15}