I am using a Flask Code with the following route:
@app.route(\'/download\')
def download_file():
path = \"certificate.docx\"
print(\"certificate printed\
This problem is most likely caused by caching which is built into Flask. This sets the Cache-Control
header, and applies to static files served by Flask, but also the send_file
and send_from_directory
functions. (reference).
This would explain the behaviour of the file downloading, but the print statements not running. In fact that request won't even be hitting the server.
You can visualise this on the network tab of the dev tools:
You can disable this by setting the following config variable on your app:
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = -1
The -1
value disables caching.
You may have to actually clear the browser cache for this setting to take affect, or change the URL of your /download
endpoint, although this is inconvenient.
Note the difference once this is set: