【问题标题】:How to display text field based on the selected number?如何根据所选数字显示文本字段?
【发布时间】:2021-12-18 10:21:53
【问题描述】:

我有一个 python 代码,允许用户选择他想在文本字段中显示的数字。程序将遍历所选数字的范围并创建一个文本字段。

但是,一旦我运行脚本,它就会崩溃并产生这个错误:

error: TypeError: cannot unpack non-iterable int object Traceback: 文件“F:\AIenv\lib\site-packages\streamlit\script_runner.py”,行 354,在_run_script中 exec(code, module.dict) 文件“f:\AIenv\streamlit\app2.py”,第 1309 行,在 main() 文件“f:\AIenv\streamlit\app2.py”,第 682 行,在 main 对于 i , old_val in range(int(number_of_replacement)):

number_of_replacement = st.number_input("Number of values To Replace",0,100)
with st.form(key='my_form'):
    col1,col2 = st.columns(2)
    st_input = st.number_input if is_numeric_dtype(df[columns]) else st.text_input

    with col1:
        for i , old_val in range(int(number_of_replacement)):
            old_val = st_input("Old value",key=i)

【问题讨论】:

    标签: python for-loop streamlit


    【解决方案1】:

    您的 for 循环似乎有误。

    要么删除i

    with col1:
        for old_val in range(int(number_of_replacement)):
            old_val = st_input("Old value",key=i)
    

    或者,如果您稍后在代码 enumerate 中需要索引 i,而不是迭代:

    with col1:
        for i, old_val in enumerate(range(int(number_of_replacement))):
            old_val = st_input("Old value",key=i)
    

    【讨论】:

      猜你喜欢
      • 2019-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2012-08-29
      • 1970-01-01
      相关资源
      最近更新 更多