I defined a dialog, and after calling it, the dialog window does not close regardless of clicking “Yes” or “No”. Pressing “Esc” can always close the window, but this is not what I want. After multiple attempts to use st.dialog, I gave up due to the inability to close the window. Could you provide guidance?
@st.dialog("Duplicate Solution")
def duplicate_solution_warning(design_result_data):
st.warning('This configuration already exists. Are you sure you want to add it?')
ok_button, cancel_button, blankspace = st.columns([0.2, 0.2, 0.6])
with ok_button:
ok = st.button("Yes")
with cancel_button:
cancel = st.button("No")
if ok:
st.session_state.config.append(design_result_data)
st.rerun()
if cancel:
st.session_state.design_result_data = []
st.rerun()
You are calling rerun() after the button is pressed. It the dialog keeps open, it is probably because you are calling duplicate_solution_warning() again.
I modified the example code for st.dialog from the official documentation as shown below, but the window still doesn’t close. If the screen refreshes multiple times during program execution, leading to multiple calls of st.dialog, how can I ensure that st.dialog is only called the last time? If this is mandatory, then the use cases for st.dialog become very limited; how useful is it then?
@st.dialog("Dialog Test")
def warning_dialog():
st.write('Warning Text......')
if st.button("Submit"):
print('ok')
st.rerun()
Thanks! I simplified the code calling st.dialog to the following, based on the official documentation’s example. After pasting it into several different locations in my program and running it, the st.dialog window closed each time. However, when I placed warning_dialog() inside a specific if statement, the window failed to close. Later, in the same location, when I changed st.button to st.checkbox or st.toggle in the code below, the window’s inability to close reappeared. Switching back to st.button allowed normal closure again. I don’t know if I’ve found a pattern in using st.dialog, but my current findings are rather frustrating. Is this the reality of the situation?
I’ve created a new program, but I can’t figure out what’s wrong. The st.dialog window won’t close when I click the st.button("Submit"). The code is as follows:
import streamlit as st
@st.dialog("Dialog Test")
def warning_dialog():
st.write('Warning Text......')
if st.button("Submit"):
print('ok')
st.rerun()
if st.checkbox('Test'):
warning_dialog()
The runtime environment is macOS + Safari, with Python 3.12.
The checkbox remains checked on the rerun so the dialog will open. A Streamlit rerun is not a reset of widget state, so you need to manually arrange for the widget states to be reset using callbacks. The solution is quite ugly if you want both the checkbox state to revert to unchecked and the dialog box to dismiss. However, the dialog box will dismiss if you click outside and the checkbox will still be checked. That is not ideal.
You are much better off using a button activated dialog solution. It is simpler to implement and understand as button state reverts back to False on a rerun making the logic straightforward.
Both solutions are contrasted below:
import streamlit as st
# CHECKBOX ACTIVATED --------------------------------
if "test_checkbox_val" not in st.session_state:
st.session_state.test_checkbox_val = False
def _submit_cb():
st.session_state.test_checkbox_val = False
def _checkbox_cb():
st.session_state.test_checkbox_val = st.session_state.test_checkbox
@st.dialog("CHECKBOX ACTIVATED Dialog Test")
def checkbox_warning_dialog():
st.write('Warning Text......')
if st.button("Submit", on_click=_submit_cb):
print('ok')
st.rerun()
if st.checkbox('Test', on_change=_checkbox_cb, value=st.session_state.test_checkbox_val, key='test_checkbox'):
checkbox_warning_dialog()
# BUTTON ACTIVATED --------------------------------
@st.dialog("BUTTON ACTIVATED Dialog Test")
def button_warning_dialog():
st.write('Warning Text......')
if st.button("Submit"):
print('ok')
st.rerun()
if st.button('Test'):
button_warning_dialog()
Thank you very much for your guidance! I’m a Python beginner, but I’m passionate about programming. I chose Streamlit because of its speed, efficiency, and ease of implementation, but transitioning from an event-driven tool to Streamlit requires a shift in programming thinking (at least for me). In the code I’m developing, st.dialog is used for prompts or additional actions after a widget’s state changes, so it’s difficult to call st.dialog solely with st.button (perhaps I’m constrained by ingrained thinking from using other tools before). I modified the example from the official documentation to the following code. I noticed that if "vote" not in st.session_state: and st.session_state.vote = 'D' (the value of st.session_state.vote can be arbitrary) must appear together in the code as shown below, and warning_dialog() must be inside if "vote" not in st.session_state: (even if if "vote" not in st.session_state: and if st.checkbox("A"): are swapped), for the dialog box to close correctly. I’m very curious; st.session_state.vote is completely unrelated to the state of the st.checkbox in the code or the st.button in the dialog, yet the closing of the warning_dialog() box is affected by it.
import streamlit as st
@st.dialog("Cast your vote")
def warning_dialog():
if st.button("Submit"):
print("ok")
st.session_state.vote = 'D'
st.rerun()
if "vote" not in st.session_state:
if st.checkbox("A"):
warning_dialog()
The closing only happens because of the rerun. Once the vote has been cast, then the warning dialog can’t be activated because the vote is now in session state, causing checkbox “A” to not render. Perhaps this example is more intuitive from the messages which will be printed:
@st.dialog("Cast your vote")
def warning_dialog():
vote = st.radio("Vote", ["A", "B", "C", "D"])
if st.button("Submit"):
print("ok")
st.session_state.vote = vote
st.rerun()
if "vote" not in st.session_state:
st.warning("You have not voted yet.")
if st.button("Cast a vote", type="primary"):
warning_dialog()
else:
st.success(f"Your vote is: {st.session_state.vote}.")
if st.button("Change vote"):
st.session_state.pop("vote")
st.rerun()
So I am using the st.data_editor table, and I have a column called checkbox when the checkbox is ticked, I show the @st.dialog box when the user button or press x I want to close the @st.dialog box and uncheck the selectbox from the table can you write sample code it help me a lot
thanks
I’ve never used data_editor. Post minimal sample code demonstrating your issue and explain what is happening and what you want to happen, with a screenshot or two?
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
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.
Performance cookies
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.
Functional cookies
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.
Targeting cookies
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.