i have python dictionary of form
data = { 'category1': { 'titles': ['t1', 't2', 't3'], 'dates': ['d1', 'd2', 'd3'] }, 'category2': { 'titles': ['t4', 't5', 't6'], 'dates': ['d4', 'd5', 'd6'] } }
and want create html table looks like:
category1 category2 t1 t4 d1 d4 -------------------------- t2 t5 d2 d5 -------------------------- t3 t6 d3 d6 --------------------------
i using flask, flask/python code is:
from flask import flask, render_template import pickle app = flask(__name__) @app.route('/') def hello_world(): data=pickle.load(open('results.p','rb')) return render_template('index.html', x=data) if __name__ == "__main__": app.run()
and html template is:
<!doctype html> <html lang="en"> <head> <title>my webpage</title> </head> <body> <table> {% r,s in x.iteritems() %} <tr> <td>{{ r['titles'] }}{{r['dates']}}</td> <td>{{ s['titles'] }} {{s['dates']}}</td> </tr> {% endfor %} </table> </body> </html>
however getting internal server error when run this.
i have tired data.iteritems()
without joy.
i followed accepted answer here: python dictionary in html table, same error.
newdata = zip( zip(*(x['titles'] x in data.values())), zip(*(x['dates'] x in data.values()))) print(list(data.keys())) group in newdata: row in group: print(row) print('-----')
gives output:
['category1', 'category2'] ('t1', 't4') ('d1', 'd4') ----- ('t2', 't5') ('d2', 'd5') ----- ('t3', 't6') ('d3', 'd6') -----
as can see, items correctly paired in newdata
. can iterate on better , produce table output.
the template code this:
<table> {% group in x %} {% row in group %} <tr> <td>{{ row[0] }}</td> <td>{{ row[1] }}</td> </tr> {% endfor %} {% endfor %} </table>
Comments
Post a Comment