55from googleapiclient .discovery import build
66from googleapiclient .http import MediaFileUpload
77
8- # App Title
9- st .title ("Scientific Slide Library Uploader" )
8+ # Set page title
9+ st .set_page_config (page_title = "Scientific Slide Library" , page_icon = "🔬" )
10+ st .title ("🔬 Scientific Slide Library Uploader" )
11+ st .markdown ("Use the form below to upload your slides and update the tracking sheet." )
1012
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 )
13+ # 1. AUTHENTICATION (Using Streamlit Secrets)
14+ try :
15+ # Get IDs and JSON contents from Secrets
16+ SHEET_ID = st .secrets ["SHEET_ID" ]
17+ FOLDER_ID = st .secrets ["FOLDER_ID" ]
18+
19+ # Map the service account keys from the [gcp_service_account] section in Secrets
20+ credentials_info = st .secrets ["gcp_service_account" ]
21+
22+ creds = service_account .Credentials .from_service_account_info (
23+ credentials_info ,
24+ scopes = [
25+ "https://www.googleapis.com/auth/spreadsheets" ,
26+ "https://www.googleapis.com/auth/drive.file"
27+ ]
28+ )
29+
30+ # Build the API services
31+ sheets_service = build ("sheets" , "v4" , credentials = creds )
32+ drive_service = build ("drive" , "v3" , credentials = creds )
33+ except Exception as e :
34+ st .error ("⚠️ Authentication Error: Please check your Streamlit 'Secrets' configuration." )
35+ st .stop ()
2636
27- # Simple Form UI
37+ # 2. THE UPLOAD FORM
2838with 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" )
39+ name = st .text_input ("Presenter Name" )
40+ description = st .text_area ("Topic/Description" )
41+ keywords = st .text_input ("Keywords (comma separated)" )
42+
43+ # Corrected function name: st.file_uploader
44+ uploaded_file = st .file_uploader ("Choose Presentation File (PDF, PPTX, etc.)" )
3345
34- submit_button = st .form_submit_button ("Submit" )
46+ submit_button = st .form_submit_button ("Submit Entry " )
3547
48+ # 3. PROCESSING
3649if submit_button :
3750 if not name or not description :
38- st .error ("Please provide both a name and a description." )
51+ st .warning ("Please provide at least a name and a description." )
3952 else :
40- with st .spinner ("Uploading.. ." ):
41- file_link = ""
53+ with st .spinner ("Processing upload... please wait ." ):
54+ file_link = "No file uploaded "
4255
43- # Handle File Upload to Google Drive
56+ # --- STEP A: UPLOAD TO GOOGLE DRIVE ---
4457 if uploaded_file is not None :
58+ # Write memory file to a temp file on disk for the Google API to read
4559 with tempfile .NamedTemporaryFile (delete = False ) as tmp :
4660 tmp .write (uploaded_file .getvalue ())
4761 tmp_path = tmp .name
4862
4963 try :
50- file_metadata = {"name" : uploaded_file .name , "parents" : [FOLDER_ID ]}
64+ file_metadata = {
65+ "name" : uploaded_file .name ,
66+ "parents" : [FOLDER_ID ]
67+ }
5168 media = MediaFileUpload (tmp_path , resumable = True )
52- drive_file = drive_service .files ().create (
53- body = file_metadata , media_body = media , fields = "webViewLink"
69+
70+ # Create the file in Drive
71+ uploaded_drive_file = drive_service .files ().create (
72+ body = file_metadata ,
73+ media_body = media ,
74+ fields = "webViewLink"
5475 ).execute ()
55- file_link = drive_file .get ("webViewLink" )
76+
77+ file_link = uploaded_drive_file .get ("webViewLink" )
78+ except Exception as e :
79+ st .error (f"Drive Error: { e } " )
5680 finally :
57- os .remove (tmp_path )
81+ # Cleanup the temporary file from the server
82+ if os .path .exists (tmp_path ):
83+ os .remove (tmp_path )
5884
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 } " )
85+ # --- STEP B: UPDATE GOOGLE SHEET ---
86+ try :
87+ # Prepare row data
88+ row_data = [[name , description , keywords , file_link ]]
89+
90+ sheets_service .spreadsheets ().values ().append (
91+ spreadsheetId = SHEET_ID ,
92+ range = "Sheet1!A1" ,
93+ valueInputOption = "USER_ENTERED" ,
94+ body = {"values" : row_data }
95+ ).execute ()
96+
97+ st .success ("✅ Entry recorded successfully!" )
98+ st .balloons ()
99+ except Exception as e :
100+ st .error (f"Sheets Error: { e } " )
0 commit comments