Paginate json data in flask or jinja2 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Paginate json data in flask or jinja2 (/thread-11160.html) |
Paginate json data in flask or jinja2 - soli1994 - Jun-26-2018 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+'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)This is my Jinja2 code: <button class="collapsible">Bekijk product Informatie</button> <div class="content"> <table id = "productentabel"> <tr class = "header"> <th>ID</th> <th>Titel </th> <th>Prijs Exclusief BTW</th> <th>Prijs Inclusief BTW</th> <th>Datum</th> {% for product in parsed['products'] %} <TR> <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> </TR> </tr> {% endfor %} </table> <input class = "exportknop" value="Exporteer product informatie" type="button" onclick="$('#productentabel').table2CSV({header:['ID','Titel','Prijs Exclusief BTW', 'Prijs Inclusief BTW', 'Datum']})"> </div>As you can see I am using a for loop, this code works, but the pagination is the issue. My JSON looks like this: 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 = 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'] RE: Paginate json data in flask or jinja2 - gontajones - Jun-28-2018 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. |