Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't enable debug mode in Flask

Tags:

python

flask

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

like image 910
jcaine04 Avatar asked May 08 '14 20:05

jcaine04


People also ask

How do I turn on debug mode on flask?

If you click on the “Run and Debug” icon on the left hand side of the IDE or alternatively type Ctrl+Shift+D you will see the “RUN AND DEBUG” window. Now click on the “create a launch. json file” link and when prompted to “Select a debug configuration” choose “Python File Debug the currently active Python file”.

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 .


1 Answers

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).

like image 199
Martijn Pieters Avatar answered Oct 18 '22 06:10

Martijn Pieters