Skip to content

Commit

Permalink
Add profile dump stats option
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosenpfand authored and jeffwidman committed Nov 21, 2023
1 parent 1aedfb0 commit a0de8b6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changes

Enhancements:

- Support to dump profiler stats via `DEBUG_TB_PROFILER_DUMP_FILENAME` by @Dosenpfand in https://github.com/pallets-eco/flask-debugtoolbar/pull/204
- ??

Fixes:
Expand Down
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ Name Description De
``DEBUG_TB_PANELS`` List of module/class names of panels enable all built-in panels
``DEBUG_TB_PROFILER_ENABLED`` Enable the profiler on all requests ``False``, user-enabled
``DEBUG_TB_TEMPLATE_EDITOR_ENABLED`` Enable the template editor ``False``
``DEBUG_TB_PROFILER_DUMP_FILENAME`` Filename of the profiler stats dump, ``None``, no dump will be written
can be a ``str`` or a ``callable``
==================================== ===================================== ==========================

To change one of the config options, set it in the Flask app's config like::
Expand Down
13 changes: 12 additions & 1 deletion src/flask_debugtoolbar/panels/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ProfilerDebugPanel(DebugPanel):
"""
Panel that displays the time a response took with cProfile output.
"""

name = 'Profiler'

user_activate = True
Expand All @@ -22,6 +23,9 @@ def __init__(self, jinja_env, context={}):
DebugPanel.__init__(self, jinja_env, context=context)
if current_app.config.get('DEBUG_TB_PROFILER_ENABLED'):
self.is_active = True
self.dump_filename = current_app.config.get(
"DEBUG_TB_PROFILER_DUMP_FILENAME"
)

def has_content(self):
return bool(self.profiler)
Expand Down Expand Up @@ -88,7 +92,14 @@ def process_response(self, request, response):

self.stats = stats
self.function_calls = function_calls
# destroy the profiler just in case

if self.dump_filename:
if callable(self.dump_filename):
filename = self.dump_filename()
else:
filename = self.dump_filename
self.profiler.dump_stats(filename)

return response

def title(self):
Expand Down

0 comments on commit a0de8b6

Please sign in to comment.