0

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!!

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.