[Add] init_app()-method to DebugPanel base class

Adds a method `DebugPanel.init_app()` that gets called from
`DebugToolbar.init_app()`.

This allows DebugPanels to register their own routes, and do setup work
that should go across requests (as opposed to per-request setup) and
needs access to the Flask `app`-object.
This commit is contained in:
Malthe Jørgensen
2016-06-06 17:10:43 +02:00
parent 719fe02df5
commit 5bf5e093bb
2 changed files with 25 additions and 2 deletions

View File

@@ -25,6 +25,29 @@ class DebugPanel(object):
# If the client enabled the panel
self.is_active = False
@classmethod
def init_app(cls, app):
"""Method that can be overridden by child classes.
Can be used for setting up additional URL-rules/routes.
Example::
class UMLDiagramPanel(DebugPanel):
@classmethod
def init_app(cls, app):
app.add_url_rule(
'/_flask_debugtoolbar_umldiagram/<path:filename>',
'_flask_debugtoolbar_umldiagram.serve_generated_image',
cls.serve_generated_image
)
@classmethod
def serve_generated_image(cls, app):
return Response(...)
"""
pass
def render(self, template_name, context):
template = self.jinja_env.get_template(template_name)
return template.render(**context)

View File

@@ -48,8 +48,8 @@ class DebugToolbar(object):
@classmethod
def load_panels(cls, app):
for panel_class in cls._iter_panels(app):
# just loop to make sure they've been loaded
pass
# Call `.init_app()` on panels
panel_class.init_app(app)
@classmethod
def _iter_panels(cls, app):