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

时间:2018-04-21 16:24:17

标签: javascript python python-2.7 flask python-3.6

我正在尝试仅在我的机器上实时显示图像。想想一个非常基本的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

img

模板/

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并查看:  enter image description here

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

enter image description here

但是当我将tree.jpg更改为背景中的其他图像并输入其他图像时,我无法获得我正在寻找的即时图像更新。换句话说,那棵树不会成为最新的树:(我们希望在我的基本网页上看到Maury的漂亮面孔

enter image description here

1 个答案:

答案 0 :(得分:0)

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

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

send_from_directory(directory, filename, cache_timeout=0)

相关文件:

相关问题