Open jettify opened 9 years ago
Currently trying to port Jinja2 debug panel.
I've added on_jinja2_template_rendered
signal to aiohttp_jinja2:
def setup(app, *args, app_key=APP_KEY, context_processors=(), **kwargs):
...
app.on_jinja2_template_rendered = signals.Signal(app)
...
def render_string(template_name, request, context, *, app_key=APP_KEY):
...
asyncio.ensure_future(app.on_jinja2_template_rendered.send(app, context))
...
and subscribed on it in new debug panel:
class Jinja2DebugPanel(DebugPanel):
"""
A panel to display Jinja2 template variables.
"""
name = 'Jinja2'
has_content = True
template = 'jinja2.jinja2'
title = 'Jinja2'
nav_title = title
def __init__(self, request):
super().__init__(request)
# attach handler
request.app.on_jinja2_template_rendered.append(self.on_template_rendered)
def on_template_rendered(self, app, context):
self.populate(context)
# detach handler
app.on_jinja2_template_rendered.remove(self.on_template_rendered)
def populate(self, context):
self.data = {'variables': []}
for name, value in context.items():
self.data['variables'].append((name, repr(value)))
Works pretty similar to flask-debugtoolbar except they don't need to detach handler each time because there's only one request. I don't like this approach - weird things happens if two or more handlers left attached (e.g. due to exception between panel initialization and template rendering), but can't figure out how/where to put single handler and how to return captured context. Any suggestions?
Calling async signals from sync render_string
looks weird.
I've looked around for cool panel in pyramid/django/flask projects: 1) git history: https://github.com/sjhewitt/django-debug-toolbar-git-panel 2) plot tables relations for sqlalchemy: https://github.com/uralbash/pyramid_debugtoolbar_sadisplay 3) replay ajax calls: https://github.com/jvanasco/pyramid_debugtoolbar_ajax
May be we should add some library like:
aiohttp_debugtoolbar_extras
or own repository for each task.