使用 Flask 提供不断变化的文件

joe*_*joe 4 python linux flask

我目前正在使用 Flask 提供可视化编程环境。我希望用户稍后回来时能够加载系统中当前的代码。我尝试过使用:

return redirect(url_for('static', filename='rawxml.txt'))
return redirect(send_from_directory('static', 'rawxml.txt'))
Run Code Online (Sandbox Code Playgroud)

但是,两者都永远不会提供文件的修改版本,而是提供似乎是文件的缓存版本。如何提供经常被新内容重写的文件。

注意:rawxml.txt 存储在“static”目录中,但它是指向实际 XML 位置的符号链接(我也尝试过硬链接)。

tbi*_*icr 5

我有静态文件的下一个实现:

hash_cache = {}

@app.url_defaults
def add_hash_for_static_files(endpoint, values):
    '''Add content hash argument for url to make url unique.
    It's have sense for updates to avoid caches.
    '''
    if endpoint != 'static':
        return
    filename = values['filename']
    if filename in hash_cache:
        values['hash'] = hash_cache[filename]
        return
    filepath = safe_join(app.static_folder, filename)
    if os.path.isfile(filepath):
        with open(filepath, 'rb') as static_file:
            filehash = get_hash(static_file.read(), 8)
            values['hash'] = hash_cache[filename] = filehash
Run Code Online (Sandbox Code Playgroud)

它只是将哈希参数添加到使用url_for.