在 Flask 的 HTML 页面上打印 python 控制台输出
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 语言简单,您很快就会习惯它:)
我想在 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 语言简单,您很快就会习惯它:)