Skip to main content

A powerful and flexible pagination and sorting package for arrays and lists.

Project description

Pagination and Sorting Package

PyPI Version License Python Versions

A powerful and flexible pagination and sorting package for arrays and lists, compatible with Django and Flask-based apps. It provides a wide range of features to simplify pagination and sorting in your Python projects.

Features

  • Paginate lists or arrays of any type
  • Customize the number of items per page and handle orphaned items
  • Support for multiple pagination styles:
    • Traditional page-based pagination
    • Infinite scrolling
    • Load more button
  • Generate page URLs with customizable query parameters
  • Render pagination controls as HTML with default or custom templates
  • Integrate sorting functionality with pagination
  • Comprehensive pagination information, including page range, previous/next page URLs, and more
  • Extensive documentation and usage examples

Installation

You can install the package using pip:

pip install pagination-sorting

Usage

Pagination

from pagination_sorting.pagination import Paginator

items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
paginator = Paginator(items, items_per_page=3)

# Get paginated items
page1_items = paginator.get_page(1)
print(page1_items)  # Output: [1, 2, 3]

# Check if there are previous or next pages
has_previous = paginator.has_previous(2)
print(has_previous)  # Output: True

has_next = paginator.has_next(4)
print(has_next)  # Output: False

# Get page range with ellipsis
page_range = paginator.get_page_range(4, num_pages=5, left_edge=2, right_edge=2)
print(page_range)  # Output: [1, '...', 3, 4, 5, '...', 10]

# Get page information
page_info = paginator.get_page_info(2)
print(page_info)
# Output:
# {
#     'current_page': 2,
#     'has_previous': True,
#     'has_next': True,
#     'total_pages': 4,
#     'total_items': 10,
#     'items_per_page': 3,
#     'items': [4, 5, 6],
#     'start_index': 4,
#     'end_index': 6,
#     'previous_page_url': '?page=1',
#     'next_page_url': '?page=3'
# }

# Generate page URL
page_url = paginator.get_page_url(3, base_url='/products', query_param='pg')
print(page_url)  # Output: /products?pg=3

# Render pagination controls
pagination_html = paginator.render_pagination(2)
print(pagination_html)
# Output:
# <ul class="pagination">
#     <li><a href="?page=1">&laquo; Previous</a></li>
#     <li><a href="?page=1">1</a></li>
#     <li class="active"><span>2</span></li>
#     <li><a href="?page=3">3</a></li>
#     <li><a href="?page=4">4</a></li>
#     <li><a href="?page=3">Next &raquo;</a></li>
# </ul>

# Set custom pagination template
custom_template = '<div class="custom-pagination">{page_info["current_page"]} of {page_info["total_pages"]}</div>'
paginator.set_custom_template(custom_template)
rendered_html = paginator.render_pagination(2)
print(rendered_html)  # Output: <div class="custom-pagination">2 of 4</div>

# Paginate with sorting
page_info = paginator.paginate(2, sort_key=lambda x: -x)
print(page_info['items'])  # Output: [7, 6, 5]

Sorting

from pagination_sorting.sorting import sort_items

items = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_items = sort_items(items)
print(sorted_items)  # Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

reverse_sorted_items = sort_items(items, reverse=True)
print(reverse_sorted_items)  # Output: [9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]

Testing

To run the tests, use the following command:

python -m unittest discover tests

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.

License

This package is licensed under the MIT License. See the LICENSE file for more details.

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

pagination_sorting-0.1.1.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

pagination_sorting-0.1.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file pagination_sorting-0.1.1.tar.gz.

File metadata

  • Download URL: pagination_sorting-0.1.1.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for pagination_sorting-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a56d972d8b40ed503771cac46babf9d584b2ab662a4ce91410ef4adddf58f62c
MD5 cbacc1f8b43d5d4484c5298feab0271b
BLAKE2b-256 1f9e2f842f80bcc6df2d4661cf957aa0bad5398c31dc296cd8742c198c1b4eaa

See more details on using hashes here.

File details

Details for the file pagination_sorting-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pagination_sorting-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a13dd9c573dcd347eaafb04a7f2a6aad5134a3836202b8e84f44c7f8003d823a
MD5 2463107f1d065ae28294f06862e4ce96
BLAKE2b-256 16b053c3f53556e3923f3a89befe537b26a14a9dc487b03170f51a103f225220

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