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
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”.
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 .
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With