6

Consider, I have a flask server deployed which routes to multiple webpages. If I want to change content of one route, by changing its code, is it possible to reflect those changes in webpages without rerunning the flask server? It is possible to host and rerun other scripts on the linux server or entriely another flask server as long as the website url(port number and route) doesn't change.

Please suggest any way you can come up with!

Jayant
  • 83
  • 1
  • 9
  • 1
    As far as I know that is done automatically if you do ``app.run(debug=True)`` – Gonzalo Hernandez Mar 05 '20 at 07:20
  • 1
    @GonzaloHernandez Thats a way but it has vulnerability. Running a Flask application with debug mode enabled may allow an attacker to gain access through the Werkzeug debugger. (https://help.semmle.com/wiki/display/PYTHON/Flask+app+is+run+in+debug+mode) – Jayant Mar 05 '20 at 11:11

5 Answers5

5

setting flask environment will do the thing for you in the shell.

export FLASK_ENV=development
Nilanka Manoj
  • 3,527
  • 4
  • 17
  • 48
2

Assuming you are using flask development server,

Yes, its possible using use_reloader=True,

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    # You business logic code
    return some_data

if __name__ == '__main__':
    app.run(debug=False, host='0.0.0.0', port=8000,
            use_reloader=True, threaded=True)


However it is not good idea to use it in production, related Is the server bundled with Flask safe to use in production?

anils
  • 1,782
  • 6
  • 19
  • 29
0

My assumption is that you want your deployment service to recognize that a change has occurred in a file, which prompts it to automatically restart to reflect the changes, rather than you manually restarting the service.

Assuming that you are deploying on a production uWSGI server, starting the service with touch-reload=/path/to/folder would allow uWSGI to reload when the specified file/folder is modified or touched. https://uwsgi-docs.readthedocs.io/en/latest/Options.html#touch-reload

--touch-reload=/path/to/file.txt
pastaleg
  • 1,782
  • 2
  • 17
  • 23
0

I think you can only do this with the Apache Webserver. Refer to the Flask documentation. I haven't tried it (yet), but when you have deployed your new code any small change in the wsgi-file should automatically reload the code.

You mentioned two requirements

  1. I have a flask server deployed I would assume you mean it is deployed in a production environment. This automatically rules out debug-mode from the development server, because it is not stable, efficient or secure for production use.
  2. Without rerunning the flask server This rules out autoreload, because this will completely restart your server. Any requests that come in until the server is ready again will fail.
marco
  • 41
  • 3
-1

One way of going about is by running your application with debug mode ON through app.run(debug=True)

The problem with this approach is that your application would be exposed over the internet with the application's internal debugger on which shouldn't be done.

A better way I can think of is to call the functions you need to change frequently from a different file other than where your core flask code exists and change in that file whenever needed. This way you don't need to change your Flask code and you won't need to restart the application


Edit:

A sample Flask route would be

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    # Route's business logic usually goes here
    return data

Now, instead of writing your application's business logic in the same file as your route, you could simply write it in a different file as follows and import it in the file with the route:

File with the business logic

def process_hello_world(params):
    # Write your business logic here which you want to change without reloading the application
    return data

And in the application file with the route call the business logic as follows:

from flask import Flask
from 
app = Flask(__name__)

@app.route('/')
def hello_world():
    data = process_hello_world()
    return data