【问题标题】:How can 2 selectboxes 1st with alphabets and the 2nd with the names that start with that alphabet2个选择框如何第一个带有字母,第二个带有以该字母开头的名称
【发布时间】:2021-08-30 14:40:14
【问题描述】:

如何 2 个选择框第一个带有字母,第二个带有以该字母开头的名称

1- 我使用以下代码创建了一个 CSV 文件名列表

path = "/home/zhamane/streamlit/Streamlit_tutorials/data"
li = [os.path.splitext(filename)[0] for filename in os.listdir(path)]

例如,我在“数据”文件夹中有 3 个文件(all_stocks_5yr.csv、auto-mpg.csv、clean_auto_mpg.csv),它将返回一个列表

[all_stocks_5yr, auto-mpg, clean_auto_mpg]

2- 我使用 stremlit 中的选择框来选择这些文件

select_event = st.sidebar.selectbox('Choisir des indicateurs', li)

但我想首先创建 2 个选择框,其中包含从每个文件名的初始字母创建的字母列表(在我们的示例中)

[a, c]

当我选择例如 a 时,第二个列表将显示所有以 a 开头的名称

3- 之后我想读取用熊猫选择的文件

df = pd.read_csv("path/{select_event}.csv")

【问题讨论】:

  • 我不明白你在问什么。您是否尝试使用搜索/过滤条件过滤选择框?

标签: python-3.x streamlit


【解决方案1】:

我不确定你要的是什么。我假设您正在尝试过滤您的输入?我想出了以下解决方案。

import streamlit as st
list_of_files = ["all_stocks_5yr", "auto-mpg", "clean_auto_mpg"]

st.write(list_of_files)

def should_include(item, search_term, criteria):
    if criteria == "starts with":
        return item.startswith(search_term)
    elif criteria == "contains":
        return search_term in item

search_term = st.text_input("Filter")

option = st.radio("choose filter option", ("starts with", "contains"))

new_items = [item for item in list_of_files if should_include(item, search_term, criteria = option)]
st.multiselect("Select files", list_of_files, new_items)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多