What's a good way to calculate the execution time for every page load using Python's Flask library?
I was thinking of putting a start timer in views/__ init__.py:
@app.before_request def before_request(): g.start = time.time()
But where would I put the end part of the timer to calculate the difference, and how would I get it into the footer of my html template?
Put it in
@app.teardown_request def teardown_request(exception=None): diff = time.time() - g.start ...
teardown_request you are not allowed to change the response, If you want to use the calculated string in your response use
@app.after_request def after_request(response): diff = time.time() - g.start if (response.response): response.response = response.response.replace('__EXECUTION_TIME__', str(diff)) return response