Can you help me get jinja2 to render {{thing.a}}

9 views
Skip to first unread message

Craig Burton

unread,
Feb 13, 2018, 8:14:24 AM2/13/18
to pylons-discuss
Hi there, if you can help me with this it will save what's left of my hair.

I want to have a jinja2 template render with variables passed in from an external program.  The views.py has this in it

@view_config(route_name='stats', renderer='templates/stats.jinja2')
def my_view_stats(request):
return {'r': subprocess.check_output(['program_that_returns_json']),
'page': 'My Stats', 'next' : '/hello', 'prev': '/paydown'}

program_that_returns_json returns this kind of thing {'on_target' : 3425}

The template stats.jinja2 has 
We are this much on target: {{r.on_target}} 
...
The next page is {{next}}

{{next}} gets rendered but {{r.on_target}} does not.

The technique above works for me when renderer='json' but I need javascript to unpack the response and jquery to set content, variable by variable.  Here I have a large list of them.  Can it work like I have guessed or ?
Cheers
Craig.

Michael Merickel

unread,
Feb 13, 2018, 8:39:34 AM2/13/18
to Pylons
Craig,

The subprocess is returning a json-encoded string. This is a string. You must parse it first via json.loads(subprocess.check_output(...)) if you want to use the objects it contains in your template. Similarly you said it's working with renderer="json" but it's actually not. You're json-encoding a json-encoded string. You'll see if you try to use that response that it is double encoded and you will need to double-decode it. You'll want to use the same solution in that case via json.loads() or you can just pass through the check_output content as the response body by making a response yourself instead of using the renderer.

- Michael

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/7541dfdb-307f-46a6-9c0e-1c755fc64295%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Craig Burton

unread,
Feb 13, 2018, 8:58:34 AM2/13/18
to pylons-discuss
Thank you Michael this did indeed fix it.
Works a charm now, cheers.

This does already works for me though - not sure how?  Perhaps the json renderer parses the string returned by subprocess?
Or jinja2 deparses it twice?  In the previous example I noticed that merely putting {{r}} on a page dumped the entire "json" string with the quotes marked up to unicode before the change you suggested.

@view_config(route_name='rachel_content', renderer='json')
def rachel_content(request):
 
return {'rachel': subprocess.check_output('program_that_accepts_and_returns_json',json.dumps(request.json_body)])
 
}


On Wednesday, February 14, 2018 at 12:39:34 AM UTC+11, Michael Merickel wrote:
Craig,

The subprocess is returning a json-encoded string. This is a string. You must parse it first via json.loads(subprocess.check_output(...)) if you want to use the objects it contains in your template. Similarly you said it's working with renderer="json" but it's actually not. You're json-encoding a json-encoded string. You'll see if you try to use that response that it is double encoded and you will need to double-decode it. You'll want to use the same solution in that case via json.loads() or you can just pass through the check_output content as the response body by making a response yourself instead of using the renderer.

- Michael
On Tue, Feb 13, 2018 at 6:03 AM, Craig Burton <craig.alexa...@gmail.com> wrote:
Hi there, if you can help me with this it will save what's left of my hair.

I want to have a jinja2 template render with variables passed in from an external program.  The views.py has this in it

@view_config(route_name='stats', renderer='templates/stats.jinja2')
def my_view_stats(request):
return {'r': subprocess.check_output(['program_that_returns_json']),
'page': 'My Stats', 'next' : '/hello', 'prev': '/paydown'}

program_that_returns_json returns this kind of thing {'on_target' : 3425}

The template stats.jinja2 has 
We are this much on target: {{r.on_target}} 
...
The next page is {{next}}

{{next}} gets rendered but {{r.on_target}} does not.

The technique above works for me when renderer='json' but I need javascript to unpack the response and jquery to set content, variable by variable.  Here I have a large list of them.  Can it work like I have guessed or ?
Cheers
Craig.

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages