Pandas dataframe to flask template as a json [duplicate] Pandas dataframe to flask template as a json [duplicate] flask flask

Pandas dataframe to flask template as a json [duplicate]


Per pandas docs, to_json renders a json string which you can confirm with type(jsonfiles). Though the print output looks like a json object, single quotes will wrap at beginning and end.

To resolve, simply import the built-in json module (part of Python standard library) and parse the to_json string into a json object with loads method:

import json...jsonfiles = json.loads(df.to_json(orient='records'))return render_template('index.html', ctrsuccess=jsonfiles)