Pagination and Sorting Package

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
from pagination_sorting.pagination import Paginator
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
paginator = Paginator(items, items_per_page=3)
page1_items = paginator.get_page(1)
print(page1_items)
has_previous = paginator.has_previous(2)
print(has_previous)
has_next = paginator.has_next(4)
print(has_next)
page_range = paginator.get_page_range(4, num_pages=5, left_edge=2, right_edge=2)
print(page_range)
page_info = paginator.get_page_info(2)
print(page_info)
page_url = paginator.get_page_url(3, base_url='/products', query_param='pg')
print(page_url)
pagination_html = paginator.render_pagination(2)
print(pagination_html)
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)
page_info = paginator.paginate(2, sort_key=lambda x: -x)
print(page_info['items'])
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)
reverse_sorted_items = sort_items(items, reverse=True)
print(reverse_sorted_items)
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.