使用python显示google图表并每10秒刷新一次数据

使用python显示google图表并每10秒刷新一次数据,python,Python,我需要显示谷歌图表使用python和随机2整数每10秒,替换成图表中的整数,然后显示在浏览器上 这是我的代码,我是python新手,不知道我的代码出了什么问题 import random import time while True: f = open('message.html', 'w') message1 = '<html><head><script type="text/javascript" src="

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

这是我的代码,我是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()
随机导入
导入时间
尽管如此:
f=open('message.html','w')
消息1=''
message2=“google.charts.load('current',{'packages':['corechart']});google.charts.setOnLoadCallback(drawChart);function drawChart(){var data=google.visualization.arrayToDataTable([['Task',Hours per Day'])”
message3=“[‘工作’,”
message4=“]”,[“睡眠”
message5=“]”);var options={title:'My Daily Activities'};var chart=new google.visualization.PieChart(document.getElementById('PieChart');chart.draw(数据,选项);}”
消息6=“”
a=random.randint(0,101)
b=random.randint(0,101)
message=message1+message2+message3+str(a)+message4+str(b)+message5+message6
f、 写(信息)
时间。睡眠(10)
f、 关闭()

我试图在终端中打印a和b,结果非常随机,但图表没有显示在浏览器上。

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

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

它起作用了!非常感谢,我的逻辑很糟糕。