Our key application of this tool would be a live dashboard reading data from sensors and continuously updating their state and history. In order to do that I want the page to automatically update every second or so.
I can get this to work by abusing a while loop like this:
state = run(read_plc())
json_view = st.json(state)
line_chart = st.line_chart([{key: val for key, val in state.items()}])
while True:
time.sleep(1)
state = run(read_plc())
json_view.json(state)
line_chart.add_rows([{key: val for key, val in state.items()}])
But that feels like itās abusing the abstraction a bit. Is there a better way to achieve this behavior? Iād love a method to specify an update frequency.
Hi @JamesJeffryes, this is a very interesting use-case. The code you are suggesting will work, but we are working at providing first-class support for dashboards. To give you some context, by wrapping your logic inside the while True loop, Streamlit will not release resources (thread and connection) while a user is connected. Streamlit will release resources once the user disconnects. Depending on your specifics this may or may not be acceptable. In general, we think that the code of a Streamlit App should run to completion and re-runs should be used on changes.
Unfortunately, it is not possible at the moment to programmatically trigger re-runs, but we are investigating whether it makes sense to implement a mechanism to allow that. By programmatically triggering a re-run, you would be able to control when re-executing and pulling new data. If you are interested, I can provide a gist on top of Streamlit to try that out and definitely I will keep you posted on the progress of this feature.
Iād definitely be interested in programmatic triggers for re-runs. Streamlet seems to abstract away web concepts better than any other python library Iāve played with so I think there could be real interest in using it to make simple interfaces for IoT devices. Not releasing resources while the user is connected would likely be fine for our use cases, especially if the websocket is reconnecting.
It uses two other gists that we are considering merging into Streamlit, but you should be able to use them right now. One gist implements reruns, the other one implements SessionState. The gist above has the links to the other two.
Hello,
I am trying to develop a dashboard too. However, I would like to keep the sensor data collection isolated from the dashboard. So I spawn them within two different processes. The code runs without errors but does not output anything? How to use streamlit within a subprocess? Or what is the best way to isolate these two parts. Code snippet:
def collect_data():
while True:
s = read_sensor_data()
send_data_to_db()
def dashboard():
while True:
df = pd.read_sql(...)
st.dataframe(df)
p1 = mp.Process(collect_data())
p2 = mp.Process(dashboard())
p1.start()
p2.start()
p1.join()
p2.join()
Result: An the default webpage spawned by streamlit.
I have the same question, I donāt know that you solved your problem and maybe help me because Iām new in streamlit and tried to implement a solutions that found in web but I canāt solve the application.
In my aplication I designed a detector based on FPGA that send the monitoring of a sensors through serial port and is proccessing and storage with a script in python. This script create a csv file that is updated each time that the FPGA send a monitoring frame.
I wrote a other script to use streamlit to show the data of the monitoring and I push the option to load a old file or detect a new file(this files is the csv thats created in real time when my system is on operation and its updated each 2-3 seconds) .
I share the code that use for the select the source and load the data:
st.sidebar.title(āSettingsā)
if not st.sidebar.checkbox(āLive Dataā, True):
st.sidebar.markdown(āChoose the data file csvā)
folder_path = path_script = os.path.abspath(os.getcwd())
filenames = os.listdir(folder_path )
selected_filename = st.selectbox(āSelect a fileā, filenames)
DATA_URL=os.path.join(folder_path + ā\ā + selected_filename)
st.write('You selected `%s`' % DATA_URL)
else:
list_of_files = glob.glob(ā*.csvā)
#print(list_of_files)
LASTEST_FILE = max(list_of_files, key=os.path.getctime)
#print(latest_file)
path_script = os.path.abspath(os.getcwd())
DATA_URL = path_script + ā\ā+ LASTEST_FILE
def load_data():
update_timestamp = time.ctime(os.path.getmtime(DATA_URL))
st.write(update_timestamp)
return cached_data_load(update_timestamp)
@st.cache(ttl=60)
def cached_data_load(timestamp):
data = pd.read_csv(DATA_URL)
return data
data = load_data()
#%%
st.markdown("### Rate vs Time")
f1 = alt.Chart(data).mark_circle().encode(
x=alt.X(āTimeā, axis=alt.Axis(title=āTimeā)),
y=alt.Y(āRateā, axis=alt.Axis(title=āRate [s^-1]ā)),
color=āRateā
)
st.altair_chart(f1, use_container_width=True)
The dataframe is update only when I pushed a checkbox implemented on script
Could you help me please to understand why the app.py donāt update the dataframe and graph please?
I am also interested in this - in my case to have a display from a sqlite db which is continually capturing sensor data. Any ideas @randyzwitch or others?
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking āAccept allā, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.