title: Paginate through json data with Flask
body: <p>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. </p>
  
  <p>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. </p>
  
  <p>This is my Python code:</p>
  
  <pre><code>@app.route('/products',methods = ['POST', 'GET'])
  def products():
      shopnaam = request.form['shopname'] 
      username = request.form['username']
      password = request.form['password']
      login = 'https://'+shopnaam+'example.com'
      url = 'https://'+shopnaam+'.example.com/admin/products.json'
  
      payload = {
            'login[email]': username,
            'login[password]': password
            }
  
      with requests.Session() as session:
            post = session.post(login, data=payload)
  
            r = session.get(url)
            parsed = json.loads(r.text)
  
  
      return render_template('producten.html',parsed = parsed)
  </code></pre>
  
  <p>This is my Jinja2 code:</p>
  
  <pre><code>&lt;button class="collapsible"&gt;Bekijk product Informatie&lt;/button&gt;
  &lt;div class="content"&gt;
  &lt;table id = "productentabel"&gt;
    &lt;tr class = "header"&gt;
      &lt;th&gt;ID&lt;/th&gt;
      &lt;th&gt;Titel &lt;/th&gt;
      &lt;th&gt;Prijs Exclusief BTW&lt;/th&gt;
      &lt;th&gt;Prijs Inclusief BTW&lt;/th&gt;
      &lt;th&gt;Datum&lt;/th&gt;
  
      {% for product in parsed['products'] %}
      &lt;TR&gt;
      &lt;TD  width="100px" &gt;{{product['id']}}&lt;/TD&gt;
      &lt;TD  width="300px" &gt;{{product['nl']['title']}}&lt;/TD&gt;
      &lt;TD  width="150px"&gt;{{product['price_excl']}}&lt;/TD&gt;
      &lt;TD  width="150px"&gt;{{product['price_incl']}}&lt;/TD&gt;
      &lt;TD  width="300px"&gt;{{product['created_at']}}&lt;/TD&gt;
      &lt;/TR&gt;
      &lt;/tr&gt;
      {% endfor %}
      &lt;/table&gt;
  
  &lt;input class = "exportknop" value="Exporteer product informatie" type="button" onclick="$('#productentabel').table2CSV({header:['ID','Titel','Prijs Exclusief BTW', 'Prijs Inclusief BTW', 'Datum']})"&gt;     
  &lt;/div&gt;
  </code></pre>
  
  <p>As you can see I am using a for loop, this code works, but the pagination is the issue.</p>
  
  <p>My JSON looks like this:</p>
  
  <pre><code>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,
  
  links: {
  first: ".json",
  last: ".json?page=70",
  prev: null,
  next: ".json?page=2",
  count: 3497,
  limit: 50,
  pages: 70
  }
  </code></pre>
  
  <p>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. Only I can't send the data to the tables.</p>
  
  <pre><code>while url:
          with requests.Session() as session:
              post = session.post(login, 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']  
  </code></pre>