[英]how to display python console output on html page using flask
[英]Print python console output on HTML page in Flask
我想在Flask的HTML頁面上打印python控制台輸出。 請有人幫我做同樣的事情。 我做了三個文件。 app.py,index.html和result.html。
我的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()
這是我的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>
1) count
不是python函數。 而是使用enumerate
。
2)您在嵌套迭代中使用變量i
,這意味着第二個變量將覆蓋最外面的變量的值,這將破壞您的迭代。
您可以改為這樣:
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)
要么:
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)
甚至:
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])
但是,我建議使用第一個,因為它更具可讀性。
關鍵是,Python確實比C更簡單,並且在您習慣它之前不會花很長時間:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.