Skip to content

Development

First steps

Before writing your own panel you need to provide a Jinja loader instance used to load your templates from the file system or other locations.

from debug_toolbar.middleware import DebugToolbarMiddleware
from fastapi import FastAPI
from fastapi.templating import Jinja2Templates

app = FastAPI(debug=True)
templates = Jinja2Templates(directory="templates")

app.add_middleware(
    DebugToolbarMiddleware,
    panels=["panels.ExamplePanel"],
    jinja_loaders=[templates.env.loader],
)

Create a panel

Subclass Panel and override generate_stats() method to implement a custom panel on your panels.py. This method should return a dict with the panel stats.

from debug_toolbar.panels import Panel


class ExamplePanel(Panel):
    title = "Example Panel"
    template = "example.html"

    async def process_request(self, request):
        response = await super().process_request(request)
        return response

    async def generate_stats(self, request, response):
        return {"example": "value"}

Tip

The process_request() method is optional and particularly useful for adding behavior that occurs before the request is processed.

Please see the Panel class reference for further details.

Writing the template

Create a template at templates/example.html to display your panel stats:

<span>{{ example }}</span>