【问题标题】:Web: How often does Flask/JS refresh?Web:Flask/JS 多久刷新一次?
【发布时间】: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.htmlsrc=... 行上显示图像之外,这两个是相同的。

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


【解决方案1】:

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

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

send_from_directory(directory, filename, cache_timeout=0)

相关文档:

【讨论】:

  • 我没有使用send_from_directory()。我正在使用render_from_template() 并对文件进行硬编码。但也许我应该改用send_from_directory()render_from_template() 没有你提到的 cache_timeout 标志
  • 啊,是的。我不会在二进制文件上使用渲染模板方法。那是在创造不必要的工作。您明确导入了 send_from_directory - 我认为您必须使用 /static 路由。这将是一个过期问题。调试的一个好方法是 Chrome 的开发者工具。使用网络时间线检查每个请求/响应的标头。
  • 我现在尝试使用 flask_cache.Cache 并在每次需要重新执行 render_template('show.html') 时使用 cache.clear() 手动清除它。但它不起作用。开发人员工具有效,但在我展示此项目时不允许这样做
  • 我反复使用render_template()的原因是我想每次都显示文本框和上传的图片
  • 您实现客户端的方式必须为每次更新接收两个文件 - show.html 和 *.jpg。这是两个http请求。目前,您的浏览器通常会缓存 *.jpg 请求 - 因此不会再次向服务器请求它。为确保客户端 Web 浏览器始终再次请求图像,您必须设置一个标头以指示该文件已立即过期。
猜你喜欢
  • 2018-05-10
  • 2018-08-22
  • 1970-01-01
  • 2011-03-11
  • 2012-07-10
  • 2014-07-07
  • 2022-01-11
  • 2021-06-24
相关资源
最近更新 更多