https://github.com/sujeetkv/py-paginator
Paginator to generate page numbers for pagination
https://github.com/sujeetkv/py-paginator
pagination paginator python python-libary python-utility
Last synced: 9 days ago
JSON representation
Paginator to generate page numbers for pagination
- Host: GitHub
- URL: https://github.com/sujeetkv/py-paginator
- Owner: sujeetkv
- License: mit
- Created: 2018-08-16T12:55:45.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-12-18T12:54:11.000Z (over 7 years ago)
- Last Synced: 2026-01-04T00:12:50.704Z (5 months ago)
- Topics: pagination, paginator, python, python-libary, python-utility
- Language: Python
- Size: 26.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# py-paginator
Paginator to generate page numbers for pagination
[](https://travis-ci.org/sujeetkv/py-paginator)
[](https://pypi.org/project/py-paginator)
## Installation
```bash
pip install py-paginator
```
## Usage Example with flask framework
- **app.py**
```python
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**
```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) %}
{% if pager %}
{% set pager = paginator.get_pager() %}
-
{% do kwargs.update({'page': pager.prev}) %}
« Prev
-
{% do kwargs.update({'page': pager.next}) %}
Next »
{% else %}
- «
- «
- »
- »
- …
- {{ page_num }}
- {{ page_num }}
{% for page_type, page_num in paginator.get_pages() %}
{% do kwargs.update({'page': page_num}) %}
{% if page_type == 'prev' %}
{% if page_num %}
{% else %}
{% endif %}
{% elif page_type == 'next' %}
{% if page_num %}
{% else %}
{% endif %}
{% elif page_type == 'ellip' %}
{% elif page_type == 'curr' %}
{% else %}
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
{% endmacro %}
```
- **templates/home.html**
```html
{% from "macros.html" import render_pagination with context %}
Total Records: {{ paginator.total_items }}
Total Pages: {{ paginator.total_pages }}
{% for record in records %}
{{ loop.index + paginator.item_offset }} - {{ record.field_name }}
{% else %}
No Records found.
{% endfor %}
{{ render_pagination(paginator) }}
```