Sunday, May 2, 2010

Announcing He3-AppEngine-Lib

I recently built a simple Paging module for Google App Engine, and in the process I created a Google Code site named He3-AppEngine-Lib to host this and any other App Engine code I wish to share. All the code on the site is available under the Apache 2.0 licence. Feel free to take a look!

2 comments:

  1. Hi Ben,
    I've had a quick look at the paging.py sample but I'm a little confused (mainly because I'm very much a Python newbie!)
    Do you have a sample that implements it where you maybe have a very simple query and use paging.py and the PageLinks functionality to navigate through the query ... I think once I see it in context I'll have a lightbulb moment (been a long time since I did anything more than simple javascript stuff)
    thanks
    J

    ReplyDelete
  2. Hi J

    Hopefully you won't mind if I stitch together an example from the examples already in the documentation.

    #a regular query
    widget_query = Widget.all().filter('category =', 6)

    #turn it into a PagedQuery
    paged_widget_query = PagedQuery(widget_query, 10) #where 10 is the page size you want

    #get the second page of results
    widget_page_results = paged_widget_query.fetch_page(2)

    #get the links to display on the page for other pages
    paged_links_object = PagedLinks(page=2, #we are on page 2
    page_count= pg2_widget.page_count(),
    url_root="/widgets/category/6", # will depend on your site
    page_field="page", #to create the query string in the link with the page number
    page_range=3 #to show at most three links on either side of the current page
    )

    links_list = paged_links_object.get_links()

    #pass both widget_page_results and links_list to your template for display

    Does that help? It think it is reasonably straightforward. For a newbie I think the hardest part is probably pulling out page number, page link pairs from the links_list.

    In python you could loop over and construct link text like this:

    for (page_number, page_link) in links_list:
    link_text = '%s' % (page_number, page_link)

    But more likely you would do this in the templating engine.

    Not sure how these code snippets will look in comments... please bear with me :)

    ReplyDelete