Upload 3 files
Browse files- README.md +20 -12
- app.py +51 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,12 +1,20 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Gradio Library Inventory App
|
| 2 |
+
|
| 3 |
+
A minimal multi-user web app for managing book inventory and checkout logs using Gradio and Google Sheets.
|
| 4 |
+
|
| 5 |
+
## 📝 Google Sheet Template
|
| 6 |
+
|
| 7 |
+
Copy this sheet and set its access to "Anyone with the link can edit":
|
| 8 |
+
👉 https://docs.google.com/spreadsheets/d/1u6klpE_4Rm2i-sxXgNf5wK1M-Kj9csQqXQ3M6P8eX40/edit?usp=sharing
|
| 9 |
+
|
| 10 |
+
Make sure your Google Service Account has edit access if using credentials.
|
| 11 |
+
|
| 12 |
+
## 🚀 Deploy on Hugging Face
|
| 13 |
+
|
| 14 |
+
1. Create a new **Gradio Space** on [Hugging Face Spaces](https://huggingface.co/spaces)
|
| 15 |
+
2. Upload:
|
| 16 |
+
- `app.py`
|
| 17 |
+
- `requirements.txt`
|
| 18 |
+
- `credentials.json` (downloaded from Google Cloud Console)
|
| 19 |
+
3. Set visibility to **Private or Protected** to secure your credentials
|
| 20 |
+
4. Your app is ready!
|
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import gspread
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
# Google Sheet ID (replace with your own copy)
|
| 6 |
+
SHEET_ID = "1u6klpE_4Rm2i-sxXgNf5wK1M-Kj9csQqXQ3M6P8eX40"
|
| 7 |
+
|
| 8 |
+
# Connect to public Google Sheet (read/write access required)
|
| 9 |
+
gc = gspread.service_account(filename="credentials.json")
|
| 10 |
+
sheet = gc.open_by_key(SHEET_ID)
|
| 11 |
+
book_sheet = sheet.worksheet("Books")
|
| 12 |
+
log_sheet = sheet.worksheet("Logs")
|
| 13 |
+
|
| 14 |
+
def add_book(title, author, genre):
|
| 15 |
+
next_id = len(book_sheet.get_all_records()) + 1
|
| 16 |
+
book_sheet.append_row([next_id, title, author, genre, "Yes"])
|
| 17 |
+
log_sheet.append_row([len(log_sheet.get_all_records()) + 1, next_id, "Added", "Admin", datetime.now().isoformat()])
|
| 18 |
+
return "✅ Book added!"
|
| 19 |
+
|
| 20 |
+
def view_inventory():
|
| 21 |
+
return book_sheet.get_all_values()
|
| 22 |
+
|
| 23 |
+
def checkout_book(book_id, user):
|
| 24 |
+
books = book_sheet.get_all_records()
|
| 25 |
+
for i, book in enumerate(books):
|
| 26 |
+
if int(book["ID"]) == int(book_id) and book["Availability"] == "Yes":
|
| 27 |
+
book_sheet.update_cell(i+2, 5, "No")
|
| 28 |
+
log_sheet.append_row([len(log_sheet.get_all_records()) + 1, book_id, "Checked Out", user, datetime.now().isoformat()])
|
| 29 |
+
return "✅ Book checked out!"
|
| 30 |
+
return "❌ Book not available or not found"
|
| 31 |
+
|
| 32 |
+
with gr.Blocks() as demo:
|
| 33 |
+
with gr.Tab("Add Book"):
|
| 34 |
+
t, a, g_ = gr.Text(label="Title"), gr.Text(label="Author"), gr.Text(label="Genre")
|
| 35 |
+
add_btn = gr.Button("Add Book")
|
| 36 |
+
out1 = gr.Textbox()
|
| 37 |
+
add_btn.click(add_book, [t, a, g_], out1)
|
| 38 |
+
|
| 39 |
+
with gr.Tab("Inventory"):
|
| 40 |
+
inv_btn = gr.Button("Refresh Inventory")
|
| 41 |
+
table = gr.Dataframe()
|
| 42 |
+
inv_btn.click(view_inventory, outputs=table)
|
| 43 |
+
|
| 44 |
+
with gr.Tab("Checkout Book"):
|
| 45 |
+
book_id = gr.Number(label="Book ID")
|
| 46 |
+
user = gr.Text(label="User Name")
|
| 47 |
+
chk_btn = gr.Button("Check Out")
|
| 48 |
+
out2 = gr.Textbox()
|
| 49 |
+
chk_btn.click(checkout_book, [book_id, user], out2)
|
| 50 |
+
|
| 51 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
gspread
|
| 3 |
+
oauth2client
|