Jinja

python template engine


Ticket #349 (new enhancement)

Opened 9 months ago

Last modified 8 months ago

Make generated Python code importable

Reported by: moraes Owned by: mitsuhiko
Priority: trivial Milestone:
Component: general Version: dev
Keywords: compiler Cc:

Description

Currently the compiled Python code generated by Jinja2 is not importable because it uses a 'environment' variable injected during exec. Wrapping the generated code by a function would make it usable through import, creating a new modality of template loading - import as module.

It's an edge case, but it's super fast and would be particularly useful in App Engine - where we need to use precompiled templates to have a performance comparable to the one provided by the bytecode cache.

The idea: since we are precompiling templates on deployment, why go through compile() and exec while we can simply import the precompiled code as a module and run it? Making the generated code importable would allow this, while keeping other loading methods unaffected.

A proof of concept is here:

http://paste.pocoo.org/show/124381/

And the related discussion is here:

http://groups.google.com/group/pocoo-libs/browse_thread/thread/748b0d2024f88f64?pli=1

Change History

Changed 9 months ago by moraes

Obs: jinja2.debug is the only part uncovered by the proof of concept, afaik.

I tried to fit it, but had a hard time trying to understand what is happening there. :-P

Changed 9 months ago by moraes

Obs2: this started with this tool I created to precompile template directories to use in App Engine:

http://bitbucket.org/moraes/appengine/src/tip/gaefy/jinja2/compiler.py

The saved code is then loaded using a special loader:

http://bitbucket.org/moraes/appengine/src/tip/gaefy/jinja2/code_loaders.py

While it greatly improves performance in App Engine, it is still much slower than using bytecode cache. Importing the templates as modules would be as fast or faster than using bytecode cache.

Changed 8 months ago by moraes

Here's a cleaner version, without touching the __jinja_template__ variable passed in the namespace, and so not modifying globals in the compiled code:

http://paste.pocoo.org/show/131061/

Changed 8 months ago by moraes

Calling the function template() is probably a bad choice, but you get the idea... :-) Here it is back to run(), anyway:

http://paste.pocoo.org/show/131062/

Changed 8 months ago by moraes

Note: See TracTickets for help on using tickets.