Pyramid framework tips : WebHelpers html tags and Paginate

WebHelpers has many useful functions. WebHelpers html tags and paginate are very useful for Pyramid Applications.Some features are not compatible with Pyramid framework, please see documentations.

WebHelpers in Pyramid Applications: install WebHelpers with easy_install:

$ easy_install WebHelpers

In Pyramid Application in __init__.py:

def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
  ...
  config.add_subscriber(add_renderer_globals, BeforeRender)
  ...

In helpers.py module:

  # your helpers imports
  from webhelpers.html.tags import *

In subscribers.py module:

def add_renderer_globals(event):
    """ add helpers """
    event['h'] = helpers

In Mako Templates:

  ## text input
  ${h.text("name", model.name, class_ = "large", maxlength="100")}

How to use WebHelpers Paginate with SQLAlchemy?
In your controller code:

@view_config(route_name="category_list", renderer="category/list.html")
def list(request):
    """categories list """
    ...
    # db query
    dbsession = DBSession()
    query = dbsession.query(Customer)

    # paginate
    page_url = paginate.PageURL_WebOb(request)
    customers = Page(query,
                     page=int(request.params.get("page", 1)),
                     items_per_page=10,
                     url=page_url)

    return {"customers": customers}

Paginate format in Mako Templates:

<div class="pager">
  <% link_attr={"class": "btn small"} %>
  <% curpage_attr={"class": "btn primary small disabled"} %>
  <% dotdot_attr={"class": "btn small disabled"} %>
  ${customers.pager(format="$link_previous ~2~ $link_next",
    symbol_previous="«",
    symbol_next="»",
    link_attr=link_attr,
    curpage_attr=curpage_attr,
    dotdot_attr=dotdot_attr)}
</div>

paginate

I am using Bootstrap from Twitter as css framework.

13 Comments

  1. BruceC
    Jan 13, 2012 @ 02:49:37

    Hi Cem, nice tutorial. Any tips on how you might modify your pagination code to perform an AJAX implementation?

    Reply

    • Cem Ikta
      Jan 13, 2012 @ 15:12:40

      Hi Bruce,

      Unfortunately I don’t have ajax pagination examples with Paginate. In Webhelpers documentation there are ajax parameters, but no code examples.

      Reply

    • Cem Ikta
      Jan 14, 2012 @ 04:58:46

      Hi Bruce,

      I have implemented WebHelpers Paginate with ajax partial rendering, see please Customers App code on GitHub.
      I hope it helps.

      Reply

  2. BruceC
    Jan 16, 2012 @ 08:52:49

    Hi Cem,

    You champion! Worked like a treat…My old pylons apps had used the “if ‘partial’ in request params just render the partial template otherwise render the full page”, but as I have been following the Pyramid docs I had been using a single renderer call in my pyramid @view_config() declaration, & I was thus uncertain how to render specific templates based on request params. Your example app solved the riddle, so many thanks!

    Reply

  3. Tonio
    Apr 17, 2012 @ 11:03:42

    Thanks for that!
    I just had to change next/previous symbol (u’«’, u’»’) to make it works.

    Reply

  4. Joshua Welch
    Feb 27, 2013 @ 16:09:01

    Hey thanks a bunch for this.

    It seems your code examples are repeating attrs, for example i see “dotdot_attrdotdot_attr”

    This really helped when I figured that out!

    Reply

  5. ironhoang
    May 13, 2013 @ 05:27:06

    Hi. I have a question.
    How can i do, if i have two partial need Paginate ?

    Reply

    • Cem Ikta
      May 13, 2013 @ 13:47:56

      Hi,

      I have written a ui helpers, so you can use paginate with multiple lists.
      https://github.com/devsniper/customers/blob/master/customers/templates/base/uiHelpers.html

      Add this namespace in your mako templates:
      and paginate code in your templates:
      ## pager with category items
      ${pager(categories)}

      ## pager with customer items
      ${pager(customers)}

      More details in customers project: https://github.com/devsniper/customers

      Reply

      • ironhoang
        May 15, 2013 @ 04:39:46

        No no.
        I already used it
        ## pager with category items
        ${pager(categories)}

        ## pager with customer items
        ${pager(customers)}

        but when changed categories page, the customers page changed too
        How can i fix it
        Thank you

        Reply

      • ironhoang
        May 15, 2013 @ 04:43:36

        Note: I have two list, for example one categories and one customers. There list display in a site.
        Please help me

        Reply

        • Cem Ikta
          May 16, 2013 @ 11:50:00

          I do not know, works for me. Maybe copy pager partial in template and try 2 lists without pager partial.

          Reply

Leave a Reply