Streamlit 选择框正在重置网页

问题描述 投票:0回答:2

我目前正在尝试使用选择框来允许用户选择用于运行模型的列的名称。当我将选择框放在侧边栏中时,我没有遇到问题,但是当我将选择框放在主页上时,一旦用户选择了一个选项,页面就会重置。代码如下。我对streamlit还是个新手,所以我不确定我是否正确使用了它。我感谢您的帮助!

#add a blank space in the columnName section
columnNames = [" "]

#grab the column names of the dataframe
for column in df.columns:
    columnNames.append(column)

#place the selectboxe in the col1 slot
with col1:
    #display the column names to the user
    #So that they can select the columns they want to use
    columnValue = st.multiselect("Select the column:", columnNames)

#place a button in col2 slot
with col2:
    #a button to add the selected column to a list of want to use columns
    addButtonList = st.button("Add to select list: ")

#when 'addButtonList' is selected take the value from
#'columnValue' and place it on the screen.
if(addButtonList):
    st.write(columnValue)
python drop-down-menu streamlit
2个回答
0
投票

0
投票

有两种可能的解决方案。但在我们查看它们之前,这是导致问题的代码(您可以尝试一下here):

import streamlit as st
import pandas as pd

# Sample dataframe for demonstration
data = {
    "Column 1": [1, 2, 3],
    "Column 2": [4, 5, 6],
    "Column 3": [7, 8, 9]
}

df = pd.DataFrame(data)

# Grab the column names of the dataframe
column_names = list(df.columns)

# Create two columns for layout
col1, col2 = st.columns(2)

# Place a multiselect widget in the col1 slot
with col1:
    # Display the column names so that the user can select the columns
    selected_columns = st.multiselect("Select the column:", column_names)

# Place a button in the col2 slot
with col2:
    # A button to add selected columns to the list
    add_columns_button = st.button("Add to the list")

# If the button is clicked, show the selected columns
if add_columns_button:
    st.write(selected_columns)

解决方案 1:使用
st.session_state

如官方Streamlit文档中所述

st.session_state

会话状态是一种在重新运行之间共享变量的方法,对于每个 用户会话。除了存储和持久状态的能力之外, Streamlit 还公开了使用以下方式操纵状态的能力 回调。会话状态也会在多页面内的应用程序之间持续存在 应用程序。

因此,您可以通过使用

st.session_state
将选定的列存储在会话状态中来解决问题,如下所示:

import streamlit as st
import pandas as pd

# Sample dataframe for demonstration
data = {
    "Column 1": [1, 2, 3],
    "Column 2": [4, 5, 6],
    "Column 3": [7, 8, 9]
}

df = pd.DataFrame(data)

# Initialize session state for selected columns if not already initialized
if 'selected_columns' not in st.session_state:
    st.session_state['selected_columns'] = []

# Grab the column names of the dataframe
column_names = list(df.columns)

# Create two columns for layout
col1, col2 = st.columns(2)

# Place a multiselect widget in the col1 slot
with col1:
    # Display the column names so that the user can select the columns
    selected_columns = st.multiselect("Select the column:", column_names)

# Function to update session state with selected columns
def update_selected_columns():
    st.session_state['selected_columns'] = selected_columns

# Place a button in the col2 slot
with col2:
    # A button to add selected columns to the list and save them in session state
    add_columns_button = st.button("Add to the list", on_click=update_selected_columns)

# If session state is not empty, show the selected columns from session state
if st.session_state['selected_columns']:
    st.write(st.session_state['selected_columns'])

这解决了问题的原因是我们显示会话状态中选定的列,而不是像以前那样从

selected_columns
变量中显示。

解决方案 2:使用
st.form

如官方Streamlit文档中所述

st.form

创建一个表单,通过“提交”按钮将元素批处理在一起。

表单是一个容器,可以直观地对其他元素和小部件进行分组 在一起,并包含一个提交按钮。当表单的提交按钮 按下后,表单内的所有小部件值将被发送到 批量流媒体。

/.../

表格有一些限制:

  • 每个表格必须包含
    st.form_submit_button
  • st.button
    st.download_button
    无法添加到表单中。
  • 表单可以出现在应用程序中的任何位置(侧边栏、列等),但不能嵌入其他表单中。
  • 在表单中,唯一可以具有回调函数的小部件是
    st.form_submit_button

因此,您可以通过用

st.form
包装所有元素来解决问题,如下所示:

import streamlit as st
import pandas as pd

# Sample dataframe for demonstration
data = {
    "Column 1": [1, 2, 3],
    "Column 2": [4, 5, 6],
    "Column 3": [7, 8, 9]
}

df = pd.DataFrame(data)

# Grab the column names of the dataframe
column_names = list(df.columns)

with st.form("my_form", border=False):
    # Create two columns for layout
    col1, col2 = st.columns(2)

    # Place a multiselect widget in the col1 slot
    with col1:
        # Display the column names so that the user can select the columns
        selected_columns = st.multiselect("Select the column:", column_names)
    
    # Place a button in the col2 slot
    with col2:
        # A button to add selected columns to the list
        add_columns_button = st.form_submit_button("Add to the list")

    # If the button is clicked, show the selected columns
    if add_columns_button:
        st.write(selected_columns)

这解决了问题的原因,虽然我们像以前一样显示从

selected_columns
变量中选择的列(!),是因为现在所有元素都用
st.form
包裹。

© www.soinside.com 2019 - 2024. All rights reserved.