printing HTML and inserting variables using python-flask
<p>Im trying to print html code that will also include variables, in this case its data from database with this code:</p>

<pre><code>def displayHot():
myCursor = mongo.db.posts.find()
for result_object in myCursor:
        print''' 
        Content-Type: text/html

        &lt;div class="card-header"&gt;
        &lt;h3 %s
        &lt;/h3&gt;
        &lt;/div&gt;
        &lt;div class="card-block"&gt;
        &lt;p&gt; %s 
        &lt;/p&gt;
        &lt;a class="panel-google-plus-image" href="{{url_for('Images', filename = 'Images/%s')}}"
        &lt;/a&gt;
        &lt;/div" 
        ''' % (result_object['uploader'], result_object['description'], result_object['file_name'])
</code></pre>

<p>and rendering it here:</p>

<pre><code>@app.route('/', methods=['POST','GET'])
def home():
    if request.method == 'GET':
        return render_template('index.html', function = displayHot())
</code></pre>

<p>when i run it, i get the printed data in my cmd.. so my question is how can i print the html code so it will act as html code and not just a print command in cmd</p>

<p>It looks like you would be better served by doing your looping in a jinja template, and just sending the template the data for the loop.  Something like this is what I'm thinking:</p>

<h3>Python code</h3>

<pre><code>@app.route('/', methods=['POST','GET'])
def home():
    if request.method == 'GET':
        myCursor = mongo.db.posts.find()
        return render_template('index.html', cursor = myCursor)
</code></pre>

<h3>index.html</h3>

<pre><code>&lt;!-- your other code here --&gt;
{% for result_object in cursor %}
    &lt;div class="card-header"&gt;
        &lt;h3&gt; {{result_object['uploader']}}
        &lt;/h3&gt;
    &lt;/div&gt;
    &lt;div class="card-block"&gt;
        &lt;p&gt; {{result_object['description']}}
        &lt;/p&gt;
        &lt;a class="panel-google-plus-image" href="{{url_for('Images', filename = '''Images/result_object['file_name']''')}}"
        &lt;/a&gt;
    &lt;/div&gt;
{% endfor %}
&lt;!-- the rest of your html --&gt;
</code></pre>

<p>This is simplified, and you would likely need to do more in order to get <code>myCursor</code> set up properly before sending it to the template, but I hope that gets the idea across.</p>

python html flask pymongo