【发布时间】:2018-04-21 16:24:16
【问题描述】:
我试图让图像仅在我的机器上实时显示。想想一个非常基本的 Google 图片版本。用户输入“红锤”,我给他们看一张红锤的图片
问题在于刷新率。我更新了要显示的图像文件,当我直接将其查找为http://127.0.0.1:6007/static/tree.jpg 时,它会立即给我最新的 jpg。然后,奇怪的是,在我查找http://127.0.0.1:6007/static/tree.jpg 之类的内容后,图像在最初的http://127.0.0.1:6007 上发生了变化!
我的设置:
在static/目录下,tree.jpg:
模板/
show.html
在templates/,show.html:
<!DOCTYPE html>
<html>
<body>
<h1>Text-to-Image Synthesis</h1>
<form method="POST" action="/generator">
<p>Input to Generator: <input type="text" name="input_text"><input type="submit" value="Generate Image"></p>
</form>
<img src="{{url_for('static', filename='tree.jpg')}}" />
</body>
</html>
index.html
和index.html:
<!DOCTYPE html>
<html>
<body>
<h1>Text-to-Image Synthesis</h1>
<form method="POST" action="/generator">
<!-- button -->
<p>Input to Generator: <input type="text" name="input_text"><input type="submit" value="Generate Image"></p>
</form>
</body>
</html>
除了show.html 在src=... 行上显示图像之外,这两个是相同的。
server.py
#!/usr/bin/env python2.7
import os
from flask import Flask, request, render_template, g, redirect, Response, send_from_directory
tmpl_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
app = Flask(__name__, template_folder=tmpl_dir)
@app.route('/')
def index():
return render_template("index.html")
@app.route('/generator', methods=['POST'])
def generator():
# save typed-in text
text = request.form['input_text']
filename = "/home/ubuntu/icml2016/scripts/cub_queries.txt"
with open(filename, "a+") as f:
f.write(text + "\n")
"""
print('start')
subprocess.call('./scripts/demo_cub.sh', shell=True) # change the image in the background
print('end')
"""
return render_template("show.html")
if __name__ == "__main__":
HOST='0.0.0.0'
PORT=6007
app.run(host=HOST, port=PORT)
所以现在,如果我已经正确地为您提供了所有内容,您应该可以致电 python3 server.py 并看到以下内容:
如果你在框中输入“hi”,它会显示:
但是当我将 tree.jpg 更改为背景中的其他图像并输入其他内容时,我没有得到我正在寻找的即时图像更新。换句话说,那棵树不会成为最近的树 :( 我们想在我的基本网页上看到 Maury 的美丽面孔
【问题讨论】:
-
你能禁用浏览器缓存并尝试重新加载吗?
-
我认为这有帮助,但我认为问题的很大一部分是 /generator 在您输入查询后调用自身时不会刷新页面。所以
show.html再次调用 generator() 来阻止 show.html 重新加载?但我是 Flask 和 webdev 的新手,所以我不确定这是否正确
标签: javascript python python-2.7 flask python-3.6