Python Forum
Paginate json data in flask or jinja2
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Paginate json data in flask or jinja2
I am using flask and generate tables that are filled with the JSON data that I retrieve. The problem that I have now is that I need to paginate through all the JSON data, because the maximum per page is set at '50'and I want to show all the products in my table.

So far I can't get this working and I don't really know how to get it working with Flask. I tried using a while loop, but that doesn't work with Jinja2 because that command is not recognized.

This is a part of my Python code:

 @app.route('/products',methods = ['POST', 'GET'])
    def products():
        shopnaam = request.form['shopname'] 
        username = request.form['username']
        password = request.form['password']
        login = 'https://'+shopnaam+''
        url = 'https://'+shopnaam+''
        payload = {
              'login[email]': username,
              'login[password]': password
        with requests.Session() as session:
              post =, data=payload)
              r = session.get(url)
              parsed = json.loads(r.text)
        return render_template('producten.html',parsed = parsed)
This is my Jinja2 code:

   <button class="collapsible">Bekijk product Informatie</button>
    <div class="content">
    <table id = "productentabel">
      <tr class = "header">
    	<th>Titel </th>
        <th>Prijs Exclusief BTW</th>
        <th>Prijs Inclusief BTW</th>
    	{% for product in parsed['products'] %}
    	<TD  width="100px" >{{product['id']}}</TD>
    	<TD  width="300px" >{{product['nl']['title']}}</TD>
    	<TD  width="150px">{{product['price_excl']}}</TD>
    	<TD  width="150px">{{product['price_incl']}}</TD>
    	<TD  width="300px">{{product['created_at']}}</TD>
    	{% endfor %}
    <input class = "exportknop" value="Exporteer product informatie" type="button" onclick="$('#productentabel').table2CSV({header:['ID','Titel','Prijs Exclusief BTW', 'Prijs Inclusief BTW', 'Datum']})">		
As you can see I am using a for loop, this code works, but the pagination is the issue.

My JSON looks like this:

products: [ { article_code: "123", barcode: "456", brand_id: 2600822, created_at: "2018-05-31T15:15:34+02:00", data01: "", data02: "", data03: "", delivery_date_id: null, has_custom_fields: false, has_discounts: false, has_matrix: false, hits: 0, hs_code: null, id: 72660113, image_id: null, is_visible: false, price_excl: 33.0165, price_incl: 39.95, price_old_excl: 0, price_old_incl: 0, product_set_id: null, product_type_id: null, search_context: "123 456 789", shop_id: 252449, sku: "789", supplier_id: 555236, updated_at: "2018-05-31T15:15:34+02:00", variants_count: 1, visibility: "hidden", weight: 0, nl: { content: "", fulltitle: "Grid Lifter", slug: "grid-lifter", title: "Grid Lifter" } ], links: { first: ".json", last: ".json?page=70", prev: null, next: ".json?page=2", count: 3497, limit: 50, pages: 70 }
So 'links' is where the pagination happens, I tried the following in my Python code and with this I get all the values printed in my python terminal. The problem that I have now is that I need this to work with jinja2/flask and enable pagination in my generated table to fill it will all the necessary data.

  while url:
            with requests.Session() as session:
                post =, data=payload)
                r = session.get(url)
                parsed = json.loads(r.text)
                for product in parsed['products']:
                    print(product['id'], product['nl']['title'])
                url = 'https://example/admin/products' + parsed['links']['next']  
Do you have the "choice" to do the pagination with javascript? Passing the JSON for a js function?
I have a project running a large amount of data that I show using DataTable.
I've done some pagination with AJAX too, but I have to find where the code is to show you.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask TypeError: Object of type Decimal is not JSON serializable mekacharan 0 1,508 Jul-15-2021, 05:28 AM
Last Post: mekacharan
  Jinja2 HTML <a> tags not rendering properly ChaitanyaPy 4 1,241 Jun-28-2020, 06:12 PM
Last Post: ChaitanyaPy
  Return Frame as well as JSON response using same API in Flask Python Ask jenkins43 0 892 May-11-2020, 04:58 PM
Last Post: jenkins43
  Extract json-ld schema markup data and store in MongoDB Nuwan16 0 1,380 Apr-05-2020, 04:06 PM
Last Post: Nuwan16
  how to save the data from MySQL to CSV in Flask farah97 4 1,588 Jan-03-2020, 03:02 AM
Last Post: farah97
  Using flask to add data to sqlite3 table with PRIMARY KEY catafest 1 2,729 Sep-09-2019, 07:00 AM
Last Post: buran
  [Flask] How to paginate a list of posts SheeppOSU 2 2,394 Jun-22-2019, 07:45 PM
Last Post: SheeppOSU
  Flask data with dynamic updates from panadas parthi1705 0 1,158 Jun-19-2019, 09:59 AM
Last Post: parthi1705
  Flask rest api How to retrieve json request raysefo 4 4,144 Jan-20-2019, 06:46 PM
Last Post: raysefo
  flask requests display data from api on webpage with javacript pascale 0 1,661 Oct-25-2018, 08:30 PM
Last Post: pascale

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020