I am not able to edit the values

import streamlit as st
import pandas as pd

def create_new_entry(state, key, value):
if (key, value) not in state.key_value_pairs:
state.key_value_pairs.append((key, value))
else:
st.warning(“This entry already exists!”)

def delete_entry(state, index):
if 0 <= index < len(state.key_value_pairs):
state.key_value_pairs.pop(index)
else:
st.warning(“Invalid index for deletion.”)

def main():
st.title(“Enter the Quality Standards”)

if "key_value_pairs" not in st.session_state:
    st.session_state.key_value_pairs = []

with st.form("dict_form"):
    st.write("Enter Data:")
    key_input = st.text_input("Description")
    value_input = st.text_input("Desired Value")
    submitted = st.form_submit_button("Add data")

    if submitted and key_input and value_input:
        create_new_entry(st.session_state, key_input, value_input)
        st.success("Pair added to dictionary!")

st.write("---")
st.write("Created Dictionary:")

# Represent entered values in a table
data = {"Index": list(range(1, len(st.session_state.key_value_pairs) + 1)),  # Start from 1
        "Description": [key for key, _ in st.session_state.key_value_pairs],
        "Desired Value": [value for _, value in st.session_state.key_value_pairs]}
df = pd.DataFrame(data)
st.table(df.set_index("Index"))  # Set the index for display

# Layout for edit and delete buttons
col1, col2 = st.columns(2)

with col1:
    edit_button_clicked = st.checkbox("Edit an Entry")
    if edit_button_clicked:
        selected_description = st.selectbox("Select an entry to edit:",
                                            ["Select an entry"] + df["Description"].tolist())
        if selected_description != "Select an entry":
            index_to_edit = df[df["Description"] == selected_description].index[0]
            new_key = st.text_input("New Description", selected_description)
            new_value = st.text_input("New Desired Value", df.loc[index_to_edit, "Desired Value"])
            if st.button("Update"):
                df.at[index_to_edit, "Description"] = new_key
                df.at[index_to_edit, "Desired Value"] = new_value
                st.success(f"Pair updated: {selected_description} -> {new_key}, {new_value}")

with col2:
    delete_button_clicked = st.checkbox("Delete an Entry")
    if delete_button_clicked:
        selected_index = st.selectbox("Select an entry to delete:",
                                      ["Select an entry"] + df["Description"].tolist())
        if selected_index != "Select an entry":
            index_to_delete = df[df["Description"] == selected_index].index[0]
            if st.button("Confirm Delete"):
                delete_entry(st.session_state, index_to_delete)
                st.success(f"Pair {selected_index} deleted!")
                st.experimental_rerun()  # Trigger re-run to immediately reflect changes

if name == “main”:
main()
In this code when i choose edit an entry and then click on update, the changes that i did are not reflecting in my entered value. What could be the cause and how can i change that

Like when you are updating those values they are updating in dataframe but the table uses session state which is not being updated so there is a mismatch between actual data and session state

Here is the working code

import streamlit as st
import pandas as pd
def create_new_entry(state, key, value):
    if (key, value) not in state.key_value_pairs:
        state.key_value_pairs.append((key, value))
    else:
        st.warning("This entry already exists!")
def delete_entry(state, index):
    if 0 <= index < len(state.key_value_pairs):
        state.key_value_pairs.pop(index)
    else:
        st.warning("Invalid index for deletion.")
def main():
    st.title("Enter the Quality Standards")
    if "key_value_pairs" not in st.session_state:
        st.session_state.key_value_pairs = []
    with st.form("dict_form"):
        st.write("Enter Data:")
        key_input = st.text_input("Description")
        value_input = st.text_input("Desired Value")
        submitted = st.form_submit_button("Add data")
        if submitted and key_input and value_input:
            create_new_entry(st.session_state, key_input, value_input)
            st.success("Pair added to dictionary!")
    st.write("---")
    st.write("Created Dictionary:")
    # Represent entered values in a table
    data = {
        "Index": list(range(1, len(st.session_state.key_value_pairs) + 1)),  # Start from 1
        "Description": [key for key, _ in st.session_state.key_value_pairs],
        "Desired Value": [value for _, value in st.session_state.key_value_pairs]
    }
    df = pd.DataFrame(data)
    st.table(df.set_index("Index"))  # Set the index for display
    # Layout for edit and delete buttons
    col1, col2 = st.columns(2)
    with col1:
        edit_button_clicked = st.checkbox("Edit an Entry")
        if edit_button_clicked:
            selected_description = st.selectbox("Select an entry to edit:",["Select an entry"] + df["Description"].tolist())
            if selected_description != "Select an entry":
                index_to_edit = df[df["Description"] == selected_description].index[0]
                new_key = st.text_input("New Description", selected_description)
                new_value = st.text_input("New Desired Value", df.loc[index_to_edit, "Desired Value"])
                if st.button("Update"):
                    df.at[index_to_edit, "Description"] = new_key
                    df.at[index_to_edit, "Desired Value"] = new_value
                    # Update session state list
                    st.session_state.key_value_pairs[index_to_edit] = (new_key, new_value)
                    st.success(f"Pair updated: {selected_description} -> {new_key}, {new_value}")
    with col2:
        delete_button_clicked = st.checkbox("Delete an Entry")
        if delete_button_clicked:
            selected_index = st.selectbox("Select an entry to delete:",["Select an entry"] + df["Description"].tolist())
            if selected_index != "Select an entry":
                index_to_delete = df[df["Description"] == selected_index].index[0]
                if st.button("Confirm Delete"):
                    delete_entry(st.session_state, index_to_delete)
                    st.success(f"Pair {selected_index} deleted!")
                    st.experimental_rerun()  # Trigger re-run to immediately reflect changes
if __name__ == "__main__":
    main()

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