Skip to content

Commit e56d3b6

Browse files
authored
Add files via upload
1 parent 9e3469e commit e56d3b6

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

requirements.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
flask
2-
gunicorn
3-
google-api-python-client
1+
streamlit
42
google-auth
5-
google-cloud-storage
6-
pandas
3+
google-api-python-client

streamlit_app.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import streamlit as st
2+
import tempfile
3+
import os
4+
from google.oauth2 import service_account
5+
from googleapiclient.discovery import build
6+
from googleapiclient.http import MediaFileUpload
7+
8+
# App Title
9+
st.title("Scientific Slide Library Uploader")
10+
11+
# Load configuration from Streamlit Secrets (for security)
12+
# In Streamlit Cloud, you'll paste your JSON content into the "Secrets" setting
13+
if "gcp_service_account" in st.secrets:
14+
info = st.secrets["gcp_service_account"]
15+
creds = service_account.Credentials.from_service_account_info(info)
16+
else:
17+
# For local testing, looks for your file
18+
creds = service_account.Credentials.from_service_account_file("service-account.json")
19+
20+
SHEET_ID = st.secrets.get("SHEET_ID", "YOUR_SHEET_ID")
21+
FOLDER_ID = st.secrets.get("FOLDER_ID", "YOUR_FOLDER_ID")
22+
23+
# Initialize Services
24+
sheets_service = build("sheets", "v4", credentials=creds)
25+
drive_service = build("drive", "v3", credentials=creds)
26+
27+
# Simple Form UI
28+
with st.form("upload_form", clear_on_submit=True):
29+
name = st.text_input("Name")
30+
description = st.text_area("Description")
31+
keywords = st.text_input("Keywords")
32+
uploaded_file = st.file_ Breaker("Choose a file")
33+
34+
submit_button = st.form_submit_button("Submit")
35+
36+
if submit_button:
37+
if not name or not description:
38+
st.error("Please provide both a name and a description.")
39+
else:
40+
with st.spinner("Uploading..."):
41+
file_link = ""
42+
43+
# Handle File Upload to Google Drive
44+
if uploaded_file is not None:
45+
with tempfile.NamedTemporaryFile(delete=False) as tmp:
46+
tmp.write(uploaded_file.getvalue())
47+
tmp_path = tmp.name
48+
49+
try:
50+
file_metadata = {"name": uploaded_file.name, "parents": [FOLDER_ID]}
51+
media = MediaFileUpload(tmp_path, resumable=True)
52+
drive_file = drive_service.files().create(
53+
body=file_metadata, media_body=media, fields="webViewLink"
54+
).execute()
55+
file_link = drive_file.get("webViewLink")
56+
finally:
57+
os.remove(tmp_path)
58+
59+
# Update Google Sheet
60+
row = [[name, description, keywords, file_link]]
61+
sheets_service.spreadsheets().values().append(
62+
spreadsheetId=SHEET_ID,
63+
range="Sheet1!A:D",
64+
valueInputOption="USER_ENTERED",
65+
body={"values": row}
66+
).execute()
67+
68+
st.success(f"Success! Data added to sheet. File link: {file_link}")

0 commit comments

Comments
 (0)