Flask: Allow debugger to be controlled from application configuration

Created on 6 Dec 2015  路  6Comments  路  Source: pallets/flask

I submitted a patch for this but learned that it was incomplete with further testing. https://github.com/mitsuhiko/flask/pull/1640

Basically, it would be nice to allow DEBUG on the application to control the activation of the debugger and reloader when running the CLI like app.run does.

Most helpful comment

I just want to respond as a user in a story :unicorn: that just did a hello world from the docs.

I expected that using DEBUG = True from http://flask.pocoo.org/docs/0.11/config/#configuration-best-practices would do the same as export FLASK_DEBUG=1 from http://flask.pocoo.org/docs/0.11/server/#command-line.

All 6 comments

I'm surprised noone has replied to this. I was really confused about this due to the comments in the source code at https://github.com/mitsuhiko/flask/blob/master/flask/cli.py#L399-L400

The reloader and debugger are by default enabled if the debug flag of Flask is enabled and disabled otherwise.

That doesn't seem to be the case. The app is configured with DEBUG to True but it doesn't get picked up by the run command.

I ended up writing my cli entry point like this.

@click.group(cls=FlaskGroup, create_app=create_app)                                
@script_info_option('--config', script_info_key='config')                          
@pass_script_info                                                                  
def cli(info, **params):                                                           
    '''This is an entry point for scripts that require the app context.            
    '''                                                                            
    info.debug = info.load_app().config['DEBUG']

The workaround above did not work for me. In the run_command() the debug flag is enabled based on the return value of get_debug_flag() which in turn only depends on the environment.

This is what worked for me

@click.group(cls=FlaskGroup, create_app=create_app)
@pass_script_info
def cli(info):
    """This is the management script of the dzik application"""
    os.environ["FLASK_DEBUG"] = str(info.load_app().config['DEBUG'])

I think the behavior of flask run unconditionally overriding app.debug is completely intentional. It is not particularly convenient, but given that there's already such a complex dependency between app.debug, Jinja's auto_reload etc (see https://github.com/pallets/flask/pull/1910 for just one example), I think this behavior is just easier to understand.

However, we definetly should provide a way to set FLASK_DEBUG from within your own CLI, no question about that.

I just want to respond as a user in a story :unicorn: that just did a hello world from the docs.

I expected that using DEBUG = True from http://flask.pocoo.org/docs/0.11/config/#configuration-best-practices would do the same as export FLASK_DEBUG=1 from http://flask.pocoo.org/docs/0.11/server/#command-line.

@dalai4git the FLASK_DEBUG environment needs to be either 0 or 1 rather than False or True and thus I think you additionally need to cast the DEBUG configuration variable to an int first, i.e.,

str(int(info.load_app().config['DEBUG']))

We do not plan to support this. Use FLASK_ENV=development to enable the development environment, or FLASK_DEBUG=1 to control debug mode.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xliiv picture xliiv  路  3Comments

lnielsen picture lnielsen  路  3Comments

dreampuf picture dreampuf  路  3Comments

stillesjo picture stillesjo  路  4Comments

ghost picture ghost  路  3Comments