【问题标题】:Displaying widgets in the correct order when using callbacks in Streamlit在 Streamlit 中使用回调时以正确的顺序显示小部件
【发布时间】:2022-02-04 04:43:58
【问题描述】:

我在 Streamlit (1.3.1) 中制作了一个非常简单的应用程序:

  1. 使用文件上传器小部件读取 Excel 文件
  2. 让用户使用选择框下拉菜单选择他们有兴趣预览的工作表
  3. 显示工作表的预览

我正在使用来自st.selectbox 的回调来显示预览,因此它仅在用户主动选择了他们感兴趣的工作表后显示。但为了这样做,我需要定义我的回调在调用 st.selectbox 之前调用函数 - 这导致预览显示在应用程序中的文件上传器和选择框小部件上方,而不是在这些小部件下方我想要的位置。

这是我的代码和输出的屏幕截图。

import streamlit as st
import pandas as pd

# CREATE APP
# Add file_uploader
uploaded_file = st.file_uploader(
    label='Upload a file', type=['xls', 'xlsx', 'xlsm'],
    key='file-uploader',
    help='''
        Upload an Excel file. The file must be
        closed in order for you to upload it.
    '''
)


# Define function that loads sheet preview
def load_sheet_preview():
    st.dataframe(df[st.session_state.selectbox_sheet])


# Add selectbox
if uploaded_file is not None:
    df = pd.read_excel(uploaded_file, sheet_name=None)      # sheet_name=None needs explicitly including - it isn't the default        # noqa: E501

    st.selectbox(
        key='selectbox_sheet',
        label='Select worksheet',
        options=df.keys(),
        on_change=load_sheet_preview
    )

按照我希望它们出现在应用程序中的顺序将生成文件上传器和选择框的代码包装在函数中,也不会对问题进行排序。

【问题讨论】:

    标签: python streamlit


    【解决方案1】:

    我想出了一个解决方案,它涉及(明确地)使用st.session_state 而不是回调并向下拉选项添加默认值。选择默认选项时不会加载预览。

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import io
    
    import pandas as pd
    import streamlit as st
    
    # CREATE APP
    # Add file_uploader
    uploaded_file = st.file_uploader(
        label='Upload a file', type=['xls', 'xlsx', 'xlsm'],
        key='file-uploader',
        help='''
            Upload an Excel file. The file must be
            closed in order for you to upload it.
        '''
    )
    
    # Add selectbox
    if 'selectbox_sheet' not in st.session_state:       # Initialise variable
        st.session_state['selectbox_sheet'] = '--'
    
    if uploaded_file is not None:
        sheets_dict = pd.read_excel(uploaded_file, sheet_name=None)      # This creates a dictionary of dataframes. sheet_name=None needs explicitly including - it isn't the default        # noqa: E501
    
        default_option = {'--': ''}
    
        selectbox_options = dict(**default_option, **sheets_dict)     # Join dictionaries        # noqa: E501
    
        st.selectbox(
            key='selectbox_sheet',
            label='Select worksheet',
            options=selectbox_options.keys()
        )
    
    # Load sheet preview
    if st.session_state.selectbox_sheet != '--':
        st.dataframe(sheets_dict[st.session_state.selectbox_sheet])
    

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      相关资源
      最近更新 更多