Hi i was trying to populate Google Visualization api in jinja template . I took the sample parameters and passed it to the API but it is converting single and double quotes to ' and & Here is the script :
<script type="text/javascript">
//load the Google Visualization API and the chart
google.load('visualization', '1', {'packages': ['columnchart']});
//set callback
google.setOnLoadCallback (createChart);
//callback function
function createChart() {
//create data table object
var dataTable = new google.visualization.DataTable();
//define columns
dataTable.addColumn('string','Quarters 2009');
dataTable.addColumn('string', 'Earnings');
//define rows of data
// answerjson=answerjson.replace("'",'"');
{% set answerjson1='[["1": "Saturday"], ["6": "Sunday"], ["1": "Wednesday"], ["1": "Monday"], ["1": "Monday"], ["1": "Tuesday"], ["1": "Sunday"]' %}
dataTable.addRows( {{answerjson1}} );
//instantiate our chart object
var chart = new google.visualization.ColumnChart (document.getElementById('chart'));
//define options for visualization
var options = {width: 400, height: 240, is3D: true, title: 'Company Earnings'};
//draw our chart
chart.draw(dataTable, options);
}
</script>
Here is the input passed to the API
Please help me, what i need to do.
Use the safe template filter:
dataTable.addRows( {{ answerjson1 | safe }} );
tojson filter render the data in json form:
dataTable.addRows( {{ answerjson1 | tojson }} );
The safe filter can generate errors if you parse to json after. For example, the safe filter gives a string of a json formed by single quotes, while the JSON.parser() function can only have as input a string with double quotes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With