Wednesday, 15 June 2011

How to achieve pagination with results from a list in web.py python -



How to achieve pagination with results from a list in web.py python -

i using python web.py design little web app , here not using database fetching results/records, have list of records(which according requirement :) )

below code

code.py

import web web import form urls = ( '/', 'index', '/urls', 'urls_result', ) app = web.application(urls, globals()) render = web.template.render('templates/') class index: def get(self): homecoming render.home() def post(self): result_list = [('images', 'http://www.google.co.in/imghp?hl=en&tab=wi'), ('maps', 'http://maps.google.co.in/maps?hl=en&tab=wl'), ('play', 'https://play.google.com/?hl=en&tab=w8'), ('youtube', 'http://www.youtube.com/?gl=in&tab=w1'), ('news', 'http://news.google.co.in/nwshp?hl=en&tab=wn'), ('gmail', 'https://mail.google.com/mail/?tab=wm'), ('drive', 'https://drive.google.com/?tab=wo'), ('more»', 'http://www.google.co.in/intl/en/options/'), ('web history', 'http://www.google.co.in/history/optout?hl=en'), ('settings', 'http://www.google.co.in/preferences?hl=en'), ('sign in', 'https://accounts.google.com/servicelogin?hl=en&continue=http://www.google.co.in/'), ('advanced search', 'http://www.google.co.in/advanced_search?hl=en-in&authuser=0'), .............. .............. .............. on until 200 records ] homecoming render.recordslist(result_list) if __name__ == "__main__": app.run()

home.html

$def with() <html> <head> <title>home page</title> <body alink="green" link="blue" > <div class="main"> <center> <form method="post" action='urls'> <input class="button" type="submit" name="submit" value="submit" /> </form> </center> </div> </body> </html>

recordslist.html

$def with(result_list) <html> <head> <title>list of records</title> </head> <body> <table> $for link in result_list: <tr> <td>$link[0]</td> <td>$link[1]</td> </tr> </table> </body>

so above code doing is, when run server , nail browser ip returned web.py, redirected home page (with url / , template home.html) consists of form single button.

now here not using database fetch records, have hardcored records in form of list of tuples can see above.

so when user clicks submit button displaying records in form of table directing /url renders template recordslist.html

now above process working fine. here list of tuples/records may 200 or more, want implement pagination /url page.

i have googled lot , hits found retreiving records database, not list, confused on how paginate results 10 pages page.

so can please allow me how paginate results/records list above code.

get page

firstly, have pull page out of request user. assuming utilize page querystring parameter, can utilize determine page number:

params = web.input() page = params.page if hasattr(params, 'page') else 1 use page

once have page, pagination involves returning piece of results. next function should give slices required (assuming pages 1-indexed):

def get_slices(page, page_size=10): homecoming (page_size * (page - 1), (page_size * page)

this homecoming lower , upper bound can utilize piece results list. return render.recordslist(result_list), instead use:

lower, upper = get_slices(page) homecoming render.recordslist(result_list[lower:upper])

python list pagination tuples web.py

No comments:

Post a Comment