Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fengsp/pypages
Simple Python Pagination
https://github.com/fengsp/pypages
Last synced: about 2 months ago
JSON representation
Simple Python Pagination
- Host: GitHub
- URL: https://github.com/fengsp/pypages
- Owner: fengsp
- License: other
- Created: 2014-05-07T02:58:36.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-06-25T04:44:17.000Z (over 10 years ago)
- Last Synced: 2024-10-29T00:43:49.482Z (about 2 months ago)
- Language: Python
- Homepage:
- Size: 211 KB
- Stars: 9
- Watchers: 4
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES
- License: LICENSE
Awesome Lists containing this project
README
##PyPages: Easier Pagination
**Do pagination in python like a charm.**##install
pip install PyPages
##usage
```python
from pypages import Paginator# Inside view func
def view():
# get current page
page = 3
# retrieve the objects of the certain page
# you should handle your data pagination yourself(database query or other ways)
# make sure use the same per_page value and pass in the right objects total number
objects = range(10)
p = Paginator(100, per_page=10, current=page, range_num=5)
# you can attach your objects to the paginator
p.objects = objects
return render_all(p=p)
def render_all(p):
print "Your objects:"
for o in p.objects:
print o
print "Your pages:"
print "
- "
- first "
- %s " % page
- %s " % (page, page)
- last " % p.page_num
print "
for page in p.pages:
if page == p.current:
print "
else:
print "
print "
print "
def render(p):
print "Your objects:"
for o in p.objects:
print o
print "Your pages:"
print "
- "
- previous " % p.previous
- %s/%s " % (p.current, p.page_num)
- next " % p.next
if p.has_previous:
print "
print "
if p.next: # pythonic, of course you can check p.has_next too
print "
print "
```
##api
*class* pypages.**Paginator**(*object_num, per_page=10, current=1, start=None, range_num=10*)
***Parameters:***
* **object_num** – the total number of items
* **per_page** – the maximum number of items to include on a page, default 10
* **current** – the current page number, default 1
* **start** – the start index for your page range, default to be current page minus half of the page range length
* **range_num** – the maximum page range length, default 10
NOTICE: **page range** is the pages that will be displayed, like you have one page "5, 6, 7, 8, 9, 10", then your page range ``start`` is 5 and ``range_num`` is 6.
***Attributes:***
* **object_num** - the total number of items
* **per_page** – the maximum number of items to include on a page
* **current** – the current page number
* **start** – the start index for your page range
* **range_num** – the maximum page range length
* **end** - the end index for your page range
* **page_num** - the total number of pages
* **pages** - the page range, a list like `[4, 5, 6, 7, 8]`
* **has_previous** - bool value to indicate whether current page have previous page
* **has_next** - bool value to indicate whether current page have next page
* **previous** - the previous page number, if do not exist, will be `None`
* **next** - the next page number, if do not exist, will be `None`