问题描述
我想在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楼
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更简单,并且在您习惯它之前不会花很长时间:)