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.
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.
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.