Trying to get a drop-down list of all the csv files in my Supabase bucket but I am getting an error on my Streamlit app that no files are found in the bucket. This is the code I have
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from supabase import create_client, Client
import io
# Supabase credentials (replace with your actual credentials)
SUPABASE_URL = "url" #replace with actual
SUPABASE_API_KEY = "api" #replace with actual
# Initialize Supabase client
def get_supabase_client() -> Client:
return create_client(SUPABASE_URL, SUPABASE_API_KEY)
# Function to list all files in a specific bucket
def list_files_in_bucket(bucket_name: str):
supabase = get_supabase_client()
# List all files in the 'csv_files' bucket (or your specific bucket)
files = supabase.storage.from_(bucket_name).list()
# Check if any files were found
if files:
return [file['name'] for file in files]
else:
st.error("No files found in the specified bucket.")
return []
# Function to download a CSV from Supabase storage
def download_csv_from_supabase(file_name: str) -> pd.DataFrame:
supabase = get_supabase_client()
# Get the file from storage as bytes
bucket_name = "nameofmybucket" # Replace with your actual bucket name
response = supabase.storage.from_(bucket_name).download(file_name)
# Check response status
if response.status_code == 200:
data = io.BytesIO(response.data)
df = pd.read_csv(data)
return df
else:
st.error(f"Error downloading file: {response.status_code}")
return None
# Function to plot bar graph
def plot_bar_graph(df: pd.DataFrame):
# Assuming the CSV has columns 'Category' and 'Value'
if df is not None:
fig, ax = plt.subplots(figsize=(8, 6))
df.plot(kind='bar', x='Category', y='Value', ax=ax)
ax.set_title("Category vs Value")
ax.set_xlabel("Category")
ax.set_ylabel("Value")
st.pyplot(fig)
# Streamlit app interface
def main():
st.title("Supabase CSV File Viewer")
# List all available files in the Supabase bucket
bucket_name = "LETREPbucket" # Replace with your actual bucket name
files = list_files_in_bucket(bucket_name)
# Show file dropdown or search box
if files:
file_name = st.selectbox("Select a file to view:", files)
if file_name:
# Download the selected CSV
df = download_csv_from_supabase(file_name)
if df is not None:
st.write("Data Preview:")
st.write(df.head()) # Show a preview of the CSV data
# Plot data as bar graph
plot_bar_graph(df)
# Run the app
if __name__ == "__main__":
main()