chetan parmar
chetan parmar

Reputation: 73

Print python console output on HTML page in Flask

I want to print the python console output on Html page in Flask. Please someone help me to do the same. I have made three files. app.py, index.html and result.html .

My app.py:

for i in image_path_list:
        j=j+1
        if i in duplicate:
            continue
        else:
            print(i+"  "+str(count[j])+"\n")
    return render_template('results.html', file_urls=file_urls)

if __name__ == '__main__':
    app.run()

This is my result.html

<h1>Hello Results Page!</h1>
<a href="{{ url_for('index') }}">Back</a><p>

<ul>
{% for file_url in file_urls %}
    <li><img style="height: 150px" src="{{ file_url }}"></li>
{% endfor %}
</ul>

Upvotes: 1

Views: 4798

Answers (1)

Charming Robot
Charming Robot

Reputation: 2660

1) count isn't a python function. Instead use enumerate.

2) You're using the variable i in a nested iteration, which means that the second one will override the value of the outermost one, which will break your iteration.

You can instead do it like that:

file_urls = []
for count, image_path in enumerate(image_path_list):
   if image_path not in duplicate:
      file_urls.append(str(count) + ". " + image_oath)

return render_template('results.html', file_urls=file_urls)

or:

file_urls = [". ".join(str(count),image_path) for count, image_path in enumerate(image_path_list) if image_path not in duplicate]
return render_template('results.html', file_urls=file_urls)

or even:

return render_template('results.html', file_urls=[".".join(str(count),image_path) for count, image_path in enumerate(image_path_list) if image_path not in duplicate])

However, I recommend using the first one as it's more readable.

The point is, Python is really simpler than C and it won't take you long till you get used to it :)

Upvotes: 2

Related Questions