Rammohan0504 commited on
Commit
66f7c8e
·
verified ·
1 Parent(s): 8552cf8

Create Cart.py

Browse files
Files changed (1) hide show
  1. Cart.py +84 -0
Cart.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # In-memory cart to store added items
4
+ cart = []
5
+
6
+ # Menu data
7
+ menu_data = [
8
+ {"name": "Vegetable Biryani", "category": "VEGAN",
9
+ "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/veg_burger.jpg",
10
+ "description": "An Indian dish made to be thoroughly enjoyed by anyone who eats it. Made with the best basmati rice and a perfect blend of spices, cooked in original dum style.",
11
+ "price": "$13.99"},
12
+ ]
13
+
14
+ # Function to add an item to the cart
15
+ def add_to_cart(name, price):
16
+ cart.append({"name": name, "price": price})
17
+ return f"{name} has been added to the cart!", display_cart()
18
+
19
+ # Function to display the cart
20
+ def display_cart():
21
+ if not cart:
22
+ return "Cart is empty"
23
+ html_content = "<div style='display: flex; flex-direction: column; align-items: center; width: 100%;'>"
24
+ for item in cart:
25
+ html_content += f"""
26
+ <div style='margin: 10px; display: flex; justify-content: space-between; width: 80%; padding: 10px;
27
+ border: 1px solid #ddd; border-radius: 10px; background-color: #f9f9f9;'>
28
+ <p style='font-size: 16px; color: #444;'>{item['name']} - {item['price']}</p>
29
+ </div>
30
+ """
31
+ html_content += "</div>"
32
+ return html_content
33
+
34
+ # Function to display the menu
35
+ def display_menu():
36
+ html_content = "<div style='display: flex; flex-direction: column; align-items: center; width: 100%;'>"
37
+ for dish in menu_data:
38
+ html_content += f"""
39
+ <div style='margin: 10px; display: flex; justify-content: space-between; width: 80%; padding: 10px;
40
+ border: 1px solid #ddd; border-radius: 10px; background-color: #f9f9f9;'>
41
+ <div>
42
+ <h4 style='font-size: 18px; color: #444; margin: 0;'>{dish['name']}</h4>
43
+ <p style='font-size: 14px; color: #666; margin: 5px 0;'>{dish['description']}</p>
44
+ <p style='font-size: 16px; color: #444; margin: 0;'>Price: {dish['price']}</p>
45
+ </div>
46
+ <div style='text-align: center;'>
47
+ <img src='{dish['image']}' alt='{dish['name']}'
48
+ style='width: 150px; height: 100px; object-fit: cover; border-radius: 10px;'>
49
+ <button onclick="addToCart('{dish['name']}', '{dish['price']}')"
50
+ style="margin-top: 15px; padding: 10px 20px; background-color: #28a745; color: white; border: none;
51
+ border-radius: 5px; cursor: pointer;">Add to Cart</button>
52
+ </div>
53
+ </div>
54
+ """
55
+ html_content += "</div>"
56
+ return html_content
57
+
58
+ # Gradio App
59
+ with gr.Blocks() as demo:
60
+ gr.HTML("<h1 style='text-align: center; color: #333;'>🍛 Biryani Hub Menu 🍛</h1>")
61
+
62
+ # Display menu
63
+ menu_section = gr.HTML(value=display_menu())
64
+
65
+ # Add to Cart functionality
66
+ name_input = gr.Textbox(label="Dish Name", visible=False)
67
+ price_input = gr.Textbox(label="Dish Price", visible=False)
68
+ add_to_cart_button = gr.Button("Add to Cart", visible=False)
69
+ message_output = gr.Textbox(label="Message", interactive=False)
70
+ cart_display = gr.HTML(value=display_cart())
71
+
72
+ # Add to cart interaction
73
+ add_to_cart_button.click(
74
+ add_to_cart,
75
+ inputs=[name_input, price_input],
76
+ outputs=[message_output, cart_display]
77
+ )
78
+
79
+ # Menu Section with dynamic cart updates
80
+ demo += menu_section
81
+ demo += message_output
82
+ demo += cart_display
83
+
84
+ demo.launch()