Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto reloading Flask app when source code changes

I know for a fact that Flask, in debug mode, will detect changes to .py source code files and will reload them when new requests come in.

I used to see this in my app all the time. Change a little text in an @app.route decoration section in my views.py file, and I could see the changes in the browser upon refresh.

But all of a sudden (can't remember what changed), this doesn't seem to work anymore.

Q: Where am I going wrong?

I am running on a OSX 10.9 system with a VENV setup using Python 2.7. I use foreman start in my project root to start it up.

App structure is like this:

[Project Root]
+-[app]
| +-__init__.py
| +- views.py
| +- ...some other files...
+-[venv]
+- config.py
+- Procfile
+- run.py

The files look like this:

# Procfile
web: gunicorn --log-level=DEBUG run:app
# config.py
contains some app specific configuration information.
# run.py
from app import app

if __name__ == "__main__":
    app.run(debug = True, port = 5000)
# __init__.py
from flask import Flask
from flask.ext.login import LoginManager
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.mail import Mail
import os

app = Flask(__name__)
app.config.from_object('config')

db = SQLAlchemy(app)

#mail sending
mail = Mail(app)

lm = LoginManager()
lm.init_app(app)
lm.session_protection = "strong"

from app import views, models
# app/views.py
@app.route('/start-scep')
def start_scep():
    startMessage = '''\
<html>
<header>
<style>
body { margin:40px 40px;font-family:Helvetica;}
h1 { font-size:40px; }
p { font-size:30px; }
a { text-decoration:none; }
</style>
</header>

<p>Some text</p>
</body>
</html>\
'''
    response = make_response(startMessage)
    response.headers['Content-Type'] = "text/html"
    print response.headers
    return response
like image 521
HanSooloo Avatar asked May 01 '14 01:05

HanSooloo


People also ask

How do I rerun Flask app?

To run the application, use the flask command or python -m flask . You need to tell the Flask where your application is with the --app option. As a shortcut, if the file is named app.py or wsgi.py , you don't have to use --app . See Command Line Interface for more details.

Is Flask synchronous or asynchronous?

Flask has been claimed as synchronous on many occasions, yet still possible to get async working but takes extra work.

How do you turn on hot reloading on a Flask?

Enable Flask development mode By default, flask run launches a production-friendly server process. However, you can opt into a hot-reloading debug mode by setting the FLASK_ENV environment variable. Now, changing any python source files will automatically restart the flask process.


1 Answers

Sample Application where app is our application and this application had been saved in the file start.py:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hallo():
    return 'Hello World, this is really cool... that rocks... LOL'

now we start the application from the shell with the flag --reload

gunicorn -w 1 -b 127.0.0.1:3032 start:app --reload

and gunicorn reloads the application at the moment the file has changed automaticly. No need to change anything at all.

if you'd love to run this application in the background add the flag -D

gunicorn -D -w 1 -b 127.0.0.1:3032 start:app --reload

-D Demon mode

-w number of workers

-b address and port

start (start.py) :app - application

--reload gunicorns file monitoring

Look at the settings file: http://docs.gunicorn.org/en/latest/settings.html

all options and flags are mentioned there. Have fun!

like image 96
SmileMZ Avatar answered Sep 21 '22 00:09

SmileMZ