Skip to content

Commit

Permalink
Add init_app() method to DebugPanel base class (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
macnewbold authored Dec 20, 2023
2 parents 719fe02 + 5bf5e09 commit b7f5a72
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/flask_debugtoolbar/panels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ def __init__(self, jinja_env, context={}):
# 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)
Expand Down
4 changes: 2 additions & 2 deletions src/flask_debugtoolbar/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def render_toolbar(self):
@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):
Expand Down

0 comments on commit b7f5a72

Please sign in to comment.