Package: py-paginator
py-paginatorPaginator to generate page numbers for paginationUsage Example with flask frameworkapp.py:fromflaskimportFlask,request,render_templatefrompy_paginatorimportPaginatorapp=Flask(__name__)app.jinja_env.add_extension('jinja2.ext.do')@app.route('/')defhome():page=int(request.args.get('page',1))limit=20records_count=get_records_count()# get records count from storagepaginator=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 storagereturnrender_template('home.html',records=records,paginator=paginator)Herepaginator.item_limitandpaginator.item_offsetcan be used in database query to apply limit.paginatorobject 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) %}<navaria-label="Page navigation">{% if pager %}
                {% set pager = paginator.get_pager() %}<ulclass="pager"><liclass="previous{% if not paginator.has_prev %} disabled{% endif %}">{% do kwargs.update({'page': pager.prev}) %}<ahref="{% if paginator.has_prev %}{{ url_for(endpoint, **kwargs) }}{% else %}#{% endif %}"><spanaria-hidden="true">&laquo;</span>Prev</a></li><liclass="next{% if not paginator.has_next %} disabled{% endif %}">{% do kwargs.update({'page': pager.next}) %}<ahref="{% if paginator.has_next %}{{ url_for(endpoint, **kwargs) }}{% else %}#{% endif %}">Next<spanaria-hidden="true">&raquo;</span></a></li></ul>{% else %}<ulclass="pagination">{% for page_type, page_num in paginator.get_pages() %}
                        {% do kwargs.update({'page': page_num}) %}
                        {% if page_type == 'prev' %}
                            {% if page_num %}<li><ahref="{{ url_for(endpoint, **kwargs) }}"aria-label="Previous"><spanaria-hidden="true">&laquo;</span></a></li>{% else %}<liclass="disabled"><span><spanaria-hidden="true">&laquo;</span></span></li>{% endif %}
                        {% elif page_type == 'next' %}
                            {% if page_num %}<li><ahref="{{ url_for(endpoint, **kwargs) }}"aria-label="Next"><spanaria-hidden="true">&raquo;</span></a></li>{% else %}<liclass="disabled"><span><spanaria-hidden="true">&raquo;</span></span></li>{% endif %}
                        {% elif page_type == 'ellip' %}<liclass="disabled"><span><spanaria-hidden="true">&hellip;</span></span></li>{% elif page_type == 'curr' %}<liclass="active"><span>{{ page_num }}</span></li>{% else %}<li><ahref="{{ 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) }}