Hello,
As described in the title, I am trying to create a streamlit error before displaying the results warning the user that there may be some mistakes with the inputs. A button then allows the user to “proceeds” if they still want to continue despite the warning.
The syntax looks like this :
However, whenever I try running it, the message is not being displayed and any function used after that doesn’t work either, or perhaps it is being run and instantly reset right after. Is there a way to do what I want with the buttons ?
import streamlit as st
if 'proceed' not in st.session_state:
st.session_state.proceed = False
st.session_state.proceed = False
def click_proceed():
st.session_state.proceed = True
st.warning('test')
if st.session_state['warning_displayed']:
st.error('It seems one of the parameters you inputed exceeds limit value, do you still want to proceed ?', icon="🚨")
st.button('Run Simulation', on_click=click_proceed)
if st.session_state.proceed:
st.write('Proceeding...')
#trying random operations and functions to see if it runs
Sorry for the delay! It looks like st.session_state['warning_displayed'] is never initialized, so that if statement at the end will never run.
Instead of checking to see if st.session_state.warning_displayed exists in that if statement, it seems like it might make more sense to have an if statement that checks whether the input you mentioned contains mistakes.
i.e. something like this:
import streamlit as st
if 'proceed' not in st.session_state:
st.session_state.proceed = False
def click_proceed():
st.session_state.proceed = True
if some_input_val > some_benchmark:
st.error('It seems one of the parameters you inputed exceeds limit value, do you still want to proceed ?', icon="🚨")
continue_button = st.button('Run Simulation', on_click=click_proceed)
if st.session_state.proceed:
st.write('Proceeding...')
st.warning('Warning message while you proceed', icon="🚨")
#trying random operations and functions to see if it runs
Alternatively – if I’m misunderstanding the use case and you do want to check st.session_state.warning_displayed at that point, you’ll just need to make sure st.session_state.warning_displayed gets initialized at some point in your app
if 'warning_displayed` not in st.session_state:
st.session_state.warning_displayed = False
if st.warning("You've been warned"):
st.session_state.warning_displayed = True
If you can share the code referencing the input values you mentioned, happy to help debug further
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.