Search code examples
streamlit

Streamlit dynamic text based on slider below


Streamlit makes dynamic text, based on a slider value, super easy:

value = st.slider('Slide me', 0, 100, 50)
if value > 80:
    st.warning('Value is too big!')

How do I issue this warning above the slider? This doesn't work:

if value > 80:
    st.warning('Value is too big!')
value = st.slider('Slide me', 0, 100, 50)

The warning will never get displayed when sliding over 80 (actually I'd get an error that value is undefined so I have to set value = 0 at the top).


Solution

  • You can declare a container (or using st.empty) above the slider and then fill it in the conditional that runs after the slider:

    container = st.container()
    value = st.slider('Slide me', 0, 100, 50)
    if value > 80:
        container.warning('Value is too big!')
    

    It could be possible that st.warning does not go "into" the container, then you'd have to define your own warning banner to put in there. Sorry, cannot test it currently.