Reputation: 361
Sorry if this question is a bit vague. I would like to display data from mongodb to a flask template, but I'm having troubles. This is what I have at this moment, but as you can see, it doesn't work. (I tried to be creative)
@app.route('/find')
def find():
collection = db.lists.find()
names = []
for data in collection:
names.append(data['name'])
return render_template("find.html", title="Find - Website", name=names)
I know that there has to be a better way to do it, and I would really appreciate it if somebody could show me. Thanks!!
Upvotes: 0
Views: 280
Reputation: 161
Not fully sure what is the end goal. However this is something I have used in couple of my projects before.If you want to show a table output with columns you can maybe leverage pandas and the pandas.to_html function. Something like below - I havent tested the syntax though - please do check from your end
`df = pd.DataFrame.from_dict(json_normalize(db.lists.find))
return render_template("find.html", title="Find - Website", tables = [df.to_html(classes='dfstyle',header='true')]`
you might have to import packages like jsonify from flask , pandas and pandas.io.json.json_normalize from python
Upvotes: 1