Download app.py from NIVED47/admin: direct link, hf CLI and curl.
- Browser
- Download file 2.8 kB
-
https://huggingface.co/spaces/NIVED47/admin/resolve/main/app.py
- Command line
-
hf download hf://spaces/NIVED47/admin/app.py
-
curl -L -o app.py https://huggingface.co/spaces/NIVED47/admin/resolve/main/app.py
2.8 kB
| import gradio as gr | |
| import firebase_admin | |
| from firebase_admin import credentials, firestore | |
| # Firebase Admin SDK initialization | |
| cred = credentials.Certificate("key.json") # Replace with your Firebase service account key file | |
| firebase_admin.initialize_app(cred) | |
| db = firestore.client() | |
| # Function to get orders from Firestore and calculate totals | |
| def fetch_orders(): | |
| orders = db.collection("orders").get() | |
| orders_display = [] | |
| grand_total = 0 | |
| for order in orders: | |
| order_data = order.to_dict() | |
| order_id = order.id # Capture document ID for delete operations | |
| name = order_data.get("name") | |
| phone = order_data.get("phone", "N/A") | |
| hostel = order_data.get("hostel_name", "N/A") | |
| items = order_data.get("items", []) | |
| total_price = order_data.get("total_price", 0) | |
| grand_total += total_price | |
| for item in items: | |
| if isinstance(item, dict): | |
| category = item.get("category", "") | |
| item_name = item.get("item", "") | |
| quantity = item.get("quantity", 0) | |
| price = item.get("price", 0) | |
| orders_display.append([order_id, name, phone, hostel, category, item_name, quantity, price]) | |
| name, phone, hostel = "", "", "" | |
| orders_display.append(["", "", "", "", "Subtotal", "", total_price]) | |
| orders_display.append(["", "", "", "", "Grand Total", "", grand_total]) | |
| return orders_display | |
| # Function to delete an order | |
| def delete_order(order_id): | |
| try: | |
| db.collection("orders").document(order_id).delete() | |
| return f"Order with ID {order_id} deleted successfully!" | |
| except Exception as e: | |
| return f"Failed to delete order: {e}" | |
| # Gradio Interface | |
| def gradio_display_interface(): | |
| with gr.Blocks() as demo: | |
| gr.Markdown("### ADMIN PANEL") | |
| # Display section for individual order totals with headers | |
| orders_display = gr.Dataframe(value=[], headers=["ID", "Name", "Phone", "Hostel", "Category", "Item", "Quantity", "Price"], interactive=False, wrap=True) | |
| # Input field for delete operation | |
| order_id = gr.Textbox(label="Order ID (for Delete)", placeholder="Enter Order ID here") | |
| # Textbox for displaying the result message | |
| result_message = gr.Textbox(label="Result", interactive=False) | |
| # Buttons for operations | |
| fetch_button = gr.Button("Fetch Orders") | |
| delete_button = gr.Button("Delete Order") | |
| # Button functionalities | |
| fetch_button.click(fetch_orders, inputs=[], outputs=[orders_display]) | |
| delete_button.click(delete_order, inputs=[order_id], outputs=[result_message]) | |
| return demo | |
| # Run the Gradio app | |
| if __name__ == "__main__": | |
| gradio_display_interface().launch() | |