sending data as JSON object from Python to Javascript with Jinja


This question already has an answer here:


The Flask Jinja2 documentation covers this pretty well. The first example under the "Standard Filters" section shows exactly how to embed a JSON object from python into a Javascript script:

<script type=text/javascript>
    doSomethingWith({{ user.username|tojson|safe }});

So in this case:

var lat_lng = {{ lat_lng|tojson|safe }};

tojson calls dumps on the data, so you should pass the data directly to the template rather than calling dumps on it, otherwise you double-serialize the data and end up with a JSON string.

Need Your Help

How do you set up use HttpOnly cookies in PHP

php security cookies xss httponly

How can I set the cookies in my PHP apps as HttpOnly cookies?

Kafka how to read from __consumer_offsets topic

apache-kafka kafka-consumer-api

I'm trying to find out which offsets my current High-Level consumers are working off. I use Kafka, with no "" set in the of Kafka - which, I think, means that