langdev.web.pager — Pager for long length web application

This module provides the pager like following look:

[1]  2  3  4  5  6  7  8  9  10  ...  123
1  2  3  [4]  5  6  7  8  9  10  ...  123
1  ...  53  54  55  56  57  [58]  59  60  61  62  ...  123
1  ...  113  114  115  116  117  118  [119]  120  121  122  123
1  ...  113  114  115  116  117  118  119  120  121  122  [123]

It can be used like following code with Jinja:

<ul class="pager">
  {% for flag, page in pager %}
  <li class="{{ flag is number and '' or flag }}">
    <a href="/?page={{ page }}">{{ page }}</a>
  </li>
  {% endfor %}
</ul>
class langdev.web.pager.Pager(length, selected_page=1, width=10)

Pager class that is iterable. Pager instances can be looped with for statements.

The length represents a maximum number of pages.

>>> list(Pager(7))
[('selected', 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7)]

The selected_page represents a currently selected page number. Its default value is just 1.

>>> list(Pager(7, selected_page=5))
[(1, 1), (2, 2), (3, 3), (4, 4), ('selected', 5), (6, 6), (7, 7)]
>>> list(Pager(7, selected_page=7))
[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), ('selected', 7)]

Last, the width represents how many page numbers are shown at once. First and last page numbers are excluded in the width.

>>> list(Pager(8, 4, width=5))
[(1, 1), (2, 2), (3, 3), ('selected', 4), (5, 5), ('last', 8)]

The form of sequence is [(flag_or_page, page), ...]. In (flag, page) pair, the flag can be Pager.FIRST ("first"), Page.LAST ("last"), Page.SELECTED ("selected") or ordinal page number.

>>> list(Pager(100, 100, width=5))  
[('first', 1), (96, 96), (97, 97), (98, 98), (99, 99),
 ('selected', 100)]
>>> list(Pager(100, 1, width=5))
[('selected', 1), (2, 2), (3, 3), (4, 4), (5, 5), ('last', 100)]
>>> list(Pager(100, 50, width=5))  
[('first', 1), (48, 48), (49, 49), ('selected', 50), (51, 51),
 (52, 52), ('last', 100)]

Pager is iterable, so it can be used in for loop.

>>> for typ, page in Pager(100, 50, width=5):
...   if typ == Pager.LAST:
...     print "...",
...   if typ == Pager.SELECTED:
...     print "[{0}]".format(page),
...   else:
...     print page,
...   if typ == Pager.FIRST:
...     print "...",
1 ... 48 49 [50] 51 52 ... 100
Parameters:
  • length (int, long) – total length of pages
  • selected_page (int, long) – currently selected page number. default value is 1
  • width (int, long) – pager’s width. default value is DEFAULT_WIDTH
DEFAULT_WIDTH

Default width which is exactly 10.

FIRST

Flag value which is exactly "first" for first page.

LAST

Flag value which is exactly "last" for last page.

SELECTED

Flag value which is exactly "selected" for selected page.

langdev.web.pager.pager

alias of Pager

Project Versions

Previous topic

langdev.web.thirdparty — Third-party applications

Next topic

langdev.web.serializers — Serializers for various content types

This Page