Why streamlit app is reseting whole app when giving input in any field


import streamlit as st
import pandas as pd

# Mocking the sheet
sheet = pd.DataFrame({
    'product_id': [1, 2, 3, 2, 3, 1],
    'seller_category': ['A', 'B', 'C', 'D', 'E', 'F'],
    'storeid': [101, 102, 103, 104, 105, 106]
})

# Initialize session state
if 'search_results' not in st.session_state:
    st.session_state.search_results = None

# Create the main window
st.title("Sheet Entry")

# Sidebar
st.sidebar.title("Actions")
action = st.sidebar.selectbox("Select an action:", ["Enter New Data", "Update Existing Data", "Copy Existing Data"])

# Check the selected action
if action == "Update Existing Data":
    st.title("Update Existing Data")

    search_column = st.selectbox("Search Column", ["product_id", "seller_category", "storeid"])
    search_keyword = st.text_input("Search Keyword")
    if st.button("Search"):
        search_results = sheet[sheet[search_column].astype(str).str.contains(search_keyword, case=False, na=False)]

        if not search_results.empty:
            st.write("Search Results:")
            st.write(search_results)

            # Select row to update
            selected_row_index = st.number_input("Enter the index of the row to update:", min_value=0, max_value=len(search_results)-1, value=0, step=1)

            # Display the selected row's data for editing
            st.subheader("Edit Selected Row")
            original_index = search_results.index[selected_row_index]

            if st.session_state.search_results is None:
                st.session_state.search_results = search_results.copy()

            # Create a dictionary to store the edited values
            edited_values = {}
            for column in search_results.columns:
                value = search_results.at[original_index, column]
                edited_value = st.text_input(f"{column.capitalize()}:", value=value, key=f"{column}_{original_index}")
                edited_values[column] = edited_value

            # Update the DataFrame and Sheet
            if st.button("Update"):
                for column, edited_value in edited_values.items():
                    sheet.at[original_index, column] = edited_value
                st.success("Data updated successfully!")
        else:
            st.warning("No results found")

this is my code what i want to do is:-
Select > Update Existing Data > search any number 1,2,3 > you will see 2 search result > by default first row data will come in input fields but if you edit any field app refresh and reset and same thing is happening when selecting second row

this is a demo app and it basically search rows and then it will edit that row and update same row in sheet

kindly help me here how to solve this reset and how it will work as desired that it will update same row in sheet when hit update button.

Thanks in advance.

Read and understand the main concept of streamlit, especially the data flow. This is a must for any developer.

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