简体   繁体   中英

How to use st.selectbox() to select file from drop down menu from files saved in cloud storage in Streamlit app?

I'm making a Streamlit attendance app, all the attendance excel sheets are uploaded to cloud storage (Google cloud storage in my case). How do I view all the available files(which are named date-wise) and allow a st.selectbox() to allow to view the available files and select them one at a time to work with?

I've read the following docs and threads and got the basics working but am not sure how to implement the dropdown list as streamlit selectbox for the implementation of the same.

My current way of getting files to my app is via upload: https://imgur.com/a/3l5WUTx

uploaded_file = st.file_uploader(label='upload your csv or excel file', type=['csv', 'xlsx'])

So, I want to keep all the files (which will be named in DDMMYYYY format) in a cloud server. I want to have a dropdown selection st.selectbox() for the different available filenames (which exist in a date format) and get input from the cloud storage in the Streamlit app.

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.

Instead of file_path being a constant, I want it to be a st.selectbox() which will fetch the available files from streamlit-bucket. The files stored there will be in the naming convention of DDMMYYYY (as it'll have attendance files of different dates). After which it'll be stored in the variable content and I can then start working with it.

content = read_file(bucket_name, file_path)`

My Source code for this above google cloud storage implementation is from the following link: https://docs.streamlit.io/knowledge-base/tutorials/databases/gcs#enable-the-google-cloud-storage-api

my sources and references so far:

https://www.codegrepper.com/code-examples/python/drop+down+menu+streamlit

https://discuss.streamlit.io/t/file-list-in-upload-directory/15796

As @Caroline Frasca mentioned, to select a file from the drop down menu from files saved in cloud storage we need to pull the names of the files stored in Google Cloud. Shared stackoverflow link for the detailed discussion.

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() gets us a list of files in our bucket. By default this will return all files; we can restrict the files we want to list to those in a bucket by specifying the prefix attribute.

Also you can check the document

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM