Where to set the debug flag in jars

Reading the flask documentation, I see that there are two places where the debug flag is usually placed:

  • after creating the flask object

    app = Flask(__name__) app.debug = True 
  • or run method

     app.run(host='0.0.0.0', debug = True) 

In my project, I have the app / init .py file:

  from flask import Flask app = Flask(__name__) #app.debug = True from app import views if app.debug == True: ... ... 

And the run.py file:

 from app import app import os port = int(os.environ.get('PORT', 5000)) app.run(host='0.0.0.0', port=port, debug = True) 

The problem that I see with the second option (by app.run) is that True will not be set until the run method is executed. Because, in my init .py file, I will have the default value app.debug (False). In the first option, I do not have this problem.

Is this right or is there something that I do not see? What is the best place to put debug value regardless of application?

+4
source share
1 answer

The docs say Both methods have the exact same effect. , but they refer to the fact that the Flask application is actually running.

http://flask.pocoo.org/docs/quickstart/#debug-mode

In the case described above, the best option would be to use the first method, since you check the value of app.debug before starting the application, but after determining it and after installing app.debug .

With this in mind, as your application gets larger, you might consider creating a slightly more robust structure in which you can define the config-$ENV.py with the debug flag set in it.

application / CONF / config -dev.py

 DEBUG = True # ... other settings (eg, log location, project root, etc) 

application / CONF / config -live.py

 DEBUG = False # ... other settings (eg, log location, project root, etc) 

app / CONF / __ __ INIT. RU

 EMPTY FILE 

app / __ __ INIT. RU

 from flask import Flask def create_app(env='dev'): app = Flask(__name__) app.config.from_object('app.conf.config-%s' % env) if app.debug: print 'running in debug mode' else: print 'NOT running in debug mode' return app 

This means that you can immediately check whether your application will be running in debug mode, and when you start the application, you can indicate in which environment it is running, which will determine whether the debug setting is set to True or False "

run.py

 from app import create_app import os port = int(os.environ.get('PORT', 5000)) app = create_app(env='dev') #Or pass 'live' to NOT be in debug mode app.run(host='0.0.0.0', port=port) 
+8
source

Source: https://habr.com/ru/post/1469073/


All Articles