Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate x number of pages to display out of the total number of pages and current page number
def get_pages_range(total_pages_number, display_pages_number,
current_page_number):
"""
Generate x number of pages to display out of the total number of pages and
current page number.
:param int total_pages_number: Total number of pages.
:param int display_pages_number: Number of pages to display.
:param int current_page_number: Current page number.
:return iterable:
"""
lower_limit = upper_limit = min(current_page_number, total_pages_number)
i = 1
while i < display_pages_number and i < total_pages_number:
if lower_limit > 1:
lower_limit = lower_limit - 1
i = i + 1
if i < display_pages_number and upper_limit < total_pages_number:
upper_limit = upper_limit + 1
i = i + 1
return range(lower_limit, upper_limit + 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment