Can a slider callback pass the slider value?

Can I use a callback function in a slider to pass the value that the slider was just changed to? How do I do that?

Certainly! Here’s an example adapted from our session state docs. You need to assign a key to the slider widget so its value is stored in session state. You can then get this value as a session state variable st.session_state.key:

Code

import streamlit as st

# Use a callback to display the current value of the slider when changed
def display_value():
    st.write("The value of the slider is:", st.session_state.myslider)

st.slider(
    "My Slider", 0.0, 100.0, 1.0, step=1.0, key="myslider", on_change=display_value
)

Output

slider-callback

Best, :balloon:
Snehan

1 Like