Delete app.py
Browse files
app.py
DELETED
|
@@ -1,88 +0,0 @@
|
|
| 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 |
-
|
| 11 |
-
# Open the Google Sheet tabs
|
| 12 |
-
book_sheet = client.open("LibraryLog").worksheet("Books")
|
| 13 |
-
log_sheet = client.open("LibraryLog").worksheet("BorrowLog")
|
| 14 |
-
|
| 15 |
-
# Helper: Fetch book dropdown list
|
| 16 |
-
def get_book_list():
|
| 17 |
-
records = book_sheet.get_all_records()
|
| 18 |
-
return [f"{r['Book ID']} - {r['Title']}" for r in records if r["Available Copies"] > 0]
|
| 19 |
-
|
| 20 |
-
# Log issuing or returning a book
|
| 21 |
-
def process_log(book_info, student_name, action, condition):
|
| 22 |
-
if not book_info or not student_name:
|
| 23 |
-
return "โ Book and student name required."
|
| 24 |
-
|
| 25 |
-
book_id, title = book_info.split(" - ", 1)
|
| 26 |
-
timestamp = datetime.now().strftime("%Y-%m-%d")
|
| 27 |
-
records = book_sheet.get_all_records()
|
| 28 |
-
log_records = log_sheet.get_all_records()
|
| 29 |
-
|
| 30 |
-
book_row = next((i+2 for i, r in enumerate(records) if r["Book ID"] == book_id), None)
|
| 31 |
-
if not book_row:
|
| 32 |
-
return "โ Book not found in sheet."
|
| 33 |
-
|
| 34 |
-
# Issue a book
|
| 35 |
-
if action == "Issue":
|
| 36 |
-
available = int(records[book_row-2]["Available Copies"])
|
| 37 |
-
if available <= 0:
|
| 38 |
-
return "โ No copies available."
|
| 39 |
-
book_sheet.update_cell(book_row, 5, available - 1)
|
| 40 |
-
log_sheet.append_row([
|
| 41 |
-
f"L{len(log_records)+1:03d}", book_id, title, student_name,
|
| 42 |
-
"Issued", timestamp, "", ""
|
| 43 |
-
])
|
| 44 |
-
return f"โ
Book issued to {student_name}"
|
| 45 |
-
|
| 46 |
-
# Return a book
|
| 47 |
-
else:
|
| 48 |
-
# Find last unreturned log for student and book
|
| 49 |
-
for i, r in reversed(list(enumerate(log_records))):
|
| 50 |
-
if r["Book ID"] == book_id and r["Student Name"] == student_name and r["Status"] == "Issued":
|
| 51 |
-
log_row = i + 2 # account for header
|
| 52 |
-
log_sheet.update(f"E{log_row}:H{log_row}", [["Returned", r["Date"], timestamp, condition]])
|
| 53 |
-
available = int(records[book_row-2]["Available Copies"])
|
| 54 |
-
book_sheet.update_cell(book_row, 5, available + 1)
|
| 55 |
-
return f"โ
Book returned by {student_name}"
|
| 56 |
-
return "โ No issued log found for this student/book."
|
| 57 |
-
|
| 58 |
-
# Search borrow log
|
| 59 |
-
def search_logs(keyword):
|
| 60 |
-
logs = log_sheet.get_all_records()
|
| 61 |
-
result = [
|
| 62 |
-
f"{r['Date']} | {r['Book Title']} | {r['Student Name']} | {r['Status']} | {r['Condition on Return']}"
|
| 63 |
-
for r in logs if keyword.lower() in r["Book Title"].lower() or keyword.lower() in r["Student Name"].lower()
|
| 64 |
-
]
|
| 65 |
-
return "\n".join(result) if result else "No results found."
|
| 66 |
-
|
| 67 |
-
with gr.Blocks() as demo:
|
| 68 |
-
gr.Markdown("## ๐ Library Log System (Google Sheets Connected)")
|
| 69 |
-
|
| 70 |
-
book_dropdown = gr.Dropdown(choices=get_book_list(), label="Select Book")
|
| 71 |
-
student = gr.Textbox(label="Student Name")
|
| 72 |
-
action = gr.Radio(["Issue", "Return"], label="Action", value="Issue")
|
| 73 |
-
condition = gr.Textbox(label="Condition on Return (only if returning)", visible=False)
|
| 74 |
-
result = gr.Textbox(label="Result")
|
| 75 |
-
|
| 76 |
-
def toggle_condition(value):
|
| 77 |
-
return gr.update(visible=(value == "Return"))
|
| 78 |
-
|
| 79 |
-
action.change(fn=toggle_condition, inputs=action, outputs=condition)
|
| 80 |
-
submit = gr.Button("Submit")
|
| 81 |
-
submit.click(fn=process_log, inputs=[book_dropdown, student, action, condition], outputs=result)
|
| 82 |
-
|
| 83 |
-
gr.Markdown("### ๐ Search Logs")
|
| 84 |
-
search = gr.Textbox(label="Search by Book or Student")
|
| 85 |
-
logs_output = gr.Textbox(label="Results", lines=10)
|
| 86 |
-
search.change(search_logs, search, logs_output)
|
| 87 |
-
|
| 88 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|