Remove the section automatically once function is completed

Once check the edit checkbox, below pic appears

Once the record is successfully updated , the screen should revert back to the first image i.e. input box and update button should not remain.
Please help to solve the issue.

One way to revert back is to force the checkbox to be reset by changing the key. Here’s an example:

import streamlit as st

"# Customer Management System"

if "data" not in st.session_state:
    st.session_state["data"] = {
        "Transaction ID": 1,
        "Customer ID": "E001",
        "Date of Transaction": "2024-01-01",
        "Amount": 6000,
    }

if "checkbox_key" not in st.session_state:
    st.session_state["checkbox_key"] = 0

st.dataframe(st.session_state["data"])

if st.checkbox("Edit", key=st.session_state["checkbox_key"]):
    new_amount = st.number_input("Enter new amount", value=st.session_state["data"]["Amount"])
    if st.button("Update"):
        st.session_state["data"]["Amount"] = new_amount
        st.session_state["checkbox_key"] += 1
        st.rerun()

You can play with the code here.

I would also suggest considering using st.data_editor - Streamlit Docs, as it’s great for cases like this where you’re editing data.

Thanks for the answer.
above code works fine but the issue is st.success() or st.write() message does not display any msg.

Foe ex:
if st.button(“Update”):
st.session_state[“data”][“Amount”] = new_amount
st.session_state[“checkbox_key”] += 1
st.success("Update successful ")----> this does not return anything
st.rerun()

The problem is that st.rerun happens immediately – if you add a little sleep call, then the success will show.

import streamlit as st
from time import sleep

"# Customer Management System"

if "data" not in st.session_state:
    st.session_state["data"] = {
        "Transaction ID": 1,
        "Customer ID": "E001",
        "Date of Transaction": "2024-01-01",
        "Amount": 6000,
    }

if "checkbox_key" not in st.session_state:
    st.session_state["checkbox_key"] = 0

st.dataframe(st.session_state["data"])

if st.checkbox("Edit", key=st.session_state["checkbox_key"]):
    new_amount = st.number_input("Enter new amount", value=st.session_state["data"]["Amount"])
    if st.button("Update"):
        st.session_state["data"]["Amount"] = new_amount
        st.session_state["checkbox_key"] += 1
        st.success("Success!")
        sleep(1)
        st.rerun()

See here

Thank you for quick reply.
Your solution worked perfectly.

1 Like

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