Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import firebase_admin
|
| 3 |
from firebase_admin import credentials, firestore
|
| 4 |
-
from datetime import datetime
|
| 5 |
|
| 6 |
# Firebase Admin SDK initialization
|
| 7 |
cred = credentials.Certificate("key.json") # Replace with your Firebase service account key file
|
|
@@ -10,62 +9,63 @@ db = firestore.client()
|
|
| 10 |
|
| 11 |
# Function to get orders from Firestore and calculate totals
|
| 12 |
def fetch_orders():
|
| 13 |
-
# Fetch orders from Firestore
|
| 14 |
orders = db.collection("orders").get()
|
| 15 |
-
|
| 16 |
-
# Initialize variables to hold display data and grand total
|
| 17 |
orders_display = []
|
| 18 |
grand_total = 0
|
| 19 |
|
| 20 |
-
# Iterate through each order to extract relevant information
|
| 21 |
for order in orders:
|
| 22 |
order_data = order.to_dict()
|
|
|
|
| 23 |
name = order_data.get("name")
|
| 24 |
phone = order_data.get("phone", "N/A")
|
| 25 |
-
hostel = order_data.get("hostel_name", "N/A")
|
| 26 |
items = order_data.get("items", [])
|
| 27 |
-
|
| 28 |
-
# Ensure items is a list of dictionaries
|
| 29 |
-
if not isinstance(items, list):
|
| 30 |
-
items = [] # Default to an empty list if items is not in the correct format
|
| 31 |
-
|
| 32 |
-
# Calculate total for each individual order
|
| 33 |
total_price = order_data.get("total_price", 0)
|
| 34 |
grand_total += total_price
|
| 35 |
|
| 36 |
-
# Append each item as a separate row
|
| 37 |
for item in items:
|
| 38 |
if isinstance(item, dict):
|
| 39 |
category = item.get("category", "")
|
| 40 |
item_name = item.get("item", "")
|
| 41 |
quantity = item.get("quantity", 0)
|
| 42 |
price = item.get("price", 0)
|
| 43 |
-
orders_display.append([name, phone, hostel, category, item_name, quantity, price])
|
| 44 |
-
# Clear name, phone, and hostel for subsequent items for a cleaner look
|
| 45 |
name, phone, hostel = "", "", ""
|
| 46 |
|
| 47 |
-
# Append a subtotal row after all items for the current person
|
| 48 |
orders_display.append(["", "", "", "", "Subtotal", "", total_price])
|
| 49 |
|
| 50 |
-
# Append a grand total row at the end
|
| 51 |
orders_display.append(["", "", "", "", "Grand Total", "", grand_total])
|
| 52 |
-
|
| 53 |
return orders_display
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
# Gradio Interface
|
| 56 |
def gradio_display_interface():
|
| 57 |
with gr.Blocks() as demo:
|
| 58 |
-
# Display section for individual order totals with headers
|
| 59 |
gr.Markdown("### ADMIN PANEL")
|
| 60 |
-
orders_display = gr.Dataframe(value=[], headers=["Name", "Phone", "Hostel", "Category", "Item", "Quantity", "Price"], interactive=False, wrap=True)
|
| 61 |
|
| 62 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
fetch_button = gr.Button("Fetch Orders")
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
)
|
| 69 |
|
| 70 |
return demo
|
| 71 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import firebase_admin
|
| 3 |
from firebase_admin import credentials, firestore
|
|
|
|
| 4 |
|
| 5 |
# Firebase Admin SDK initialization
|
| 6 |
cred = credentials.Certificate("key.json") # Replace with your Firebase service account key file
|
|
|
|
| 9 |
|
| 10 |
# Function to get orders from Firestore and calculate totals
|
| 11 |
def fetch_orders():
|
|
|
|
| 12 |
orders = db.collection("orders").get()
|
|
|
|
|
|
|
| 13 |
orders_display = []
|
| 14 |
grand_total = 0
|
| 15 |
|
|
|
|
| 16 |
for order in orders:
|
| 17 |
order_data = order.to_dict()
|
| 18 |
+
order_id = order.id # Capture document ID for delete operations
|
| 19 |
name = order_data.get("name")
|
| 20 |
phone = order_data.get("phone", "N/A")
|
| 21 |
+
hostel = order_data.get("hostel_name", "N/A")
|
| 22 |
items = order_data.get("items", [])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
total_price = order_data.get("total_price", 0)
|
| 24 |
grand_total += total_price
|
| 25 |
|
|
|
|
| 26 |
for item in items:
|
| 27 |
if isinstance(item, dict):
|
| 28 |
category = item.get("category", "")
|
| 29 |
item_name = item.get("item", "")
|
| 30 |
quantity = item.get("quantity", 0)
|
| 31 |
price = item.get("price", 0)
|
| 32 |
+
orders_display.append([order_id, name, phone, hostel, category, item_name, quantity, price])
|
|
|
|
| 33 |
name, phone, hostel = "", "", ""
|
| 34 |
|
|
|
|
| 35 |
orders_display.append(["", "", "", "", "Subtotal", "", total_price])
|
| 36 |
|
|
|
|
| 37 |
orders_display.append(["", "", "", "", "Grand Total", "", grand_total])
|
|
|
|
| 38 |
return orders_display
|
| 39 |
|
| 40 |
+
# Function to delete an order
|
| 41 |
+
def delete_order(order_id):
|
| 42 |
+
try:
|
| 43 |
+
db.collection("orders").document(order_id).delete()
|
| 44 |
+
return f"Order with ID {order_id} deleted successfully!"
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Failed to delete order: {e}"
|
| 47 |
+
|
| 48 |
# Gradio Interface
|
| 49 |
def gradio_display_interface():
|
| 50 |
with gr.Blocks() as demo:
|
|
|
|
| 51 |
gr.Markdown("### ADMIN PANEL")
|
|
|
|
| 52 |
|
| 53 |
+
# Display section for individual order totals with headers
|
| 54 |
+
orders_display = gr.Dataframe(value=[], headers=["ID", "Name", "Phone", "Hostel", "Category", "Item", "Quantity", "Price"], interactive=False, wrap=True)
|
| 55 |
+
|
| 56 |
+
# Input field for delete operation
|
| 57 |
+
order_id = gr.Textbox(label="Order ID (for Delete)", placeholder="Enter Order ID here")
|
| 58 |
+
|
| 59 |
+
# Textbox for displaying the result message
|
| 60 |
+
result_message = gr.Textbox(label="Result", interactive=False)
|
| 61 |
+
|
| 62 |
+
# Buttons for operations
|
| 63 |
fetch_button = gr.Button("Fetch Orders")
|
| 64 |
+
delete_button = gr.Button("Delete Order")
|
| 65 |
+
|
| 66 |
+
# Button functionalities
|
| 67 |
+
fetch_button.click(fetch_orders, inputs=[], outputs=[orders_display])
|
| 68 |
+
delete_button.click(delete_order, inputs=[order_id], outputs=[result_message])
|
| 69 |
|
| 70 |
return demo
|
| 71 |
|