How to achieve pagination with results from a list in web.py python
Question: I am using python `web.py` to design a small web app , here actually i am not
using any database for fetching results/records, i will have a list of
records(which i will get from some where according to requirement :) )

Below is my code

**code.py**

    
    
    import web
    from web import form
    
    urls = (
        '/', 'index',
        '/urls', 'urls_result',
    )
    
    app =  web.application(urls, globals())
    render = web.template.render('templates/')
    
    class index:
        def GET(self):
            return 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&raquo;', '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'),
                           .............. 
                           ..............
                           .............. so on until 200 records      ]
            return 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 from the above code what i am doing is, when i run the server and hit the
browser with ip returned from `web.py`, it is redirected to home page (with
url `/` and template as `home.html`) which consists of form with a single
button.

Now here i am not using any `database` to fetch the records, simply i have
hardcored records which are in the form of `list of tuples` as you can see
above.

So when the user clicks the submit button i displaying the records in the form
of the `table` by directing to the `/url` that renders template
`recordslist.html`

now the above process is working fine. But here the `list of tuples/records`
may up to `200 or more`, so i want to implement `pagination` for `/url` page.

I have googled a lot and all hits are found for retreiving the records from
database, but not from the list, i am really confused on how to paginate the
results with `10 pages for page`.

So can anyone please let me now how to paginate the results/records from list
from the above code.


Answer: ### Get the Page

Firstly, you would have to pull the page out of the request from the user.
Assuming that you will use a page querystring parameter, you can use this to
determine the page number:

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

### Use the Page

Once you have a page, all that pagination involves is returning a slice of
results. The following function should give you the slices required (assuming
that pages are 1-indexed):

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

This will return the lower and upper bound that you can use to slice your
results list. So where you currently `return render.recordslist(result_list)`,
you could instead use:

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

