Skip to main content

Cursor and offset pagination utilities for any data source.

Project description

philiprehberger-pagination

Tests PyPI version Last updated

Cursor and offset pagination utilities for any data source.

Installation

pip install philiprehberger-pagination

Usage

from philiprehberger_pagination import paginate

items = list(range(100))
page = paginate(items, page=2, per_page=25)

print(page.items)     # [25, 26, ..., 49]
print(page.pages)     # 4
print(page.has_next)  # True

Cursor Pagination

from philiprehberger_pagination import CursorPage

rows = [{"id": 1, "name": "a"}, {"id": 2, "name": "b"}, {"id": 3, "name": "c"}]
page = CursorPage.encode(rows, key="id", limit=2)

print(page.items)       # first 2 rows
print(page.has_more)    # True
print(page.next_cursor) # base64-encoded cursor

decoded = CursorPage.decode(page.next_cursor)
print(decoded)  # {"id": 2}

API

Function / Class Description
paginate(items, page, per_page) Offset-paginate a list and return a Page
Page.items Items on the current page
Page.total Total number of items
Page.page Current page number (1-based)
Page.per_page Items per page
Page.pages Total number of pages
Page.has_next Whether a next page exists
Page.has_prev Whether a previous page exists
CursorPage.encode(items, key, limit) Build a cursor page from a list of items
CursorPage.decode(cursor) Decode a cursor string back to a dict
CursorPage.items Items on the current page
CursorPage.next_cursor Encoded cursor for the next page
CursorPage.has_more Whether more items exist

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_pagination-0.1.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_pagination-0.1.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_pagination-0.1.2.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_pagination-0.1.2.tar.gz
Algorithm Hash digest
SHA256 92d65e204db589c2eb80aa9ae7161c99a88db00dd2629ce6baf0623e5ef23464
MD5 4a2637f6d718c9b0bee684b2317d3957
BLAKE2b-256 d92d8fa324ae10e7e6303975aeda6ea951b6add58b7b65c00c0c95f54cf3294e

See more details on using hashes here.

File details

Details for the file philiprehberger_pagination-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_pagination-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 99f889c8b8d973541dfff0de656429dc6fe5c58d8bbd2ad951b7c40e0a087b8e
MD5 1cf8c9e0f32228eafc23d67da69f914f
BLAKE2b-256 d79a371adac4a606c951eea77f94a5a9fc6bf74af0c4abdad9d878b6457b63d3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page