I’m new to Streamlit and coding and I’m working on a Streamlit application on Snowflake where I’m using st.experimental_data_editor to allow users to view and edit a dataframe.
Once the dataframe is displayed, users can edit the values directly in the st.experimental_data_editor. After making their changes, they can click an ‘Update’ button, which triggers a function that processes the changes and updates the dataframe.
Here’s a simplified version of my code:
def update(df):
# Process changes made by users in the data editor and uptading df with new values
df.at[row_index, 'ALFA'] = new_value
df.at[row_index, 'BETA'] = new_value * 2
return df
with tab_3:
if st.button('Search', key='search_tab3'):
st.session_state.df_table = get_results_tab3(session)
st.experimental_data_editor(st.session_state.df_table, num_rows="dynamic", key='data_tab3')
if st.button('Update', key="update_button"):
if st.session_state.df_table is not None:
st.session_state.df_table = update(st.session_state.df_table)
st.experimental_rerun()
The issue I’m facing is that the changes made by users are not immediately reflected in the displayed dataframe.
I’ve alredy tried using st.empty() to create a placeholder and then update the st.experimental_data_editor widget in the placeholder when the ‘Update’ button is clicked. However, this approach resulted in a DuplicateWidgetID error because the same key was used for the st.experimental_data_editor widget.
Any suggestions or workarounds would be greatly appreciated! Thank you in advance for your help
Hi! I know it, and I alredy used data_editor in local, but I’m working on Snowflake now and it does not support it. If I use data_editor instead of experimental_data_editor it resulted in this error: AttributeError: module ‘streamlit’ has no attribute ‘data_editor’
import streamlit as st
import pandas as pd
from test_query import get_results
from test_update import update
from snowflake.snowpark.context import get_active_session
session = get_active_session()
if 'df_table' not in st.session_state:
st.session_state.df_table = None
if 'search_clicked' not in st.session_state:
st.session_state.search_clicked = False
if 'update_clicked' not in st.session_state:
st.session_state.update_clicked = False
if st.button('Search', key='search_button'):
st.session_state.search_clicked = True
if st.session_state.search_clicked:
st.session_state.df_table = get_results(session)
st.experimental_data_editor(st.session_state.df_table, num_rows="dynamic", key='data_tab')
if st.button('Update', key="update_button"):
st.session_state.update_clicked = True
if st.session_state.update_clicked and st.session_state.df_table is not None:
st.session_state.df_table = update(session, st.session_state.df_table)
st.experimental_data_editor(st.session_state.df_table, num_rows="dynamic", key='data_tab')
This is test_query file:
import pandas as pd
def get_results(session):
query = """ SELECT * FROM EXAMPLE """
df_table = session.sql(query).toPandas()
return df_table
And this id test_update file:
import streamlit as st
import pandas as pd
def update(session, df):
if 'edited_cells' in st.session_state['data_tab']:
edited_rows = st.session_state['data_tab']['edited_cells']
changes_per_row = {int(row.split(":")[0]): {int(row.split(":")[1]): value} for row, value in edited_rows.items()}
for row_index, changes in changes_per_row.items():
row = df.loc[row_index]
for _, new_value in changes.items():
df.at[row_index, 'ALFA'] = new_value
df.at[row_index, 'BETA'] = new_value*2
return df
I've created a simple EXAMPLE table on Snowflake with this schema: EXAMPLE(ALFA, BETA, GAMMA) with ALFA, BETA and GAMMA integer.
As you can see, if I try to update my table I get: DuplicateWidgetID: There are multiple widgets with the same key='data_tab'. I know I can create another widget for my updated table, but I need to update table with key data_tab not to creat another one