[파이썬] Flask 앱 디버깅

When developing a Flask application in Python, it’s common to encounter bugs and issues. Debugging is an essential skill that helps identify and solve these problems effectively. In this blog post, we will explore various techniques and tools for debugging Flask applications.

Enable Debug Mode

The first step in Flask app debugging is to enable the debug mode. By default, Flask disables debug mode in production for security reasons. However, in a development environment, enabling debug mode provides helpful error messages and automatic reloading of code changes.

To enable debug mode in Flask, set the debug parameter to True when running the application:

app.run(debug=True)

Alternatively, you can set the FLASK_ENV environment variable to development to enable debug mode:

export FLASK_ENV=development

Logging

Logging is a powerful technique to print messages during program execution. Flask provides built-in logging support through the app.logger object. You can use various methods such as debug(), info(), warning(), error(), and critical() to log messages at different severity levels.

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    app.logger.debug('Debug message')
    app.logger.info('Info message')
    app.logger.error('Error message')
    return 'Hello, Flask!'

By default, the logs will be printed to the console. However, you can configure Flask to save logs to a file as well.

PDB Debugger

The pdb module is a built-in Python debugger that can be used to debug Flask applications. It allows you to step through code, set breakpoints, and inspect variables. To use pdb debugger in Flask, you need to import it and call the set_trace() function at the desired location in your code.

from flask import Flask
import pdb

app = Flask(__name__)

@app.route('/')
def home():
    pdb.set_trace()  # Set breakpoint
    # Code to debug
    return 'Hello, Flask!'

When the Flask app encounters the breakpoint, it will drop into the pdb debugger, enabling you to interactively debug your code.

Flask Debug Toolbar

The Flask Debug Toolbar is a third-party extension that provides a set of panels displaying various debug information about the Flask application. These panels include information about request/response data, template rendering, database queries, and more.

To use Flask Debug Toolbar, you need to install it as a dependency:

pip install flask-debugtoolbar

Then, you can enable it by adding the following code to your Flask app:

from flask import Flask
from flask_debugtoolbar import DebugToolbarExtension

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key'

toolbar = DebugToolbarExtension(app)

@app.route('/')
def home():
    return 'Hello, Flask!'

After enabling the toolbar, you can access it by loading your Flask app in a web browser and appending ?debug_toolbar=true to the URL.

Conclusion

Debugging Flask applications is an essential skill for every Python developer. By enabling debug mode, using logging, and utilizing tools like the pdb debugger or Flask Debug Toolbar, you can effectively identify and resolve issues in your Flask apps. Happy debugging!