0

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://i.stack.imgur.com/NHmxT.jpg

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

Abhisek Ganguly
  • 125
  • 2
  • 11
  • 1
    It looks like you still need to pull the names of the files you're storing in Google Cloud. There are a lot of existing SO posts that cover this. Here's one: https://stackoverflow.com/questions/22398898/google-cloud-storage-python-any-way-to-list-obj-in-certain-folder-in-gcs – Caroline Frasca Sep 13 '22 at 19:07
  • @CarolineFrasca thank you for the reply. I think now I can get it, if I pull out the names of the files and pass them into the selectbox() – Abhisek Ganguly Sep 14 '22 at 01:26

1 Answers1

1

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

Monali Ghotekar
  • 359
  • 1
  • 7