6

I have a fairly basic Flask app, but for some reason Debug mode wont enable, so whenever I get an error I get a 500 page instead of the nice debug page with the traceback and all that. Here's my app/init.py:

from flask import Flask
from config import config


def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    from .api import api as api_blueprint
    app.register_blueprint(api_blueprint, url_prefix='/api/v1.0')

    return app

and here's my config.py:

import os

basedir = os.path.abspath(os.path.dirname(__file__))

class Config:
    SECRET_KEY = '12345'
    SQL_DRIVER = 'SQL Server Native Client 11.0'
    SQL_SERVER = 'WIN8\MSSQL2K12'
    SQL_DATABASE = 'LogMe'
    SQL_USER = 'LogMe'
    SQL_PASSWORD = 'password'

    @staticmethod
    def init_app(app):
        pass


class DevelopmentConfig(Config):
    DEBUG = True

config = {
    'development' : DevelopmentConfig
}

I've posted the whole project up on GitHub if there happens to be an issue elsewhere, but I assume it's somewhere in these two files: https://github.com/jcaine04/perf-dash/tree/master/app

jcaine04
  • 427
  • 1
  • 8
  • 13

1 Answers1

6

The debugger is part of the WSGI runner; the app.run() server. If you use a different WSGI server you need to explicitly add the debugger middleware:

def create_app(config_name):
    app = Flask(__name__)

    # ...

    if app.debug:
        from werkzeug.debug import DebuggedApplication
        app.wsgi_app = DebuggedApplication(app.wsgi_app, True)

    return app

When you use Flask-Script, the runserver runs the Flask WSGI development server and will enable the debugger.

Unfortunately, Flask-Script version 2.0.3 introduced a bug; it doesn't set up the new debug flags correctly and always disabled the debugger unless you explicitly pass in the -d flag. This is regardless of wether you set use_debugger to true; this because the default of an argparse store_true option is to store False instead when not picked.

The work-around is to explicitly use -d, or to patch flask_script/commands.py to give all --debug and --no-debug options self.use_debugger as the default:

if self.use_debugger:
    options += (Option('-d', '--debug',
                       action='store_true',
                       dest='use_debugger',
                       help="(no-op for compatibility)",
                       default=self.use_debugger),)
    options += (Option('-D', '--no-debug',
                       action='store_false',
                       dest='use_debugger',
                       default=self.use_debugger),)

else:
    options += (Option('-d', '--debug',
                       action='store_true',
                       dest='use_debugger',
                       default=self.use_debugger),)
    options += (Option('-D', '--no-debug',
                       action='store_false',
                       dest='use_debugger',
                       help="(no-op for compatibility)",
                       default=self.use_debugger),)

where I've added default=self.use_debugger to the two options that did not yet have it set.

The handling of self.use_reloader is similarly flawed.

Versions 0.6.7 and 1.0 don't suffer from this bug; a fix has been committed for version 2.0.4 (not as yet released).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks for the response... I am using Flask-Script and the runserver command, so I don't think I need to explicitly add the debugger middleware. According to the documentation I should be able to add it to my config. From the Flask-Script documentation: "Unfortunately, Flask currently (as of May 2014) defaults to set the DEBUG configuration parameter to False. Until this is changed, you can safely add DEFAULT=None to your Flask configuration. Flask-Script’s runserver will then turn on debugging, but everything else will treat it as being turned off." – jcaine04 May 08 '14 at 21:09
  • @jcaine04: Yes, but you have `DEBUG = True` explicitly set in your config. The `runserver` command uses Flask's built-in server and does set `debug=True` when it executes `app.run()`. – Martijn Pieters May 08 '14 at 21:13
  • @jcaine04: looking at the Flask `app.run()` source code, Flask-Script does everything right, including setting `debug=True`, which *forces* the `app.debug` flag to be set to `True`, as well as explicitly setting `use_debugger` and `use_reloader` to `True`. Looking over your project code `manager.py` I see no obvious reasons that this wouldn't work. Make sure you have the latest versions, of course. – Martijn Pieters May 08 '14 at 21:20
  • OK, I'll keep plugging away. Thanks for taking a look at this. – jcaine04 May 08 '14 at 21:24
  • @jcaine04 - Just wanted to chime in with another data point. I'm experiencing the same behavior with Flask and Flask-Script. [You can view my source here.](https://github.com/voteblake/wib) I've replicated the issue on my local machine (ArchLinux) and a DigitalOcean Debian box. – voteblake May 08 '14 at 22:31
  • I'll try and debug this tomorrow; with two repositories it should be trivial to track down. – Martijn Pieters May 08 '14 at 22:38
  • It looks like it could be an issue with Flask-Script. I took Flask-Script out of the equation and I get the debugger as expected. – jcaine04 May 09 '14 at 02:32
  • Could be related/what's going on here. I'm on mobile so I haven't been able to dig too much into this yet: https://github.com/smurfix/flask-script/issues/91 – jcaine04 May 09 '14 at 02:37