python - Rendering JSON output on Flask Bootstrap Page -
i trying display json file flask html page.
flask
@app.route("/analysis") def analysis(): data = pd.read_csv('static/uploads/' + global_file.filename) temp_json = data.to_json(orient='records') json_df = json.loads(temp_json) list = json_df return render_template('results.html', summary = json.dumps(list)) html
<div class="results"> <table> {% key in summary %} {{ key.name }} {% endfor %} </table> </div> json
name age class marks 12 6 32 b 13 6 32 c 14 6 43 d 12 6 54 e 13 6 23 i getting blank page ouptut. want display pretty table on page.
the reason happens convert json object string using json.dumps iterate through string (character character) , try access name attribute. if leave out .name, indeed see every character printed out after eachother.
to fix should provide template actual object: list of dicts. so:
@app.route("/analysis") def analysis(): data = pd.read_csv('static/uploads/' + global_file.filename) temp_json = data.to_json(orient='records') json_df = json.loads(temp_json) return render_template('results.html', summary = json_df) then still there won't actual table, because in template there no tr or td elements. if fix that:
<div class="results"> <table> {% item in summary %} <tr><td> {{ item['name'] }} </td></tr> {% endfor %} </table> </div> you see actual table.
edit: conversions inside of analyse method bit superfluous, maybe shortened code post here?
Comments
Post a Comment