Update dataframe from csv file when updated

Hello !

I would like to share data between different user on my streamlit cloud app.
Let’s consider this example :


import streamlit as st
import pandas as pd
import os
## User input
number_1 = st.number_input("Number 1 :",value=0)
number_2 = st.number_input("Number 2 :",value=0)

## Button to update values
button_state=st.button('Update !')

if button_state:
	# Storing data as dataframe
	df=pd.DataFrame()
	df.loc['Number 1','Value']=number_1
	df.loc['Number 2','Value']=number_2

	# Showing df
	st.dataframe(df.loc)

	# Saving as csv
	df.to_csv('my_df.csv')

else:
	if os.path.exists('my_df.csv'):
		df=pd.read_csv('my_df.csv')
		st.dataframe(df.loc)

How can I tell streamlit to listen for changes to ‘my_df.csv’ and update the display, so my users can share their values with others ?

Thanks in advance !

Do these users have access to your app?

I have an app where on first load I check the time the file was last updated using pathlib.Path.stat().st_mtime and then store that in session_state as last_update_time.

I then on rerun use the same function and compare the output to last_update_time, and if different I refresh the dataframe from the updated file.

I’m sure it’s far from the most efficient way of doing this but it works.

Yes they have access, i add them with the sharing option :slight_smile:

Good idea indeed, thanks for sharing :slight_smile:
In my case i’m not sure it helps because if I rerun my app, it will reload the csv file, and as it is a light file, it doesn’t consume a lot of time. I just want it to be reloaded without reloading the whole code when changes are noticed.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.