我正在制作一个Streamlit考勤应用程序,所有出勤表格都上传到云存储中(在我的例子中是谷歌云存储)。如何查看所有可用文件(按日期命名)并允许st.selectbox()允许查看可用文件并一次选择其中一个文件?
我已经阅读了以下的文档和线程,并获得了基本的工作原理,但我不知道如何将下拉列表实现为实现该列表的流光选择箱。
我目前将文件发送到应用程序的方式是通过上传:https://imgur.com/a/3l5WUTx
uploaded_file = st.file_uploader(label='upload your csv or excel file', type=['csv', 'xlsx'])
因此,我希望将所有文件(将以DDMMYYYY格式命名)保存在云服务器中。我希望为不同的可用文件名(以日期格式存在)提供下拉选择st.selectbox(),并从Streamlit应用程序中的云存储中获取输入。
import streamlit as st
from google.oauth2 import service_account
from google.cloud import storage
# Create API client.
credentials = service_account.Credentials.from_service_account_info(
st.secrets["gcp_service_account"]
)
client = storage.Client(credentials=credentials)
# Retrieve file contents.
# Uses st.experimental_memo to only rerun when the query changes or after 10 min.
@st.experimental_memo(ttl=600)
def read_file(bucket_name, file_path):
bucket = client.bucket(bucket_name)
content = bucket.blob(file_path).download_as_string().decode("utf-8")
return content
bucket_name = "streamlit-bucket"
file_path = "myfile.xlsx" #Edit the input variable here.
而不是file_path是常量,我希望它是一个st.selectbox(),它将从流存储桶中获取可用的文件。存储在那里的文件将在DDMMYYYY的命名约定中(因为它有不同日期的出勤文件)。之后,它将存储在变量内容中,然后我就可以开始使用它了。
content = read_file(bucket_name, file_path)`
以上谷歌云存储实现的源代码来自以下链接:https://docs.streamlit.io/knowledge-base/tutorials/databases/gcs#enable-the-google-cloud-storage-api
到目前为止我的资料来源和参考资料:
https://www.codegrepper.com/code-examples/python/drop+down+menu+streamlit
https://discuss.streamlit.io/t/file-list-in-upload-directory/15796
发布于 2022-09-16 05:42:21
正如@Caroline Frasca所提到的,为了从下拉菜单中从云存储中保存的文件中选择一个文件,我们需要提取存储在Google中的文件的名称。共享stackoverflow链接进行详细讨论。
def list_files(bucketName):
"""List all files in GCP bucket."""
files = bucket.list_blobs(prefix=bucketFolder)
fileList = [file.name for file in files if '.' in file.name]
return fileList
list_blobs()
为我们提供了桶中文件的列表。默认情况下,这将返回所有文件;我们可以通过指定prefix
属性将想要列出的文件限制为桶中的文件。
此外,您还可以检查文档
https://stackoverflow.com/questions/73698270
复制相似问题