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.2.0.tar.gz (5.8 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.2.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_pagination-0.2.0.tar.gz
Algorithm Hash digest
SHA256 dc4a1a3c2186bced5c4af6a1842275d5ac37812b337ba5c3c36ee95fd33e0866
MD5 6000a8527e0f40b80ec4e55ceebccb59
BLAKE2b-256 fad6f7a19109b3c62c1f3d53dbb51eaa64762c2b35ddd5ef8511774f613c5909

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_pagination-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e67d7c9088acd7a701d2faeb9f89805b23f864a045fe4ed54e56c026da8a074a
MD5 6ed6c53d7f70157792961fe011ef3f80
BLAKE2b-256 4f8cd106cd61f355aaea1b27b7091a8e0382d02ca7d42f437dcfff298e4f0b79

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