I have 2 sliders, first one works perfect, second one works perfect too until I make its value based on a value in the session_state. Then, when I drag the slider, the slider stops and I have to unclick, reclick, and drag again.
Steps to reproduce
Code snippet:
mu = st.slider('$\mu$', 0.0, 5.0, 2.5, step=0.1)
var = st.slider('$\sigma^2$', 0.0, mu, min(mu, st.session_state.var) if 'var' in st.session_state else 1.0, step=0.1, key='var')
If applicable, please provide the steps we should take to reproduce the error or specified behavior.
Expected behavior:
When I drag the ϲ slider, it should continuously update the message below while dragging.
Actual behavior:
When I drag the ϲ slider, it stops dragging the moment the message below is updated.
Debug info
Streamlit version: 1.17 (but also happening on Streamlit Cloud)
If modifying the var = st.slider(...) line to make the value dependent on anything other than session_state makes the dragging work perfect. Problem persists if I create a new variable before this var = st.slider(...) line to be the value based on session_state.var.
I think I understand. Because you are changing the creation parameters of the second slider, you are in fact getting a completely different widget with each update. Thatās why you canāt ākeep holdā of the selector knob.
I think this would then be expected behavior and youād need a custom component if you want dynamically set the max value of the slider without it creating a new widget each time.
Thank you @mathcatsand, that makes sense. Though itās not just that changing parameters of the var slider prevents dragging from working properly. For example, when the var sliderās max_value is set to mu and its value is set to something involving mu, then the dragging works perfect. Itās only when the creation parameter for value involves session_state that the dragging issue appears.
Itās the changing of the default value thatās doing it.
As I thought about it, you can get around this by not using the default value to change its value and instead using session_state to assign it. (In the middle of something now, so I can post an example in a little bit.)
import streamlit as st
mu = st.slider('$\mu$', 0.0, 5.0, 2.5, step=0.1)
if 'var' in st.session_state:
st.session_state.var = min(mu, st.session_state.var)
var = st.slider('$\sigma^2$', 0.0, mu, min(1.0,mu), step=0.1, key='var')
This worked! Thank you. I changed the argument min(1.0,mu) to just 1.0 and that worked the same. I saw a warning when I first tried this and then the warning went away as a slid the slider. So I didnāt read it. And I canāt reproduce the warning now.
I didnāt know you could set slider values this way. Thank you.
Itās a kind of hit or miss error I get sometimes that I too have a hard time reproducing consistently on demand. I usually get it while doing live edits. Itās not really a problem, as far as I know.
If you get rid of min(1.0,mu) your second slider is going to get stuck with a max of 1.0 if you take mu<1.0, just fyi.
The documentation doesnāt say explicitly, so Iād have to look closer at the code to say for sure, but the effect appears to be: if you set an initial value that is greater than your declared max, then that initial value becomes the max.
Are you getting the warning from a fresh restart of the app, or just sometimes in the middle of editing code on a live app? Iām not sure on disabling; Iāll have to dig a bitā¦
Here is a more robust solution that avoids the error. I didnāt know how you wanted to handle mu == 0.0 so I just hid the second slider. You can avoid a few of these lines if you donāt allow mu to go to zero and break the second slider.
import streamlit as st
if 'default' not in st.session_state:
st.session_state.default = 1.0
def set_default():
if 'var' in st.session_state:
st.session_state.default = min(st.session_state.var, st.session_state.mu)
mu = st.slider('$\mu$', 0.0, 5.0, 2.5, step=0.1, key='mu', on_change=set_default)
if mu > 0.0:
var = st.slider('$\sigma^2$', 0.0, mu, st.session_state.default, step=0.1, key='var')