Bubbles30 commited on
Commit
1e89d56
·
verified ·
1 Parent(s): 122b2db

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import gspread
3
+ from oauth2client.service_account import ServiceAccountCredentials
4
+ from datetime import datetime
5
+
6
+ # Setup Google Sheets connection
7
+ scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
8
+ creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
9
+ client = gspread.authorize(creds)
10
+ sheet = client.open("LibraryLog").sheet1
11
+
12
+ def log_book(book_name, student_name, status):
13
+ if not book_name or not student_name:
14
+ return "❌ Book name and student name required!"
15
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
16
+ sheet.append_row([timestamp, book_name, student_name, status])
17
+ return "✅ Log saved!"
18
+
19
+ def search_logs(keyword):
20
+ records = sheet.get_all_records()
21
+ results = [
22
+ f"{r['Timestamp']} | {r['Book Name']} | {r['Student Name']} | {r['Status']}"
23
+ for r in records
24
+ if keyword.lower() in r["Book Name"].lower() or keyword.lower() in r["Student Name"].lower()
25
+ ]
26
+ return "\n".join(results) if results else "No results."
27
+
28
+ with gr.Blocks() as demo:
29
+ gr.Markdown("### 📚 Library Log App")
30
+ with gr.Row():
31
+ book = gr.Textbox(label="Book Name")
32
+ student = gr.Textbox(label="Student Name")
33
+ status = gr.Radio(["Issued", "Returned"], label="Status")
34
+ log_button = gr.Button("Log")
35
+ result = gr.Textbox(label="Output")
36
+ log_button.click(log_book, [book, student, status], result)
37
+
38
+ gr.Markdown("### 🔍 Search Logs")
39
+ search_input = gr.Textbox(label="Search")
40
+ search_output = gr.Textbox(label="Results", lines=10)
41
+ search_input.change(search_logs, search_input, search_output)
42
+
43
+ demo.launch()