title: How to pass data from database to html using jinja2, Flask and mongodb and while loop
body: <p>I am confused about passing data from database (mongodb) to html.</p>
  
  <p>I have python code in "<strong>init</strong>.py" that queries database using while logic - see below code snippet:</p>
  
  <pre><code>from pymongo import MongoClient
  
  from flask import Flask, render_template
  
  @app.route("/snapshot")
  
  def pymongo_query():
     db=client.collection
     months=[1,2,3,4,5,6,7,8,9,10,11,12]
     while months:
        mon=months.pop()
        query=list(db.collection.find({args}))
        query=query[0]
        month=query['month']
        item_1=query['item_1']
        item_2=query['item_2']
  </code></pre>
  
  <p>What this codes does is it goes through each month represented by number in list and queries database for the respective month. I use while loop to go over months. In Python I would use print at the end of loop to output result.</p>
  
  <p>How do I output data into html using jinja2? I was wondering if I need to have while logic in jinja or keep it as I have it right now in <strong>init</strong>.py file above. </p>