使用python显示谷歌图表,每10秒刷新一次数据



我需要使用python显示谷歌图表,每10秒随机2个整数,将整数替换到图表中,然后在浏览器上显示。

这是我的代码,我是python的新手,不知道我的代码出了什么问题

import random
import time
while True:
f = open('message.html', 'w')
message1 = '<html><head><script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script><script type="text/javascript">'
message2 = "google.charts.load('current', {'packages':['corechart']});google.charts.setOnLoadCallback(drawChart);function drawChart() {var data = google.visualization.arrayToDataTable([          ['Task', 'Hours per Day'],"
message3 = "['Work', "
message4 = "],           ['Sleep',"
message5 = "]]);var options = {title: 'My Daily Activities'};var chart = new google.visualization.PieChart(document.getElementById('piechart'));chart.draw(data, options);}</script></head><body>"
message6 = '<div id="piechart" style="width: 900px; height: 500px;"></div></body></html>'
a = random.randint(0, 101)
b = random.randint(0, 101)
message = message1 + message2 + message3 + str(a) + message4 + str(b) + message5 + message6
f.write(message)
time.sleep(10)
f.close()

我试着在终端中打印a和b,它完全随机,但浏览器上没有显示图表。

您可以在那里写,并在10s:后重新加载html以查看结果

f.write(message)
f.close()
time.sleep(10)

最新更新