Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way of detecting an automatic reload in flask's debug mode?

Tags:

python

flask

I have a flask app where I'd like to execute some code on the first time the app is run, not on the automatic reloads triggered by the debug mode. Is there any way of detecting when a reload is triggered so that I can do this?

To give an example, I might want to open a web browser every time I run the app from sublime text, but not when I subsequently edit the files, like so:

import webbrowser
if __name__ == '__main__':
    webbrowser.open('http://localhost:5000')
    app.run(host='localhost', port=5000, debug=True)
like image 752
Aaron Avatar asked Oct 21 '22 00:10

Aaron


1 Answers

You can set an environment variable.

import os
if 'WERKZEUG_LOADED' in os.environ:
    print 'Reloading...'
else:
    print 'Starting...'
    os.environ['WERKZEUG_LOADED']='TRUE'

I still don't know how to persist a reference that survives the reloading, though.

like image 92
Sergey Orshanskiy Avatar answered Oct 24 '22 01:10

Sergey Orshanskiy