Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Debugging of ajax and saving reports #26

wants to merge 3 commits into from

3 participants


This patch makes it possible to debug ajax calls and json/javascript based api calls together with facilities to force debugging and saving reports (slow ones for example) somewhere.

New config variables:

DEBUG_TB_FORCE_DEBUG_PARAMETER - specifies GET parameter to force debugtoolbar activation. Might be used for debugging ajax request and json/javascript-typed responses.

DEBUG_TB_DUMP_CALLBACK - if set enables profiling (but not showing debugtoolbar) for all requests. Specifies function that will be called after each request with toolbar html provided.


Interesting. Yeah, it would be useful to get back to the debugging info for the background AJAX calls. I guess you're using the callback to save the HTML somewhere? What might be nice is to save the last N requests and display that request history within the toolbar itself so that you can more easily browse it.


In my case I use it for both testing of ajax calls and for profiling of API calls wich retur json.
And the callback is used indeed for saving html somewhere so it can be browsed and analized later. In particular it helps catching and investigating slow or heavy requests on live by forcing profiling on one of live servers and saving reports for requests that satisfy some conditions (i.e. take more than x second for example).


I was wondering if there are any plans to either develop this feature further, as it's something I'd definitely be interested in, and I'm sure others are as well :)
For now I'll definitely check out the patch, which surely suffices, but it would be awesome if stats are updated on-the-fly in the debug toolbar.

Other than that, great plugin, it helps me a lot all the time.

@mgood mgood referenced this pull request from a commit
@mgood Move process_response logic into overridable methods
This would allow users to subclass and extend the behavior more easily for
custom processing of the toolbar response. This should fulfill the
extensibility requested in #26, but with more direct control over the response.

I was looking at this again, and I think that rather than hooking into the response processing via configuration values, it might be simpler to move the logic you're asking to extend into methods, so that it could be overridden by a subclass. I've pushed a possible implementation above to the "process-response-hooks" branch. Subclassing would look something like:

class MyToolbar(DebugToolbarExtension):
    def should_process_response(self, response):
        if DebugToolbarExtension.should_process_response(self, response):
            return True
        return my_custom_check

    def insert_toolbar(self, toolbar_html, response):
        # custom processing of toolbar_html ...
        if DebugToolbarExtension.should_process_response(self, response):
            response = DebugToolbarExtension.insert_toolbar(self, toolbar_html, response)
        return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 17, 2012
  1. @evoloshchuk

    Enable debuggiing of ajax calls together with all calls that do not r…

    evoloshchuk authored
    …eturn complete html page
    by explicitly adding configurable parameter to request.
  2. @evoloshchuk
  3. @evoloshchuk
This page is out of date. Refresh to see the latest.
Showing with 44 additions and 12 deletions.
  1. +44 −12 flask_debugtoolbar/
56 flask_debugtoolbar/
@@ -159,22 +159,54 @@ def process_response(self, response):
response.response = [content]
response.status_code = 200
- # If the http response code is 200 then we process to add the
- # toolbar to the returned html response.
- if (response.status_code == 200
- and response.headers['content-type'].startswith('text/html')):
+ app = current_app
+ toolbar_html = None
+ # check if explicitly specified to dump all debug information
+ # force processing of toolbar in this case
+ if app.config.get('DEBUG_TB_DUMP_CALLBACK'):
for panel in self.debug_toolbars[real_request].panels:
panel.process_response(real_request, response)
+ toolbar_html = self.debug_toolbars[real_request].render_toolbar()
+ app.config.get('DEBUG_TB_DUMP_CALLBACK')(toolbar_html)
+ # If the http response code is 200 then we process to add the
+ # toolbar to the returned html response.
+ if response.status_code == 200:
+ debug_all_responses = False
- if response.is_sequence:
- response_html =
- toolbar_html = self.debug_toolbars[real_request].render_toolbar()
+ # check if explicitly specified that toolbar is needed
+ if (app.config.get('DEBUG_TB_FORCE_DEBUG_PARAMETER')
+ and request.args.get(
+ app.config.get('DEBUG_TB_FORCE_DEBUG_PARAMETER'))):
- content = replace_insensitive(
- response_html, '</body>', toolbar_html + '</body>')
- content = content.encode(response.charset)
- response.response = [content]
- response.content_length = len(content)
+ response.headers['content-type'] = 'text/html'
+ debug_all_responses = True
+ # add toolbar only in case if it's text/html response
+ if response.headers['content-type'].startswith('text/html'):
+ if not toolbar_html:
+ for panel in self.debug_toolbars[real_request].panels:
+ panel.process_response(real_request, response)
+ toolbar_html = self.debug_toolbars[real_request].render_toolbar()
+ if response.is_sequence:
+ response_html =
+ if '</body>' in response_html:
+ content = replace_insensitive(
+ response_html, '</body>', toolbar_html + '</body>')
+ elif debug_all_responses:
+ content = response_html + toolbar_html
+ else:
+ # do not mess up ajax request if it's not requested
+ content = response_html
+ content = content.encode(response.charset)
+ response.response = [content]
+ response.content_length = len(content)
return response
Something went wrong with that request. Please try again.