Button works for only one final action

Hello everyone,

I have a dataset and I want to delete some rows in the dataset according to the index. For this, I created an interface with the code below:

st.subheader("Edit Your Dataset")
if st.checkbox(label = "Run Edit Engine"):
     idx = st.number_input("Edit by Index", format="%i", value=0, max_value=dataset.shape[0], step=1)
     if st.button("Drop the Row"):
          dataset.drop(index=idx, axis=0, inplace=True)
          st.success("The Record was deleted.")
        
st.write(dataset)

First, I enter the index of the row I want to be deleted and then press the ‘Drop the row’ button:

image

Then the row was deleted:

image

After this process, I want to delete one more row and I enter the index again and click the button:

image

However, in the last table, the row I deleted in the previous process is not actually deleted, only my last process is valid.

How can I edit my dataset using only one button for multiple actions?

hi @tim,
Sorry to bother you. However, I saw that you posted solutions about similar problems. Maybe you know any solution about my problem. I think I can solve my problem with st.cache or SessionState. How can I implement these functions in my code? Could you help me, please?

Hello again,

I solved this problem. I share the final version of the code with the idea that anyone who has the same problem will:

session_state = SessionState.get (df = dataset)
if st.checkbox (label = "Run Edit Engine"):
         with st.form (key = "edit_form"):
             idx = st.number_input ("Edit by Index", format = "% i", value = 0, max_value = dataset.shape [0], step = 1)
             if st.form_submit_button (label = "Drop the Row"):
                 session_state.df.drop (index = idx, axis = 0, inplace = True)
                 st.success ("The Record was deleted.")
                 st.write (session_state.df)
3 Likes