Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fengsp/pypages

Simple Python Pagination
https://github.com/fengsp/pypages

Last synced: about 2 months ago
JSON representation

Simple Python Pagination

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 "

    "
    print "
  • first
  • "
    for page in p.pages:
    if page == p.current:
    print "
  • %s
  • " % page
    else:
    print "
  • %s
  • " % (page, page)
    print "
  • last
  • " % p.page_num
    print "
"

def render(p):
print "Your objects:"
for o in p.objects:
print o
print "Your pages:"
print "

    "
    if p.has_previous:
    print "
  • previous
  • " % p.previous
    print "
  • %s/%s
  • " % (p.current, p.page_num)
    if p.next: # pythonic, of course you can check p.has_next too
    print "
  • next
  • " % p.next
    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`