本节讲解 Flask调试工具栏 。
Flask Debug Toolbar 是一个 Flask 扩展,可以帮助您调试应用程序。它在视图中添加了一个调试工具栏,它提供有关 HTTP 标头,请求变量,配置设置以及呈现特定视图所需的 SQLAlchemy 查询数的信息。您可以使用此信息来查找视图呈现中的瓶颈。
pip install flask-debugtoolbar
|
pip freeze > services/users/requirements.txt
|
要启用,创建工具栏的实例,然后将它添加到应用程序 create_app()
中的 services/users/project/__init__.py
:
import os
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_debugtoolbar import DebugToolbarExtension
db = SQLAlchemy() toolbar = DebugToolbarExtension()
def create_app(script_info=None):
app = Flask(__name__)
app_settings = os.getenv('APP_SETTINGS') app.config.from_object(app_settings)
db.init_app(app) toolbar.init_app(app)
from project.api.users import users_blueprint app.register_blueprint(users_blueprint)
@app.shell_context_processor def ctx(): return {'app': app, 'db': db}
return app
|
services/users/project/config.py
:
import os
class BaseConfig: """Base configuration""" TESTING = False SQLALCHEMY_TRACK_MODIFICATIONS = False SECRET_KEY = 'my_precious' DEBUG_TB_ENABLED = False DEBUG_TB_INTERCEPT_REDIRECTS = False
class DevelopmentConfig(BaseConfig): """Development configuration""" SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') DEBUG_TB_ENABLED = True
class TestingConfig(BaseConfig): """Testing configuration""" TESTING = True SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_TEST_URL')
class ProductionConfig(BaseConfig): """Production configuration""" SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
|
更多的配置选项可以查看文档 。
services/users/project/tests/test_config.py
:
import os import unittest
from flask import current_app from flask_testing import TestCase
from project import create_app
app = create_app()
class TestDevelopmentConfig(TestCase): def create_app(self): app.config.from_object('project.config.DevelopmentConfig') return app
def test_app_is_development(self): self.assertTrue(app.config['SECRET_KEY'] == 'my_precious') self.assertFalse(current_app is None) self.assertTrue( app.config['SQLALCHEMY_DATABASE_URI'] == os.environ.get('DATABASE_URL') ) self.assertTrue(app.config['DEBUG_TB_ENABLED'])
class TestTestingConfig(TestCase): def create_app(self): app.config.from_object('project.config.TestingConfig') return app
def test_app_is_testing(self): self.assertTrue(app.config['SECRET_KEY'] == 'my_precious') self.assertTrue(app.config['TESTING']) self.assertFalse(app.config['PRESERVE_CONTEXT_ON_EXCEPTION']) self.assertTrue( app.config['SQLALCHEMY_DATABASE_URI'] == os.environ.get('DATABASE_TEST_URL') ) self.assertFalse(app.config['DEBUG_TB_ENABLED'])
class TestProductionConfig(TestCase): def create_app(self): app.config.from_object('project.config.ProductionConfig') return app
def test_app_is_production(self): self.assertTrue(app.config['SECRET_KEY'] == 'my_precious') self.assertFalse(app.config['TESTING']) self.assertFalse(app.config['DEBUG_TB_ENABLED'])
if __name__ == '__main__': unittest.main()
|
更新容器并运行测试:
$ docker-compose -f docker-compose-dev.yml up -d --build $ docker-compose -f docker-compose-dev.yml run users python manage.py test
|
浏览器导航到 http://localhost 将看到工具栏:
![](https://raw.githubusercontent.com/keer2345/storehouse/master/hexo/images/2018/0923/005.png)