Multiuser streamlit app refreshes on data change by another

Hello All,

I recently developed an streamlit app using “Streamlit in Snowflake” and found that if 2 users login separately on different machine and editing the data, On submit from one user, data refreshes (Page itself) on another user screen who is currently updating and yet to submit.
Although, I’ve handled it using session state to store the whole data frame and it comes back as is on auto refresh but looking for the reason as well as best way to handle that if not session state.

Below is a basic sample code I tried to test on 2 users (parallelly). App updates a table with existing data “temp_test(code, value)”


import streamlit as st
from snowflake.snowpark.context import get_active_session
import pandas as pd
from snowflake.snowpark.functions import col

def main():
st.set_page_config(layout=“wide”)
st.title(“:green[Test]”)

session = get_active_session()

tableName = 'PUBLIC.temp_test'
dataframe = (session.table(tableName).select(col('CODE'),col('VALUE'))).to_pandas()

dataframe_init = dataframe
dataframe_init.insert(0, 'SELECT', False)
edited_df = st.data_editor(dataframe_init)

if st.button('Update'):
    df_toapprove = edited_df.loc[edited_df['SELECT'] == True,['CODE','VALUE']]
    query = "update "+str(tableName)+" set VALUE = '"+str(df_toapprove['VALUE'].iloc[0])+"' where CODE = '"+df_toapprove['CODE'].iloc[0]+"'"
    session.sql(query).collect()
    st.write(query)

if name == “main”:
main()

Hi @Saurabh_Sethi

Have you tried putting those widgets behind a st.form() so that changes will only apply after the form has been submitted.

I tried putting the sample code in the form, It doesn’t refresh the data now (CODE & VALUE) but if same row is being updated from 2 different users, second update will throw “IndexError: single positional indexer is out-of-bounds” error, looks like form isn’t preserving the selections for Boolean column as its getting unchecked.

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