Reset slider values to original state

I am trying to create multiple sliders through FOR loop for storing user inputs. These sliders first render with some fixed values and the user can change these sliders to new values.
I want to give “Reset” st.button option to users where this resets sliders to its original state.
I tried deleting session_state with the Reset button. however, this does not reset the rendered slider on UI.

Here is the code:
import streamlit as st
slider =
s_min = 0
s_max = 10
default_cal = 5

if st.button(‘Reset’):
for key in st.session_state.keys():
del st.session_state[key]
slider =
f = 0

for key in st.session_state.keys():
st.write(‘keys are’)
st.write(st.session_state[key])

for i in range(3):
var_number = i
slider.append(st.slider(‘Change variable value:’, s_min, s_max, default_cal,
key=“sld_%d” % var_number ))

1 Like

Hi @Pranav_s14,

Thanks for posting!

Can you share a more complete code snippet so we can try to reproduce the issue? Specifically the lines where you’re defining the sliders

Caroline :balloon:

import streamlit as st
slider =
s_min = 0
s_max = 10
default_cal = 5
number_of_sliders = 5

if st.button(‘Reset’):
for key in st.session_state.keys():
del st.session_state[key]
slider =
f = 0

for key in st.session_state.keys():
…st.write(‘keys are’+key))
…st.write(st.session_state[key])

for i in range(number_of_sliders):
…var_number = I
…slider.append(st.slider(‘Change variable value:’, s_min, s_max, default_cal,
…key=“sld_%d” % var_number ))

Interest in this topic too

“I tried deleting session_state with the Reset button. however, this does not reset the rendered slider on UI.”

This is exactly my issue as well. I can reset the variable that the widget controls, but the widget visible on the UI is not reset. Therefore, after reset, if I change any widget, any other widget that was changed before the reset is the same as before the reset.

1 Like

You can use st.empty:

key = 1
slider_place_holder = st.empty()
my_slider = slider_place_holder.slider("Weight", 0, 100, 70, key=key)

def reset_all_sliders(reset_iteration=1):
    slider_place_holder.empty()
    my_slider = slider_place_holder.slider("Weight", 0, 100, 70, key=(key+reset_iteration))

and then place it in a button:

reset_iteration = 1
if st.button('Reset to Default'):
    reset_iteration += 1
    reset_all_sliders(reset_iteration)

actually make sure that the reset all sliders function returns the new value (return my_slider)

@Costantino_Terranova, tried to follow your guidance, and it mostly worked for me, but after resetting the slider it only registers my adjustment after I interreact with it twice, seems like its re-rendering the slider after my input. appreciate any advice you can offer! see gif below…

2023-11-27_14-09-48

key = 1
slider_place_holder = st.empty()
my_slider = slider_place_holder.slider("Weight", 0, 100, 70, key=key)

def reset_all_sliders(reset_iteration=1):
    slider_place_holder.empty()
    my_slider = slider_place_holder.slider("Weight", 0, 100, 70, key=(key+reset_iteration))
    
    return my_slider

reset_iteration = 1
if st.button('Reset to Default'):
    reset_iteration += 1
    st.write(reset_iteration)
    reset_all_sliders(reset_iteration)

Has there been any update to the reset filters for the st.slider? The suggestion by @Constantino_Terranova only responded after clicking the button twice

Using session_state and callback functions you can version out the key of the sliders and randomize using the callback to cause a full reset.

import random

s_min = 0
s_max = 10
default_cal = 5
number_of_sliders = 5

if "slider_version" not in st.session_state:
    st.session_state["slider_version"] = 1

def reset_sliders():
    st.session_state["slider_version"] = +random.randint(1, 100)

for i in range(number_of_sliders):
    st.slider(
        f"Slider {i}",
        min_value=s_min,
        max_value=s_max,
        value=default_cal,
        step=1,
        key=f"slider_{i+st.session_state['slider_version']}",
    )

st.button("Reset", on_click=reset_sliders)