Download app.py from Bubbles30/lib: direct link, hf CLI and curl.
- Browser
- Download file 1.92 kB
-
https://huggingface.co/spaces/Bubbles30/lib/resolve/main/app.py
- Command line
-
hf download hf://spaces/Bubbles30/lib/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Bubbles30/lib/resolve/main/app.py
1.92 kB
| import gradio as gr | |
| import gspread | |
| from datetime import datetime | |
| # Google Sheet ID (replace with your own copy) | |
| SHEET_ID = "1u6klpE_4Rm2i-sxXgNf5wK1M-Kj9csQqXQ3M6P8eX40" | |
| # Connect to public Google Sheet (read/write access required) | |
| gc = gspread.service_account(filename="credentials.json") | |
| sheet = gc.open_by_key(SHEET_ID) | |
| book_sheet = sheet.worksheet("Books") | |
| log_sheet = sheet.worksheet("Logs") | |
| def add_book(title, author, genre): | |
| next_id = len(book_sheet.get_all_records()) + 1 | |
| book_sheet.append_row([next_id, title, author, genre, "Yes"]) | |
| log_sheet.append_row([len(log_sheet.get_all_records()) + 1, next_id, "Added", "Admin", datetime.now().isoformat()]) | |
| return "✅ Book added!" | |
| def view_inventory(): | |
| return book_sheet.get_all_values() | |
| def checkout_book(book_id, user): | |
| books = book_sheet.get_all_records() | |
| for i, book in enumerate(books): | |
| if int(book["ID"]) == int(book_id) and book["Availability"] == "Yes": | |
| book_sheet.update_cell(i+2, 5, "No") | |
| log_sheet.append_row([len(log_sheet.get_all_records()) + 1, book_id, "Checked Out", user, datetime.now().isoformat()]) | |
| return "✅ Book checked out!" | |
| return "❌ Book not available or not found" | |
| with gr.Blocks() as demo: | |
| with gr.Tab("Add Book"): | |
| t, a, g_ = gr.Text(label="Title"), gr.Text(label="Author"), gr.Text(label="Genre") | |
| add_btn = gr.Button("Add Book") | |
| out1 = gr.Textbox() | |
| add_btn.click(add_book, [t, a, g_], out1) | |
| with gr.Tab("Inventory"): | |
| inv_btn = gr.Button("Refresh Inventory") | |
| table = gr.Dataframe() | |
| inv_btn.click(view_inventory, outputs=table) | |
| with gr.Tab("Checkout Book"): | |
| book_id = gr.Number(label="Book ID") | |
| user = gr.Text(label="User Name") | |
| chk_btn = gr.Button("Check Out") | |
| out2 = gr.Textbox() | |
| chk_btn.click(checkout_book, [book_id, user], out2) | |
| demo.launch() |