I love flask and django for making web app and often use Flask for web app development.
Sometime the app will serve files after getting user request. In this case, static files which are generated by the app will be stored in static folder. And the folder will store lots of files. So I would like to delete these files after serving the file to use. How to do it?
I searched google and found good solution ;) Flask has request callback function to do the task. ‘after_this_request‘ method is suitable for my situation.
An example of using the method is below. after_this_request is used as decorator.
from flask import Flask
from flask import render_template, after_this_request, request, send_file
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def hello():
if request.method == 'POST':
# do something
@after_this_request
def rm_file(response):
# remove generated file
return response
return send_file('generatedfile')
return render_template('hoge.html')
Flask has many useful callback functions and I’ve never used them. So I would like to read the document and use them in my products.