英文:
Streamlit slider resetting when changing max_value
问题
以下是翻译的内容:
I have 2 sliders. The first slider (μ) determines the max_value
for the second slider (σ²). Works great with the following code except for a major issue:
mu = st.slider('μ', 0.0, 5.0, 1.0, step=0.1)
var = st.slider('σ²', 0.0, mu, 1.0, step=0.1)
The issue is that changing mu
resets the value of the var
slider back to 1.0
. How do I have the var
slider change its max_value
from mu
without affecting its currently set value? Is Streamlit's session_state
mechanism appropriate here? It's very important for this app to allow users the ability to have multiple tabs open with different values for mu
and var
in each tab. Is each tab a separate session
?
Here's the MRE with code: https://mre-mu.streamlit.app/
Update:
I was able to accomplish what I wanted with session_state
. Is this the right use of session_state
?
mu = st.slider('μ', 0.0, 5.0, 2.5, step=0.1)
var = st.slider('σ²', 0.0, mu, min(mu, st.session_state.var) if 'var' in st.session_state else 1.0, step=0.1, key='var')
Seems to work, even across tabs (no sharing of state, which is what I needed), but the σ² slider no longer slides! The μ slider does slide and update values as you slide. But if you try and slide the σ² slider, it stops the moment values are updated and you have to click and drag it again. Any idea what's happening?
英文:
I have 2 sliders. The first slider (μ) determines the max_value
for the second slider (σ²). Works great with the following code except for a major issue:
mu = st.slider('$\mu$', 0.0, 5.0, 1.0, step=0.1)
var = st.slider('$\sigma^2$', 0.0, mu, 1.0, step=0.1)
The issue is that change mu
, resets the value of the var
slider back to 1.0
. How do I have the var
slider change its max_value
from mu
without affecting its currently set value? Is Streamlit's session_state
mechanism appropriate here? It's very important for this app to allow users the ability to have multiple tabs open with different values for mu
and var
in each tab. Is each tab a separate session
?
Here's the MRE with code: https://mre-mu.streamlit.app/
Update:
I was able to accomplish what I wanted with session_state
. Is this the right use of session_state
?
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')
Seems to work, even across tabs (no sharing of state, which is what I needed), but the σ² slider no longer slides! The μ slider does slide and update values as you slide. But if you try and slide the σ² slider, it stops the moment values are updated and you have to click and drag it again. Any idea what's happening?
答案1
得分: 0
以下是代码的翻译部分:
问题在于当您与小部件交互时,Streamlit会重新运行您的代码 - 因此,当您与mu
互动时,var
的值会被设置回到滑块首次加载时的值。
要解决这个问题的一种方法是将两个滑块都放在同一个表单中,这将防止您的应用在点击提交按钮之前重新运行。
import streamlit as st
with st.form("slider_form"):
mu = st.slider('$\mu$', 0.0, 5.0, 1.0, step=0.1)
var = st.slider('$\sigma^2$', 0.0, mu, 1.0, step=0.1)
submit = st.form_submit_button("提交滑块数值")
更新:看起来您在这里也得到了这个问题的答案。我会将Debbie的回复粘贴如下。
您可以通过不使用默认值来更改其值,而是使用
session_state
来分配它来解决这个问题。
这是一个更健壮的解决方案,可以避免错误。我不知道您想如何处理mu == 0.0,所以我只是隐藏了第二个滑块。如果不允许mu为零并且中断第二个滑块,您可以避免一些这些行。
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')
英文:
The issue is that when you interact with a widget, Streamlit reruns your code – so when you interact with mu
, the value of var
gets set back to whatever it would be when the slider first loads.
One way to get around this is to make both sliders part of the same form, which would prevent your app from rerunning until you hit the submit button.
import streamlit as st
with st.form("slider_form"):
mu = st.slider('$\mu$', 0.0, 5.0, 1.0, step=0.1)
var = st.slider('$\sigma^2$', 0.0, mu, 1.0, step=0.1)
submit = st.form_submit_button("Submit Slider Values")
Update: looks like you got an answer to this question here as well. I'll paste Debbie's response below.
> you can get around this by not using the default value to change its
> value and instead using session_state to assign it.
> 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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论