1

I have this issue passing the data from an array variable to the python javascript function. Below is the entire script

 array1 =[['X-val', 'Y-val'], [4, 5], [8, 12], [11, 14]];

self.response.out.write('<script type="text/javascript">'+
                        'google.load("visualization", "1", {packages:["corechart"]});'+
                        'google.setOnLoadCallback(drawChart);'+
                        'function drawChart() { '+
                        'var data = google.visualization.arrayToDataTable({0});' +
                        'var chart = new google.visualization.LineChart(document.getElementById("casestrend"));chart.draw(data);} '+
                        '</script> '.json.dumps(array1))

This is the javascript code which i want to convert to the python format wherein the array required for arrayToDataTable() will be fetched through queries. Currently static version of it (array1 above) doesn't work

<script type="text/javascript">                                                                                       
 google.load("visualization", "1", {packages:["corechart"]});                                                        
 google.setOnLoadCallback(drawChart);                                                                                
 function drawChart() {                                                                                              
 var data = google.visualization.arrayToDataTable([['X-val', 'Y-val'], [4, 5], [8, 12], [11, 14]]);                
 var chart = new google.visualization.LineChart(document.getElementById('chart'));                                 
 chart.draw(data);                                                                                                 
 }                                                                                                                   
 </script>  

Please let me know how to pass the data (array) from the python to the javascript so that the chart gets loaded.

3
  • 1
    What do you mean by Python? Which web framework are you using? Commented Mar 18, 2016 at 9:44
  • 1
    You probably mean '...'.format(json.dumps(..))...?! Commented Mar 18, 2016 at 9:44
  • I am using webapp2 framework for writing python codes. I was working on creating charts using the google charts wherein the data it needs is in array format. Having trouble passing the same from python array to javascript Commented Mar 18, 2016 at 9:56

2 Answers 2

2

Your current solution doesn't work because format (you missed it, right?) calls before strings concatenated.

In current case I would prefer concatenation over format to prevent braces escaping:

js_code = '''
          <script type="text/javascript">
          google.load("visualization", "1", {packages:["corechart"]});
          google.setOnLoadCallback(drawChart);
          function drawChart() { 
          var data = google.visualization.arrayToDataTable(''' + json.dumps(array1) + ''');
          var chart = new google.visualization.LineChart(document.getElementById("casestrend"));chart.draw(data);} 
          </script>
          '''
Sign up to request clarification or add additional context in comments.

3 Comments

Note that this subtly changes the resulting string into a multi-line string. I doubt that it's going to be a problem... just saying.
@mhawke, thanks, that's true. Based on expected output in question it might be what author want.
Thank you so much @germn. Made my day!
2

This will work.

array1 =[['X-val', 'Y-val'], [4, 5], [8, 12], [11, 14]];

self.response.out.write("""<script type="text/javascript">
    google.load("visualization", "1", {{packages:["corechart"]}});
    google.setOnLoadCallback(drawChart);
    function drawChart() {{ 
    var data = google.visualization.arrayToDataTable({0});
    var chart = new google.visualization.LineChart(document.getElementById("casestrend"));chart.draw(data);}}
    </script> """.format(json.dumps(array1)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.