Streamlit 滑块在更改最大值时重置。

huangapple go评论73阅读模式
英文:

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')

huangapple
  • 本文由 发表于 2023年2月27日 06:43:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定