我目前正在使用 Flask 来提供可视化编程环境。如果用户稍后回来,我希望他们能够加载系统中当前的代码。我试过使用:
return redirect(url_for('static', filename='rawxml.txt'))
return redirect(send_from_directory('static', 'rawxml.txt'))
但是,两者都不会提供文件的修改版本,而是似乎是文件的缓存版本。我如何提供一个经常被新内容重写的文件。
注意:rawxml.txt 存储在“静态”目录中,但它是实际 XML 所在位置的符号链接(symbolic link)(我也尝试过硬链接(hard link))。
最佳答案
我有静态文件的下一个实现:
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
它只是将散列参数添加到使用 url_for
生成的 url。
关于python - 使用 Flask 提供变化的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18092380/