Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put the debug flag in flask applications

Tags:

python

flask

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

  • after the creation of the flask object

    app = Flask(__name__)
    app.debug = True
    
  • or by the 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 the True value won't be set until executing the run method. Because that, in my init.py file I will have the default value of app.debug (False). By the first option I don't have that problem.

Is the above right or is there anything that I don't see? What is then the best place to put the debug value regardless the application?

like image 719
Ivan Avatar asked Mar 13 '13 20:03

Ivan


People also ask

What is flask debugger pin?

The Debugger PIN is a just an added layer of security in case you inadvertently leave the Debug mode on in an Production application to make it difficult for the attacker to access the debugger .

What does debug true mean in flask?

Flask Debug mode allows developers to locate any possible error and as well the location of the error, by logging a traceback of the error. The Flask debug mode also enables developers to interactively run arbitrary python codes, so that one can resolve and get to the root cause on why an error happened.

How do I enable debugging in Python?

The debugger is enabled by default when the development server is run in debug mode. When running from Python code, passing debug=True enables debug mode, which is mostly equivalent. Development Server and Command Line Interface have more information about running the debugger and debug mode.


1 Answers

The docs say Both methods have the exact same effect., but they are referring to after the Flask app has actually been run.

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

In the case you describe above, the best option would be to use the first method as you're checking app.debug's value before the app is run, but after it is defined and after app.debug is set.

With that in mind, as your app gets bigger, you might consider having a slightly more robust structure where you can define a config-$ENV.py file that has the debug flag set in it.

app/conf/config-dev.py

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

app/conf/config-live.py

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

app/conf/__init__.py

EMPTY FILE

app/__init__.py

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 you'll be able to immediately check whether or not your app is going to be run in debug mode, and when you run your app you can tell it which environment it is being run in, which will define whether or not debug 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)
like image 113
Chris McKinnel Avatar answered Oct 01 '22 09:10

Chris McKinnel