1

I'm having problems getting https://github.com/Khan/gae_mini_profiler working correctly in my app.

I've followed the instructions carefully, and this is my app (with needless stuff stripped out.)

import webapp2
from webapp2_extras import jinja2
import appengine_config
import handlers
import gae_mini_profiler.profiler

class MainHandler(handlers.BaseRequestHandler):
    def get(self):
        user = separate_file.get_user_and_logins(self)
        templateid = 'home.html'
        context = {
            'user': user,
        }
        self.render(templateid, context)

app_config = {
    'webapp2_extras.jinja2.default_config': {
        'globals': {
            'profiler_includes': gae_mini_profiler.templatetags.profiler_includes,
         },
     },
}

app = webapp2.WSGIApplication([
  ('/', MainHandler)], config=app_config, debug=True)
app = gae_mini_profiler.profiler.ProfilerWSGIMiddleware(app)

I'm getting this error:

ERROR    2015-05-21 20:23:25,460 wsgi.py:263] 
Traceback (most recent call last):
  File "/Users/jedc/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/Users/jedc/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
  File "/Users/jedc/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
  File "/Users/jedc/mypathtomyapp/main.py", line 33, in <module>
'profiler_includes': gae_mini_profiler.templatetags.profiler_includes,
AttributeError: 'module' object has no attribute 'templatetags'

Is this perhaps because I'm using webapp2 instead of webapp for my main application? Or should I be importing gae_mini_profiler differently? I can't figure it out.

Update

After specifically importing gae_mini_profiler.templatetags, I've now got a new error.

  File "/Users/jedc/Dropbox (Personal)/code/seeddb-devbranch/seeddbapp/main.py", line 22, in get
self.render(templateid, context)
  File "/Users/jedc/Dropbox (Personal)/code/seeddb-devbranch/seeddbapp/handlers.py", line 64, in render
self.response.write(self.jinja2.render_template(template_name, **values))
  File "/Users/jedc/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.1/webapp2_extras/jinja2.py", line 158, in render_template
return self.environment.get_template(_filename).render(**context)
  File "/Users/jedc/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2-2.6/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
  File "templates/base.html", line 118, in template
    {% profiler_includes %}
TemplateSyntaxError: Encountered unknown tag 'profiler_includes'.

and the end of base.html has:

  </div>
  {% profiler_includes %}
  </body>
</html>

Now if I change:

{% profiler_includes %}

to:

{{ profiler_includes }}

it clears the error but doesn't actually seem to trigger the function.

I am not a Jinja2 expert, but am I specifying something incorrectly when I define the function in the webapp2_extras.jinja2.default_config section?

Jed Christiansen
  • 659
  • 10
  • 21

2 Answers2

2

You're missing an import statement. You should explicitly also import gae_mini_profiler.templatetags since you're using it.

import gae_mini_profiler.templatetags
Jeffrey Godwyll
  • 3,787
  • 3
  • 26
  • 37
  • Thanks, Jeffrey! But there also seems to be something in Jinja2 and defining the function for use in the template that is causing an error. Do you have any ideas what could be happening? – Jed Christiansen May 22 '15 at 07:41
  • It's not Jinja2, it's gae_mini_profiler. The instructions there were basically for the python 2.5 runtime. @kamens explains more here http://stackoverflow.com/a/8338910/2295256 – Jeffrey Godwyll May 23 '15 at 00:04
1

Regarding the Update:

This: {{ profiler_includes() | safe }}

instead of this: {% profiler_includes %}

is how I was able to finally use gae_mini_profiler's includes in python2.7 / jinja2...

Mihail Russu
  • 2,526
  • 1
  • 17
  • 27