Zachary,
I tried replacing the button with a toggle as you suggested.
You are correct that when the app reruns after the form is submitted, the data editor does come back.
It is unfortunate that we have to use a toggle and not a button, because its kind of an unusual/unintuitive thing to ask a user to flip a toggle instead of clicking a button to make an action happen. But it is at least a workaround that works. Is it possible to style a toggle to look like a button to make this easier for the user to understand?
Something I notice now, though, is that the app will not rerun unless the rerun() command is added to the form submit button logic. This seems like unexpected behavior, right? Do you have any idea why the change of the input widget type would affect whether the app would rerun on the form submission?
I’ve included the code below. The only thing I changed apart from comments is making the button a toggle and taking the random numbers out of the dataframe.
Thank you
# Import python packages
import datetime as dt;
import streamlit as st;
import pandas as pd;
import numpy as np;
import time;
from snowflake.snowpark.context import get_active_session;
#Set page config.
st.set_page_config(page_title="My App",layout="wide",initial_sidebar_state="expanded");
#Get rid of vertical padding.
st.markdown("""<style>.block-container {padding-top: 0rem;padding-bottom: 0rem;padding-left: 3rem;padding-right: 0rem;}</style>""", unsafe_allow_html=True);
#Get active session.
session = get_active_session();
#Title.
st.write("My App");
#Simulating a db query for selectbox options.
dfOptions=pd.DataFrame({"HELLO" : ["Foo","Bar","Buzz"]});
option=st.selectbox("Option",dfOptions,index=None,key="optionsInput");
search=st.toggle("search");
if(search):
#Simulating another db query that would run on the button click using the selectbox value as a binding variable.
dfData = pd.DataFrame({'A' : ['spam', 'eggs', 'spam', 'eggs'] * 6,
'B' : ['alpha', 'beta', 'gamma'] * 8,
'C' : ['spam', 'eggs', 'spam', 'eggs'] * 6,
'D' : ['alpha', 'beta', 'gamma'] * 8,
'E' : ['spam', 'eggs', 'spam', 'eggs'] * 6});
with st.form("myForm"):
st.data_editor(dfData, key="editor");
submitChanges = st.form_submit_button("Submit Changes");
if(submitChanges):
#Simulate writing back to db the updated rows in the data editor.
dict=st.session_state.editor["edited_rows"];
st.write(dict);
st.success("Update successful");
#Without this, the form does not rerun the app. Unexpected?
# st.rerun();