jinja2 print to console or logging jinja2 print to console or logging python python

jinja2 print to console or logging


I think you can achieve it using filters (http://jinja.pocoo.org/docs/api/#custom-filters) or extensions (http://jinja.pocoo.org/docs/extensions/#adding-extensions). The idea is to just print the filter or extension straight to console.

Not tested but the filter should be something like:

def debug(text):  print text  return ''environment.filters['debug']=debug

To be used as:

...<p>Hello world!</p> {{"debug text!"|debug}}...

Remember to remove the debug on production code!


A similar but slightly different approach using context processor:

In python / flask:

@app.context_processordef utility_functions():    def print_in_console(message):        print str(message)    return dict(mdebug=print_in_console)

In jinja2, Use it anywhere as follows:

{{ mdebug("any text or variable") }}


I would have an HTML element with an id set and the attribute of hidden for the element. Then use JavaScript as such

<p id="hidden-p">{{a_variable}}</p><script>    var hiddenP = document.getElementById("hidden-p").innerHTML;    console.log(hiddenP);</script>