How to update slider values after modification?

I have some sliders with values initialised. Later, I have functions that change the value coming from the slider. Is there a way for the slider value and visual position to be updated with this new value?

In it’s most basic form, I’ve tried

import streamlit as st

def slider(value=3):

    slider_value = st.slider("my nice slider", min_value=0, max_value=10, value=value)
    return slider_value

slider_value = slider()

st.write(f"Slider value is {slider_value}")

if st.button("Increment slider by 1"):

    slider_value = slider_value + 1

    slider_value = st.slider("my nice slider", min_value=0, max_value=10, value=slider_value)

    st.write(f"Slider value is {slider_value}")

but that just duplicates the slider, when I want it to update the original one.

Any help or ideas on this? Thanks! :slight_smile:

Hi altanner,
exactly this example is actually mentioned in the docs: Widget updating for every second input when using session state - Streamlit Docs :wink:

2 Likes

Yey! Thanks for being kind to me when you were well within your rights to say “RTFM!” :laughing:

You’re welcome. Didn’t mean to be rude at all :wink:

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