我需要實時顯示圖形中的動態數據。 因此,我需要每秒鐘繪製一張更新數據的新圖。Python空cgi瀏覽器屏幕
的問題是,我不能清空以前的輸出在瀏覽器 等,而不是得到一個簡單的圖形與更新的數據 我得到很多的圖表
的這裏是代碼。目前它有隨機數據
def graph(data):
X,Y = 500, 275 #image width and height
img = Image.new("RGB", (X,Y), "#FFFFFF")
draw = ImageDraw.Draw(img)
#draw some axes and markers
for i in range(X/10):
draw.line((i*10+30, Y-15, i*10+30, 20), fill="#DDD")
if i % 5 == 0:
draw.text((i*10+15, Y-15), `i*10`, fill="#000")
for j in range(1,Y/10-2):
draw.text((0,Y-15-j*10), `j*10`, fill="#000")
draw.line((20,Y-19,X,Y-19), fill="#000")
draw.line((19,20,19,Y-18), fill="#000")
for i in range(1,len(data)):
draw.line((i+20,Y-20,i+20,Y-20-data[i]), fill="#000")
rtime = str(time.time())
img.save("../images/out.png"+rtime, "PNG")
htmlBody = []
htmlBody.insert(0, 'Content-type: text/html; charset=UTF-8\n')
htmlBody.append('<html>')
htmlBody.append('<body>')
htmlBody.append('<img src=\"../images/out.png'+rtime+'\" alt=\"\" />')
htmlBody.append("</body>")
htmlBody.append('</html>')
sys.stdout.flush()
sys.stdout.write('\n'.join(htmlBody))
sys.stdout.flush()
data = []
if (printgraph!=None):
sys.stdout = sys.stderr = oldsysout
for i in range(1,5):
data.append(int(250*(random.random())))
graph(data)
sys.stdout.flush()
time.sleep(1)
exit()