How to debug a Flask app

前端 未结 13 2604
陌清茗
陌清茗 2020-11-22 03:08

How are you meant to debug errors in Flask? Print to the console? Flash messages to the page? Or is there a more powerful option available to figure out what\'s happening

13条回答
  •  感动是毒
    2020-11-22 03:59

    One can also use the Flask Debug Toolbar extension to get more detailed information embedded in rendered pages.

    from flask import Flask
    from flask_debugtoolbar import DebugToolbarExtension
    import logging
    
    app = Flask(__name__)
    app.debug = True
    app.secret_key = 'development key'
    
    toolbar = DebugToolbarExtension(app)
    
    @app.route('/')
    def index():
        logging.warning("See this message in Flask Debug Toolbar!")
        return ""
    

    Start the application as follows:

    FLASK_APP=main.py FLASK_DEBUG=1 flask run
    

提交回复
热议问题