Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

Loading JSON from Python into Google Charts PHP/HTML

I would like to include a JSON string generated from a py file in PHP code for Google Charts.

To generate the JSON string I followed: https://developers.google.com/chart/interactive/docs/dev/gviz_api_lib and the code would end like this:

# Create a JavaScript code string.
jscode_output = data_table.ToJSCode("jscode_data",
                               columns_order=("number", "link quality"),
                               order_by="number")
# Create a JSON string.
json_output = data_table.ToJSon(columns_order=("number", "link quality"),
                           order_by="number")

I then saved the py file as script.py

Next I use the Google Chart line chart code:

  <html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Year', 'Sales', 'Expenses'],
          ['2004',  1000,      400],
          ['2005',  1170,      460],
          ['2006',  660,       1120],
          ['2007',  1030,      540]
        ]);

        var options = {
          title: 'Company Performance',
          curveType: 'function',
          legend: { position: 'bottom' }
        };

        var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="curve_chart" style="width: 900px; height: 500px"></div>
  </body>
</html>

Question: How do I now integrate the script.py file and the json variable into this bit of code? From my understanding the var data line would need to change to this- but I'm not sure if that's correct:

var data = google.visualization.arrayToDataTable(json)

And how do I run the py script? And I would like to run all this code as PHP to display it on a (local) website but I'm not sure what the safest way to do so is in terms of running py scripts in html/php.

question from:https://stackoverflow.com/questions/65865028/loading-json-from-python-into-google-charts-php-html

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This works for me now. Lots of time spent on it as I couldn't find a comprehensive example for a beginner covering this online:

  1. Have the py file save a JSON file:
with open("output.json", "w") as text_file:
    print(json_output, file=text_file)

Here the previously created json_output does not have to be re-encoded into json as it's already in the right format so I am just saving it as a text file.

  1. Create PHP file that calls the JSON file:
<?php
$string = file_get_contents("output.json");
echo $string;
?>
  1. Amend the google chart function to the following:
    function drawChart() {
      
      //Call JSON output from php file
      var jsonData = $.ajax({
          url: "test_phpfile.php", 
          dataType: "json",
          async: false
          }).responseText;

      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(jsonData);
      var options = {
                title: 'Test Chart',
                curveType: 'function'
              }
      // Instantiate and draw our chart, passing in some options.
      var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
      chart.draw(data,options);
    }

Rather than running a py file in php (don't know how to do that yet) I'll now schedule the py file to produce a JSON file every [15]min. Would be great to know what is best practice on this- ultimately I just want to display this chart on a local website.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...