Paginate

0.5.7 · active · verified Mon Apr 06

Paginate is a Python module that helps divide large lists of items into pages for easier browsing. It's a standalone library, previously maintained as `webhelpers.paginate`, and aims to be framework-agnostic. The current version is 0.5.7, last updated in August 2024, indicating active maintenance for its core functionality.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to paginate a simple list using the `Page` class. It shows how to initialize `Page` with a collection, current page, and items per page, and how to define a `url_maker` function. It then accesses the items for the current page and generates basic pagination links.

from paginate import Page

# A list of items to paginate
all_items = list(range(1, 101)) # 100 items

# Simulate current page number (e.g., from a URL query parameter)
current_page_number = 3
items_per_page = 10

# Define a URL maker function for pagination links
def my_url_maker(page_num):
    return f"/items?page={page_num}"

# Create a Page object
page = Page(
    all_items,
    page=current_page_number,
    items_per_page=items_per_page,
    url_maker=my_url_maker
)

# Access items for the current page
print(f"Items on page {page.page}: {page.items}")
# Expected output for page 3 (items_per_page=10): [21, 22, ..., 30]

# Get page information
print(f"Total items: {page.item_count}")
print(f"Total pages: {page.page_count}")
print(f"Has previous page: {page.previous_page is not None}")
print(f"Has next page: {page.next_page is not None}")

# Generate a simple pager string (HTML representation)
# For full customization, use page.link_map() with your templating engine.
pager_html = page.pager(
    link_attr={'class':'pager_link'},
    curpage_attr={'class':'pager_curpage'},
    dotdot_attr={'class':'pager_dotdot'}
)
print("\nGenerated Pager (HTML, example output depends on page numbers):\n")
print(pager_html)

# Example of using link_map for custom rendering
print("\nLink Map for custom rendering (partial output):")
link_info = page.link_map()
for k, v in list(link_info.items())[:3]: # Show first 3 for brevity
    print(f"  {k}: {v}")

view raw JSON →