Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
A powerful and flexible pagination and sorting package for arrays and lists.
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.
You can install the package using pip:
pip install pagination-sorting
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">« 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 »</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]
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]
To run the tests, use the following command:
python -m unittest discover tests
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.
This package is licensed under the MIT License. See the LICENSE file for more details.
FAQs
A powerful and flexible pagination and sorting package for arrays and lists.
We found that pagination-sorting demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.