0

I built a little web app in Flask and am trying to deploy it on Pythonanywhere.

During development, I used a local server and launch my app as such:

if __name__ == '__main__':
    app.run(debug=True,extra_files=['./static/results.csv',])

results.csv serves as a tiny database (I don't need more) and everytime a line is added to the file, from a Flask form submition, the app is releaded. I then get:

Detected change in '/Users/rodolphegonzales/sync/Survey/app/static/results.csv', reloading

Once I deploy on Pythonanywhere, this doesn't work anymore. when I run:

if __name__ == '__main__':
    app.run(debug=False,extra_files=['./static/results.csv',])

I get:

Internal Server Error. The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

Looking at the log, this is due to the fact that the changes in results.csv weren't detected, and that the app wasn't reloaded.

How to properly detect changes in extra files when deploying a Flask app?

EDIT: file change detection and reload is apparently impossible to do outside of the debug mode. A touch to the WSGI should automatically reload, I'll try it out.

Lucien S.
  • 5,123
  • 10
  • 52
  • 88
  • You can now use the `TEMPLATES_AUTO_RELOAD = True` option as mentioned in this answer - https://stackoverflow.com/a/38371484/1662984 – Kevin Jul 02 '17 at 06:45

2 Answers2

1

This has nothing to do with the reloader (well, at least not the way you think). PythonAnywhere runs a WSGI server for you, pointed at your app. If you call the dev server unconditionally, it will block and PythonAnywhere's server will never pick it up.

Move the call to the dev server inside a __name__ guard.

if __name__ == '__main__':
    app.run(...)

You should not reload an app automatically in production (and based on the way PythonAnywhere works, you wouldn't be able to, you need to manually reload). Based on what you've described, you shouldn't need to reload it at all, just re-read the static file when you need to get data. Given that most WSGI servers run in multiple processes, you're probably at risk of corrupting your simple file by writing to it from multiple processes at once. This is why you use a database.

davidism
  • 121,510
  • 29
  • 395
  • 339
0

Ok, my little knowledge of how this works in deployment confused me. it turns out I can't, outside of development using debug = True, and to the best of my knowledge, automatically observe changed files and reload the app through an argument in app.run(...). A solution is to "touch" Pythonanywhere's wsgi file anytime another form is submitted (in which case the app is reloaded). I'm doing it like this:

import os
os.utime('/var/www/USERNAME_pythonanywhere_com_wsgi.py', None)

This seems to do the trick. Thanks for your help!

Lucien S.
  • 5,123
  • 10
  • 52
  • 88