Python 如何使Streamlit每5秒重新加载一次?

Python 如何使Streamlit每5秒重新加载一次?,python,streamlit,Python,Streamlit,我必须每5秒重新加载一个Streamlight图表,以便在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 = 'Pr

我必须每5秒重新加载一个Streamlight图表,以便在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)

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)
使用Streamlight图表代码将以下行添加到脚本:从虚拟导入*

运行refresher.py脚本

使用图表运行Streamlight脚本

Streamlit从现在起将把dummy.py文件中的任何修改识别为源代码中的修改

网页完全加载后,它会提醒您源文件已更改。点击Alway rerun,就这样


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)
使用Streamlight图表代码将以下行添加到脚本:从虚拟导入*

运行refresher.py脚本

使用图表运行Streamlight脚本

Streamlit从现在起将把dummy.py文件中的任何修改识别为源代码中的修改

网页完全加载后,它会提醒您源文件已更改。点击Alway rerun,就这样


当我运行此应用程序时,我的Streamlight应用程序只会旋转而从不加载-可能是更新破坏了此功能?@OrionthHunter您可能错过了虚拟导入的导入*。它仍然在版本0.66.0上工作。它在我的本地计算机上工作。但是,当我将其部署到prod时,它每天接收10k+的浏览量,我只看到页面加载。当我运行此应用程序时,我的Streamlight应用程序只是旋转,从未加载-可能是更新打破了这一点?@OrionHander您可能错过了虚拟导入的导入*。它仍然在版本0.66.0上工作。它在我的本地计算机上工作。但是,当我将它部署到prod上时,我只看到页面加载,prod每天接收超过10k的浏览量。