【问题标题】:How to create expanders for multiple columns using python and streamlit?如何使用 python 和 streamlit 为多列创建扩展器?
【发布时间】:2022-08-20 16:04:33
【问题描述】:

我正在尝试创建显示具有扩展器的 12 列的函数,并且每个扩展器都有一个带表格的多选小部件。

我怎样才能做到这一点 ?

我的代码:


def display_month_list(df,month):
      df_test = df.iloc[:;[0,1,2,3]]
      option_list = df_test[\"categories\"].unique().tolist()
     
      selected_option = st.multiselect(\"Select Category\",option_list)
      if selected_option:
            df_test = df_test[df_test[\"categories\"].isin(selected_option)]
      st.write(df_test)

col1,col2,col3,col4 = st.columns(4)
col5,col6,col7,col8 = st.columns(4)
col9,col10,col11,col12 = st.columns(4)

with col1:
   with st.expander(\"cat1\"):
        display_month_list(df,\"cat1\")

with col2:
   with st.expander(\"cat2\"):
        display_month_list(df,\"cat2\")

等等....

我的问题是如何在每一列中添加这个函数?

    标签: python for-loop streamlit


    【解决方案1】:

    多个多选需要一个唯一键。请参阅我的示例代码,了解如何提供唯一密钥。

    代码

    def display_month_list(i):
        selected_option = st.multiselect("Select Category", ['a', 'b', 'c'], key=i)
        # st.write(f'your stuff')
    
    cols = st.columns(4)
    for i, c in enumerate(cols):
        with c:
            with st.expander(f'cat {i+1}'):
                display_month_list(i+1)
    
    cols = st.columns(4)
    for i, c in enumerate(cols):
        with c:
            with st.expander(f'cat {i+5}'):
                display_month_list(i+5)
    
    cols = st.columns(4)
    for i, c in enumerate(cols):
        with c:
            with st.expander(f'cat {i+9}'):
                display_month_list(i+9)
    

    输出

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 2015-09-10
      • 2011-12-19
      • 1970-01-01
      • 2021-03-13
      相关资源
      最近更新 更多