Javascript Web:Flask/JS多久刷新一次?

Javascript Web:Flask/JS多久刷新一次?,javascript,python,python-2.7,flask,python-3.6,Javascript,Python,Python 2.7,Flask,Python 3.6,我试图让图像只在我的机器上实时显示。想想谷歌图片的基本版本。用户输入“redhammer”,我给他们看一张redhammer的图片 问题在于刷新率。我更新了要显示的图像文件,当我直接以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.jp

我试图让图像只在我的机器上实时显示。想想谷歌图片的基本版本。用户输入“redhammer”,我给他们看一张redhammer的图片

问题在于刷新率。我更新了要显示的图像文件,当我直接以
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>
<!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 现在,如果我已经正确地为您提供了所有信息,您应该能够调用
python3server.py
并看到以下内容:

如果您在框中键入“hi”,它将显示:

但是,当我将tree.jpg更改为背景中的其他图像并键入其他图像时,我不会得到我正在寻找的即时图像更新。换句话说,这棵树不会成为最近的树:(我们想在我的基本网页上看到莫里美丽的脸


您的问题与http缓存有关-了解http Expires标头。Flask默认将Expires标头设置为12小时。这指示您的web浏览器在12小时内无需再次请求tree.jpg。当您的浏览器再次需要tree.jpg时,它只需从缓存中加载它,甚至不会向您的浏览器发出请求服务器。在浏览器中手动输入this tree.jpg URL会覆盖此内容,在这样做时,您会要求浏览器再次请求它

您似乎没有提供相关的代码--
send\u from\u directory
调用为静态文件提供服务是您需要进行更改的地方

send\u from\u目录(目录、文件名、缓存\u超时=0)

相关文件:

  • send_file_max_age
    在同一页上(默认有效期)

您可以禁用浏览器缓存并尝试重新加载吗?我认为这有帮助,但我认为问题的一个重要方面是/generator在键入查询后调用自身时不会刷新页面。因此
show.html
调用generator()这又防止了show.html的重新加载?但我对Flask和webdev是新手,所以我不确定这是否正确。我没有使用
send\u from\u directory()
。我使用
render\u from\u template()
并对文件进行硬编码。但也许我应该使用
send\u from\u directory()
来代替
render\u from\u template()
没有您提到的标记
缓存\u超时
啊,是的。我不会在二进制文件上使用渲染模板方法。这会创建不必要的工作。您显式地从\u目录导入了发送\u-我想您一定使用了/static路由。这将是一个过期问题。调试Chrome的开发工具是一个很好的方法。使用网络时间线检查每个请求/响应的标题。我现在尝试使用
flask\u cache.cache
并在每次需要重新执行
render\u模板('show.html')时使用
cache.clear()
手动清除
。但它不起作用。开发人员工具起作用了,但当我演示此项目时,这是不允许的。我使用
render\u template()的原因
我想反复显示文本框和上传的图像。每次更新时,客户端都必须接收两个文件-show.html和*.jpg。这是两个http请求。目前,您的浏览器经常缓存*.jpg请求-因此不再向服务器请求。为确保客户端web浏览器始终是对于图像,您必须设置一个头,指示该文件已立即过期。
#!/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)