orf / django-debug-toolbar-template-timings

A django-debug-toolbar panel that displays template rendering times for your Django application
https://pypi.python.org/pypi/django-debug-toolbar-template-timings
MIT License
297 stars 28 forks source link

Any chance of wrapping Jinja? #20

Closed kylemacfarlane closed 8 years ago

kylemacfarlane commented 10 years ago

I was pretty surprised at how slow the Django templates could get. I popped in some Jinga templates using django-jinga and saw 2300ms drop to 290ms. However I couldn't see it yet in the panel.

It was pretty easy to wrap the basic Jinga rendering but I'm not sure about wrapping blocks.

The import:

try:
    from jinja2.environment import Environment
    jinja2 = True
except ImportError:
    jinja2 = False

The new name getter (pass in the args and kwargs from timing_hook).

    def name(self, args, kwargs):
        if hasattr(self, 'name'):
            name = self.name
        elif jinja2:
            if len(args) > 1:
                name = args[1]
            else:
                name = kwargs.get('name')
        return name or ''

The wrapper:

        if jinja2:
            Environment.compile = _template_render_wrapper(
                Environment.compile, "templates",
                lambda n: not any([re.match(pattern, n) for pattern in TEMPLATE_TIMINGS_SETTINGS["IGNORED_TEMPLATES"]])
            )

Also I was surprised at how plug and play django-jinga is and the immediate results. It uses a middleware to redirect all templates ending with e.g "j.html" to Jinga and so you don't have to redo all your templates at once.

orf commented 10 years ago

I like the look of this and will definitely add it. If you have some working code could you make a pull request? If not I will have a crack.

kylemacfarlane commented 10 years ago

I was just playing around with the code.

I think I might have overridden the wrong method. Environment.compile compiles the template whereas Template.render actually renders it.

kylemacfarlane commented 10 years ago

This seems like it'd be harder than first thought. Jinja compiles together templates and so you lose visibility of includes.

You'd probably need to inject some kind of custom template tag (global function in Jinja) to record timings. Such a tag would probably be useful in regular Django templates too.

orf commented 10 years ago

Or there could be a way to hook into Jinja to insert hooks before and after template includes while compiling, I'm not sure how hard that would be On 13 Feb 2014 03:30, "Kyle MacFarlane" notifications@github.com wrote:

This seems like it'd be harder than first thought. Jinja compiles together templates and so you lose visibility of includes.

You'd probably need to inject some kind of custom template tag (global function in Jinja) to record timings. Such a tag would probably be useful in regular Django templates too.

Reply to this email directly or view it on GitHubhttps://github.com/orf/django-debug-toolbar-template-timings/issues/20#issuecomment-34945240 .

tbabej commented 10 years ago

Bumping - any chance of getting Jinja2 support? (I'd love the feature!)

auvipy commented 8 years ago

as django now officially supports jinja2 template backend

dorianamouroux commented 8 years ago

Still no support for jinja 2 ?

orf commented 8 years ago

Hi, this plugin was written by me a long time ago to fill a particular need. I open sourced it and it seems it became moderately popular, but I haven't used it in 3+ years.

If anyone wants to step in and add this feature, or work on it in any way, then I will always review and merge suitable PRs. Until them I'm afraid I don't have the time or the energy to add this.