我必须每5秒重新加载一次Streamlit图表,以便在XLSX报表中可视化新数据。 如何实现呢?
import streamlit as st
import pandas as pd
import os
mainDir = os.path.dirname(__file__)
filePath = os.path.join(mainDir, "sources\\test.xlsx")
df = pd.read_excel(filePath)
option1 = 'Product'
option2 = 'Quantity'
df = df[[option1, option2]].set_index(option1)
st.write('Product Quantity')
st.area_chart(df)
答案 0 :(得分:0)
Streamlit每次识别您的源代码都会识别。因此,基于此前提,一种实现此目的的方法是创建一个空的“ dummy.py”文件,该文件将由主脚本导入,并由同时运行的另一个脚本每5秒更新一次:
在主脚本的同一文件夹内创建一个空的“ dummy.py”文件;
在同一文件夹中创建一个名为“ refresher.py”的脚本;
现在将以下While循环放入“ refresher.py”文件中,以便每5秒更新一次带有随机数(已注释)的“ dummy.py”:
from random import randint
import time
import os
def refresher(seconds):
while True:
mainDir = os.path.dirname(__file__)
filePath = os.path.join(mainDir, 'dummy.py')
with open(filePath, 'w') as f:
f.write(f'# {randint(0, 10000)}')
time.sleep(seconds)
refresher(5)
使用您的Streamlit图表代码将以下行添加到脚本中:“”“来自虚拟导入*”“”“
运行“ refresher.py”脚本;
使用图表运行Streamlit脚本;
从现在开始,Streamlit会将“ dummy.py文件”中的所有修改识别为源代码中的修改;
网页完全加载后,它将提醒您源文件已更改。点击“ Alway rerun”,就这样。