Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mardix/paginator.py
Paginator for SQLAlchemy query object, list or iterable
https://github.com/mardix/paginator.py
Last synced: 4 days ago
JSON representation
Paginator for SQLAlchemy query object, list or iterable
- Host: GitHub
- URL: https://github.com/mardix/paginator.py
- Owner: mardix
- License: mit
- Created: 2015-06-14T09:17:27.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-28T03:27:13.000Z (over 7 years ago)
- Last Synced: 2024-10-08T08:52:20.446Z (about 1 month ago)
- Language: Python
- Size: 11.7 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- License: LICENSE
Awesome Lists containing this project
README
# Paginator
Paginator for SQLAlchemy query object, list or iterable
---
## Install
pip install paginator
### Simple Usage with an iterable
from paginator import Paginator
items = range(1, 1000)
items = Paginator(items, page=1, per_page=10)
for item in items:
pass
### Simple Usage with an iterable and callback
from paginator import Paginator
items = range(1, 1000)
def my_callback(item):
# do something
return item
items = Paginator(items, page=1, per_page=10, callback=my_callback)
for item in items:
pass
#### API
**Paginator(query, page=1, per_page=10, total=None, padding=0, callback=None, static_query=False)**
:param query: Iterable to paginate. Can be a query object, list or any iterables
:param page: current page
:param per_page: max number of items per page
:param total: Max number of items. If not provided, it will use the query to count
:param padding: Number of elements of the next page to show
:param callback: a function to callback on each item being iterated.
:param static_query: bool - When True it will return the query as is, without slicing/limit. Usally when using the paginator to just create the pagination.
:return:
### Jinja macro
{#: PAGINATION -------------------------------------------------------------- #}
{#
:paginator: iterator
:endpoint:
:prev: Text for previous button
:next: Text for Next button
:class_: A class name for pagination if customed. If you are extending the class
best to add the original class and your custom class
ie: 'pagination my_custom_pagination' or 'pager my_custom_page'
:pager: If true it will show a pager instead of numbered pagination
#}
{% macro pagination(paginator, endpoint=None, prev="", next="", class_=None, pager=False) %}
{% if not endpoint %}
{% set endpoint = request.endpoint %}
{% endif %}
{% if "page" in kwargs %}
{% set _ = kwargs.pop("page") %}
{% endif %}
{% if not class_ %}
{% set class_ = "pagination" %}
{% if pager %}
{% set class_ = "pager" %}
{% endif %}
{% endif %}
{% set _prev_btn = "← %s" % prev %}
{% set _next_btn = "→ %s" % next %}
-
{{ _prev_btn | safe }}
-
{{ _prev_btn | safe }}
- {{ page }}
- {{ page }}
- …
-
{{ _next_btn | safe }}
-
{{ _next_btn | safe }}
{%- if paginator.has_prev %}
{% else %}
{%- endif %}
{% if not pager %}
{%- for page in paginator.iter_pages() %}
{% if page %}
{% if page != paginator.page %}
{% else %}
{% endif %}
{% else %}
{% endif %}
{%- endfor %}
{% endif %}
{%- if paginator.has_next %}
{% else %}
{%- endif %}
{% endmacro %}
### DOCUMENTATION IN CONSTRUCTION
---
(c) 2015 Mardix