【问题标题】:Print python console output on HTML page in Flask在 Flask 的 HTML 页面上打印 python 控制台输出
【发布时间】:2018-09-24 05:29:57
【问题描述】:

我想在 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()

这是我的结果.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>

【问题讨论】:

  • 从 src 中删除引号

标签: python python-3.x flask flask-restful


【解决方案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 简单,而且你很快就会习惯它:)

【讨论】:

  • 我试过你的方法迷人的机器人,但我仍然面临一些问题。现在在我的结果页面上,它打印了独特的图像,其中图像不可见。但我想用重复图像的数量打印唯一图像的名称
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 2020-05-17
  • 2022-10-14
  • 1970-01-01
相关资源
最近更新 更多