watson.db.utils

class watson.db.utils.Pagination(query, page=1, limit=20)[source]

Provides simple pagination for query results.

query

Query

The SQLAlchemy query to be paginated

page

int

The page to be displayed

limit

int

The maximum number of results to be displayed on a page

total

int

The total number of results

items

list

The items returned from the query

Example:

# within controller
query = session.query(Model)
paginator = Pagination(query, limit=50)

# within view
{% for item in paginator %}
{% endfor %}
<div class="pagination">
{% for page in paginator.iter_pages() %}
    {% if page == paginator.page %}
    <a href="#" class="current">{{ page }}</a>
    {% else %}
    <a href="#">{{ page }}</a>
    {% endif %}
{% endfor %}
</div>
__init__(query, page=1, limit=20)[source]

Initialize the paginator and set some default values.

has_next[source]

Return whether or not there are more pages from the currently displayed page.

Returns:boolean
has_previous[source]

Return whether or not there are previous pages from the currently displayed page.

Returns:boolean
iter_pages()[source]

An iterable containing the number of pages to be displayed.

Example:

{% for page in paginator.iter_pages() %}{% endfor %}
pages[source]

The total amount of pages to be displayed based on the number of results and the limit being displayed.

Returns:int