英文:
Conditionals and Rounding with streamlit's st.number_input()
问题
我有两个st.number_inputs(),我正在尝试使用
input1 = st.number_input("x", value=5.0, step=0.5, format="%0.1f")
input2 = st.number_input("y", value=-110)
它们工作得很好,除了一些特殊情况。关于input1,我想让它始终四舍五入到最接近的0.5。步长有效,但当输入浮点数如5.4时,数字没有四舍五入,步长表现正常,从5.4变为5.9等等,而我想要5.4四舍五入为5.5,然后表现为5.5,6.0等等... 是否有办法始终将任何给定的数字四舍五入到最近的0.5?
至于input2,我想在数字超过-100时开始向相反方向计数,即不是-102,-101,-100,-99,而是-102,-101,-100,+101,+102,等等。是否可以向st.number_input()添加这样的条件?此外,如果给定-100到+100之间的任何数字,我想将其四舍五入(类似于input1)为-100或+100,取决于哪个更接近。
因此,我的问题基本上可以归结为两个主题:是否可以将给定给st.number_input()的数字四舍五入,并添加特定条件?
我尝试使用.format()
,但数字输入是字符串,所以无法奏效。至于四舍五入,是否有四舍五入到最接近0.5的printf格式,我可以添加到格式参数中?至于我对input2要实现的目标,我不知道从哪里开始。
英文:
I have two st.number_inputs() I am trying to use
input1 = st.number_input("x", value=5.0, step=0.5, format="%0.1f")
input2 = st.number_input("y", value=-110)
They work great, except for a few specific cases. As for input1, I’d like for it to always be rounded to the nearest half number. The step works, but when a float such as 5.4 is given, the number is not rounded and the step behaves as normal and it goes from 5.4, 5.9, etc. when I’d like the 5.4 to be rounded to 5.5 and behave like 5.5, 6.0, etc… Is there a way to always round any given number to the nearest 0.5?
As for input2, I’d like to start counting the opposite direction once the number gets above -100. ie, instead of -102, -101, -100, -99, I’d like for it to go -102, -101, -100, +101, +102, … etc. Is it possible to add conditionals like this to an st.number_input()? Furthermore, If any number between -100 and +100 is given, I’d like to round it (similar to input1) to either -100 or +100, whichever is closer.
So basically, my question boils down to two main topics: is it possible to round a number given to st.number_input() and add certain conditionals?
I've tried using .format()
but the number input is a string so that doesn't work. As for the rounding, is there printf format for rounding to the nearest 0.5 I can add to the format argument? No idea where to start with what I'm trying to achieve for input2
, though.
答案1
得分: 1
因为我在Streamlit论坛上回答了这个问题,我也会在这里发布答案:
您可以使用回调函数来进行 on_change 四舍五入。
import streamlit as st
# 对于第一个小部件(以0.5的增量显示)
def forced_round():
st.session_state.half = round(st.session_state.half*2)/2
st.number_input('Half Steps', 0.0, 10.0, step=0.5, key='half', on_change=forced_round)
# 对于第二个小部件(阻止-100到100之间的值)
def skip_100(previous):
if previous == 100 and st.session_state.zone == 99:
st.session_state.zone = -100
elif previous == -100 and st.session_state.zone == -99:
st.session_state.zone = 100
elif 0 <= st.session_state.zone < 100:
st.session_state.zone = 100
elif -100 < st.session_state.zone < 0:
st.session_state.zone = -100
if 'zone' not in st.session_state:
st.session_state.zone = 100
st.number_input('No Go Zone of 100', -1000, 1000, step=1, key='zone', on_change=skip_100, args=[st.session_state.zone])
唯一需要注意的是它无法区分手动输入的更改与通过点击增量按钮进行的更改。因此,在当前显示为100时手动输入99会导致其跳到-100。对于其他当前值,输入99将四舍五入为100。
英文:
Since I answered this question over on the Streamlit forums, I'll also post the answer here:
You can use a callback to round on_change.
import streamlit as st
# For the first widget (display in increments of 0.5)
def forced_round():
st.session_state.half = round(st.session_state.half*2)/2
st.number_input('Half Steps',0.0,10.0, step=.5, key='half', on_change=forced_round)
# For the second widget (prevent values between -100 and 100)
def skip_100(previous):
if previous == 100 and st.session_state.zone == 99:
st.session_state.zone = -100
elif previous == -100 and st.session_state.zone == -99:
st.session_state.zone = 100
elif 0 <= st.session_state.zone < 100:
st.session_state.zone = 100
elif -100 < st.session_state.zone < 0:
st.session_state.zone = -100
if 'zone' not in st.session_state:
st.session_state.zone = 100
st.number_input('No Go Zone of 100',-1000,1000, step=1, key='zone', on_change=skip_100, args=[st.session_state.zone])
The only caution is that it can't distinguish between a manually entered change or one from clicking the increment button. As such, manually typing 99 when it currently says 100 will cause it to jump down to -100. With any other current value, entering 99 will round up to 100.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论