Creating a button warning the user if there's an issue and asking if he wants to continue

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 ?

Hey @BenjaminLamballais,

Thanks for sharing this question! Do you mind sharing a runnable code snippet rather than a screenshot, so we can better help you debug?

1 Like

Sure here is the code:

 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

Is there anything else you might need ?

Hey @BenjaminLamballais,

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

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