gpt4 book ai didi

python - Multiselect/Selectbox 不会在第一次选择后等待 - Streamlit

转载 作者:行者123 更新时间:2023-12-05 04:49:06 32 4
gpt4 key购买 nike

全部,

我以前成功使用过多选,但是当我尝试这个我作为 POC 尝试的特定示例时,行为非常奇怪。本质上,我想要做的是使用多选让应用程序在中间步骤等待用户输入。但是,multiselect 不会等我选择我要选择的输入,只要我选择一个东西,它就会运行,甚至没有正确执行。有人可以指导我做错什么吗?我使用的是 0.82 版本。

我还使用 selectbox 进行了相同的测试,并且看到了相同的行为。

所以,这是我所拥有的:

import streamlit as st
import pandas as pd
def basic_skeleton() -> tuple:
"""Prepare the basic UI for the app"""
st.sidebar.title('User Inputs')
beta_expander = st.sidebar.beta_expander("Upload csv")
with beta_expander:
user_file_path = st.sidebar.file_uploader(
label='Random Data',
type='csv'
)
return user_file_path
def get_filtered_dataframe(df) -> pd.DataFrame:
columns_list = df.columns
with st.form(key='Selecting Columns'):
columns_to_aggregate = st.selectbox(
label='Select columns to summarize',
options=columns_list
)
submit_button = st.form_submit_button(label='Submit')
if submit_button:
df1 = df[columns_to_aggregate]
return df1
def main():
"""Central wrapper to control the UI"""
# add title
st.header('Streamlit Testing')
# add high level site inputs
user_file_path = basic_skeleton()
load = st.sidebar.button(label='Load Data')
if load:
df = pd.read_csv(user_file_path)
st.dataframe(df)
clean_df = get_filtered_dataframe(df)
run = st.button("Aggregate Selected columns")
if run:
result = clean_df.describe(include='all')
st.dataframe(result)
main()

最佳答案

streamlit 社区的一位用户帮助回答了这个问题。我想确定一下,这里提供了答案,因此这里也提供了任何前来寻找的人:

import streamlit as st
import pandas as pd

def basic_skeleton() -> tuple:
"""Prepare the basic UI for the app"""
st.sidebar.title('User Inputs')
beta_expander = st.sidebar.beta_expander("Upload csv")
with beta_expander:
user_file_path = st.sidebar.file_uploader(
label='Random Data',
type='csv'
)
return user_file_path

def get_filtered_dataframe(df):
columns_list = df.columns
with st.form(key='Selecting Columns'):
columns_to_aggregate = st.multiselect(
label='Select columns to summarize',
options=columns_list
)
submit_button = st.form_submit_button(label='Submit')
if submit_button:
df1 = df[columns_to_aggregate]
return df1

def main():
"""Central wrapper to control the UI"""
# add title
st.header('Streamlit Testing')

# add high level site inputs
user_file_path = basic_skeleton()
if user_file_path:
load = st.sidebar.checkbox(label='Load Data')

if load:
df = pd.read_csv(user_file_path)
st.dataframe(df)
clean_df = get_filtered_dataframe(df)

if clean_df is not None:
result = clean_df.describe()
st.dataframe(result)

main()

关于python - Multiselect/Selectbox 不会在第一次选择后等待 - Streamlit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67730792/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com