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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…