-
-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to enable the toolbar to display on JSON responses? #89
Comments
Jef did you have any luck getting this done? |
No. The way to do it I think, so you don't have to get funky with adding a HTML wrapper around the JSON is detect the content type and have the toolbar append its own bit of JSON with the sqlalchemy queries + page timing info, but thats still a pain methinks compared to just manually setting sqlalchemy to echo queries to stdout and watching your logs as you hit various endpoints. |
Thanks Jeff. |
Workaround:
and
|
I also had to init the FDT like the following to make the above snippet work instead of just
|
@sarimak I add code exactly as yours, the toolbar show up on the page, but nothing pop up when click the menus. |
@guyecode You're right, FDT has changed since I posted the workaround. I can reproduce the issue (FDT for JSON response is displayed but ignores click events -- while it works fine for HTML response) with the following code:
Unfortunately, I am not skilled enough in JavaScript to troubleshoot the issue. |
modify wrap_json.html to:
|
I was also having issues with the click events not working with If you create the after_request before the toolbar extension it will see the html mimetype and work without needing future changes. Here's an extension I wrote for this in my app. """Flask Extensions"""
import flask
import flask_debugtoolbar
class DevToolbar:
"""Add debug toolbars with json to html
Note you must pass `_debug` param to convert the json response
"""
def __init__(self, app):
wrap_json = """
<html>
<head>
<title>Debugging JSON Response</title>
</head>
<body>
<h1>Wrapped JSON Response</h1>
<h2>HTTP Code</h2>
<pre>{{ http_code }}</pre>
<h2>JSON Response</h2>
<pre>{{ response }}</pre>
</body>
</html>
"""
@app.after_request
def after_request(response):
if response.mimetype == "application/json" and \
'_debug' in flask.request.args:
html_wrapped_response = flask.make_response(
flask.render_template_string(
wrap_json,
response=response.data.decode("utf-8"),
http_code=response.status
),
response.status_code,
)
return app.process_response(html_wrapped_response)
return response
flask_debugtoolbar.DebugToolbarExtension(app) |
I am debugging a Flask app that serves up JSON for an API endpoint.
I'm viewing the JSON in the browser, and it'd be convenient to display the toolbar as well.
My understanding of content types is a little hazy... is it possible to add an option to display the toolbar alongside this JSON? Like perhaps a config setting that lets me turn on the toolbar for all responses, not just with
<! doctype html>
?The text was updated successfully, but these errors were encountered: