5

I've read a couple dozen threads on this issue and I cannot find a solution that has worked for me so any help you can provide for me to understand what is breaking the reloader is greatly appreciated. I'm still fairly new to dev (mainly worked in node, angular and django). I've done tests on mac and ubuntu, with both a minimal flask app and a large application setup via their docs, here is the simplest test of the various attempts I've made:

in terminal:

python3 -m venv virtualenv
source virtualenv/bin/activate
pip install flask
vim server.py

then

from flask import Flask
app = Flask(__name__)


@app.route('/')
def home():
    return 'Hi'

wq and then back in terminal:

export FLASK_ENV=development
export FLASK_DEBUG=1
export FLASK_APP=server.py
flask run

now change the return statement string from 'hi' to 'hello' and nothing changes until I hit refresh on the browser. Importantly, the server does announce that there was a change - it just doesn't reload the browser. See output below:

(virtualenv) ➜  test git:(master) ✗ Flask run
 * Serving Flask app "server.py" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 297-732-354
 * Detected change in '/Users/pb/Documents/code/projects/test/server.py', reloading
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 297-732-354

I've tried this with template rendering, with alterations to css files, and like above with simple return statements. Thank you for any help you can offer me.

AndrogenAgonist
  • 244
  • 1
  • 3
  • 13

4 Answers4

6

Flask detects the local file changes, but won't push the changes to your browser. Flask and the browser are fully de-coupled, Flask just stands there and waits for requests, it only responds if it receives a request. After return 'Hello', the connection between the browser and Flask is going to be disconnected.

If you don't make a request to Flask, Flask can't send you the response.

davidism
  • 121,510
  • 29
  • 395
  • 339
Xiaohong Yuan
  • 493
  • 4
  • 12
  • 2
    It is standard behavior for the watchers I've worked with to reload the browser when they detect a change in the file. Does flask just not work in this manner? – AndrogenAgonist Jul 17 '18 at 11:14
  • 1
    @AndrogenAgonist Flask and the Browser are fully de-coupled, Flask just stand there, and waiting requests, only response if it receive a request. In advance, after "return 'Hello'", the connection between Browser and Flask is going to disconnected – Xiaohong Yuan Jul 17 '18 at 11:36
  • 2
    at the risk of asking the stupid question, what separates this from the way django or nodemon refresh the browser when a change in the file is detected? It sounds like flask doesn't have a watcher in the sense I originally understood how a watcher functions. – AndrogenAgonist Jul 17 '18 at 14:01
  • 1
    according to the anwser of [post](https://stackoverflow.com/questions/8106230/django-refresh-page-if-change-data-by-other-user) by @TimmyO'Mahony, it seems django doesn't have such a watcher, maybe you're looking for server-push or long-polling. – Xiaohong Yuan Jul 18 '18 at 02:30
  • yea, not sure which neurons were crossed, I think I've spent too long in angular where it will update without refreshing but that's obviously different, thanks everyone for all the help and kind feedback – AndrogenAgonist Jul 28 '18 at 05:05
3

You can use https://github.com/lepture/python-livereload to create a livereload debug server which can auto refresh your browser.

lepture
  • 2,307
  • 16
  • 18
  • 2
    I have tried but did not succeed. Would you mind providing an executable example as Github Gist? I really tried several things (e.g. this --> https://gist.github.com/amontalenti/8922609) and searched half Google. – Wlad Jul 10 '19 at 13:36
0

I don't think the browser is supposed to reload the information by itself - you have to refresh the page for anything new to show up, unless you have built that functionality in with some sort of backend application.

Magnetron
  • 357
  • 1
  • 7
  • 1
    see response on Bryan Yuan's answer - this is standard functionality with most dev environments I've run before - or at least a simple package install like if I'm working with node-sass and add a npm run watch:sass script. Is this just not a flask thing? – AndrogenAgonist Jul 17 '18 at 11:22
0

This library livereload automatically refreshes the page upon file changes.

One of the latest versions is not working correctly, so you must install the specific version 2.5.1.

  1. Install livereload at 2.5.1
pip install -Iv livereload==2.5.1
  1. Change your wsgi.py (provided by the documentation)
# app is a Flask object
app = create_app()

# remember to use DEBUG mode for templates auto reload
# https://github.com/lepture/python-livereload/issues/144
app.debug = True

server = Server(app.wsgi_app)
# server.watch
server.serve()
  1. Run python wsgi.py instead of flask run

Notice that it should display a different terminal than flask's.

testing_22
  • 2,340
  • 1
  • 12
  • 28