Flask调试工具栏的面板,用于捕获和查看逐行分析统计信息
Flask-DebugToolbar-LineProfilerPanel的Python项目详细描述
这是flask_debugtoolbar的面板,它允许查看 所选函数的行分析信息。
行配置文件信息来自line_profiler模块,但是 不用担心。
安装
首先,你需要拿到包裹。使用pip安装:
pip install flask-debugtoolbar-lineprofilerpanel
在设置了app.debug = True之后和app.run之前,您需要 指定要使用并包含的flask_debugtoolbar面板 'flask_debugtoolbar_lineprofilerpanel.panels.LineProfilerPanel'在里面 列表。
例如,这里有一个安装了面板和线条的小烧瓶应用程序
为hello_world
:
from flask import Flask app = Flask(__name__) import flask_debugtoolbar from flask_debugtoolbar_lineprofilerpanel.profile import line_profile @app.route('/') @line_profile def hello_world(): return flask.render_template('hello_world.html') if __name__ == '__main__': app.debug = True # Specify the debug panels you want app.config['DEBUG_TB_PANELS'] = [ 'flask_debugtoolbar.panels.versions.VersionDebugPanel', 'flask_debugtoolbar.panels.timer.TimerDebugPanel', 'flask_debugtoolbar.panels.headers.HeaderDebugPanel', 'flask_debugtoolbar.panels.request_vars.RequestVarsDebugPanel', 'flask_debugtoolbar.panels.template.TemplateDebugPanel', 'flask_debugtoolbar.panels.sqlalchemy.SQLAlchemyDebugPanel', 'flask_debugtoolbar.panels.logger.LoggingPanel', 'flask_debugtoolbar.panels.profiler.ProfilerDebugPanel', # Add the line profiling 'flask_debugtoolbar_lineprofilerpanel.panels.LineProfilerPanel' ] toolbar = flask_debugtoolbar.DebugToolbarExtension(app) app.run()
用法
与flask_debugtoolbar附带的常规配置文件面板不同, line profiler将只分析您明确告诉它的函数。你可以 要么将它用作装饰器,要么直接用作函数。
from flask_debugtoolbar_lineprofilerpanel.profile import line_profile # Using it as a decorator @app.route('/profile') @line_profile def profile_page(): ... return flask.render_template('profile_page') # Explicit argument line_profile(some_function)
注意,如果我在上面的示例中完成了line_profile(profile_page),那么 会分析由app.route创建的包装。一般来说,你可能 只想使用line_profile作为装饰。
还要注意,下面将分析装饰器包装,而不是内部 功能。
# Using it incorrectly as a decorator @line_profile @app.route('/profile') def profile_page(): ... return flask.render_template('profile_page')
始终使用@line_profile作为最内部的装饰器。
将line_profiler与google appengine sdk结合使用
line_profiler作为c扩展实现。不幸的是,appengine不支持云中的c扩展,并且dev_appserver在本地计算机上模拟此限制。如果您想在本地计算机上使用line_profiler,可以对appengine sdk进行monkey补丁以允许它。flask debugtoolbar将确保此插件在生产环境中被禁用(它将捕获任何导入程序并禁用受影响的面板)。
只需打开application/__init__.py,应该如下所示:
from __future__ import absolute_import from flask import Flask app = Flask('application') app.config.from_object('application.settings') if app.config['DEBUG']: from werkzeug.debug import DebuggedApplication app.wsgi_app = DebuggedApplication(app.wsgi_app, evalex=True) from flask.ext.debugtoolbar import DebugToolbarExtension toolbar = DebugToolbarExtension(app) import application.urls
然后插入猴子补丁,如下所示:
from __future__ import absolute_import from flask import Flask app = Flask('application') app.config.from_object('application.settings') if app.config['DEBUG']: from werkzeug.debug import DebuggedApplication app.wsgi_app = DebuggedApplication(app.wsgi_app, evalex=True) # We can't use LineProfiler in production because it requires a C-extension, # but we can monkey-patch it in here for use on the dev server: try: import os, sys, re if 'SERVER_SOFTWARE' in os.environ and os.environ['SERVER_SOFTWARE'].startswith('Dev'): # white-list the line_profiler C extension sys.meta_path[3]._enabled_regexes.append(re.compile(r'.*line_profiler.*')) from flask_debugtoolbar_lineprofilerpanel.profile import line_profile ## import the methods you want to profile here, and whitelist them with line_profile: #from application.views import YourViewClass # #line_profile(YourViewClass.the_method_you_want_to_profile) #line_profile(YourViewClass.another_method_you_want_to_profile) except: pass # Make sure the monkey-patch is applied before you instantiate the DebugToolbarExtension. from flask.ext.debugtoolbar import DebugToolbarExtension toolbar = DebugToolbarExtension(app) import application.urls
1.0.1(2013-11-12)
- 将其设为>;=1.0.0,因此不需要–pre标志来安装包
- (这是1.0.1而不是1.0.0,因为我不擅长做事情)
- 指定Line Profiler的版本1.0b3以使安装工作
0.0.6(2012-11-13)
- 不再污染模板命名空间
- 显示所有行,而不仅仅是包含分析信息的行
0.0.5(2012-11-13)
- 表输出,放弃lineprofiler.print_stats()提供的默认输出
- 面板中的使用说明
0.0.1(2012年11月12日)
- 开始工作了
- 超级丑