py-paginator
Paginator to generate page numbers for pagination
Usage Example with flask framework
- app.py:
from flask import Flask, request, render_template
from py_paginator import Paginator
app = Flask(__name__)
app.jinja_env.add_extension('jinja2.ext.do')
@app.route('/')
def home():
page = int(request.args.get('page', 1))
limit = 20
records_count = get_records_count() # get records count from storage
paginator = Paginator(total_items=records_count, item_limit=limit, curr_page=page)
records = get_records(limit=paginator.item_limit, offset=paginator.item_offset) # get records from storage
return render_template('home.html', records=records, paginator=paginator)
Here paginator.item_limit and paginator.item_offset can be used in database query to apply limit. paginator object can be used in templates to create pagination links.
- templates/macros.html:
{#
:paginator: Paginator object
:endpoint: flask request endpoint
:pager: If True it will show a pager instead of numbered pagination
- you can also pass further arguments that will be passed into `url_for()` of every link.
#}
{% macro render_pagination(paginator, endpoint=request.endpoint, pager=False) %}
{% if paginator.has_pages %}
{% do kwargs.update(request.args) %}
{% do kwargs.pop('page', None) %}
<nav aria-label="Page navigation">
{% if pager %}
{% set pager = paginator.get_pager() %}
<ul class="pager">
<li class="previous{% if not paginator.has_prev %} disabled{% endif %}">
{% do kwargs.update({'page': pager.prev}) %}
<a href="{% if paginator.has_prev %}{{ url_for(endpoint, **kwargs) }}{% else %}#{% endif %}"><span aria-hidden="true">«</span> Prev</a>
</li>
<li class="next{% if not paginator.has_next %} disabled{% endif %}">
{% do kwargs.update({'page': pager.next}) %}
<a href="{% if paginator.has_next %}{{ url_for(endpoint, **kwargs) }}{% else %}#{% endif %}">Next <span aria-hidden="true">»</span></a>
</li>
</ul>
{% else %}
<ul class="pagination">
{% for page_type, page_num in paginator.get_pages() %}
{% do kwargs.update({'page': page_num}) %}
{% if page_type == 'prev' %}
{% if page_num %}
<li><a href="{{ url_for(endpoint, **kwargs) }}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>
{% else %}
<li class="disabled"><span><span aria-hidden="true">«</span></span></li>
{% endif %}
{% elif page_type == 'next' %}
{% if page_num %}
<li><a href="{{ url_for(endpoint, **kwargs) }}" aria-label="Next"><span aria-hidden="true">»</span></a></li>
{% else %}
<li class="disabled"><span><span aria-hidden="true">»</span></span></li>
{% endif %}
{% elif page_type == 'ellip' %}
<li class="disabled"><span><span aria-hidden="true">…</span></span></li>
{% elif page_type == 'curr' %}
<li class="active"><span>{{ page_num }}</span></li>
{% else %}
<li><a href="{{ url_for(endpoint, **kwargs) }}">{{ page_num }}</a></li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
</nav>
{% endif %}
{% endmacro %}
- templates/home.html:
{% from "macros.html" import render_pagination with context %}
<h1>Total Records: {{ paginator.total_items }}</h1>
<h2>Total Pages: {{ paginator.total_pages }}</h2>
{% for record in records %}
<p>{{ loop.index + paginator.item_offset }} - {{ record.field_name }}</p>
{% else %}
<p>No Records found.</p>
{% endfor %}
{{ render_pagination(paginator) }}
Metadata
Release files for py-paginator 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py_paginator-1.0.2.tar.gz | 5.3 kB | Without provenance | Details for py_paginator-1.0.2.tar.gz |
Release files / py_paginator-1.0.2.tar.gz
| Download URL | py_paginator-1.0.2.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3206b7e9c4c8ac877ea2b9ee16915c32784a8e0f5d2ced3e762cd3419ce714e5
|
|
BLAKE2b-256 checksum How to use checksums |
f6d7ac3e73338d4843790b590d9208da313ae84a20155c6dac78419d242de490
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7
|
Security for py-paginator 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.