Rammohan0504 commited on
Commit
654b785
·
verified ·
1 Parent(s): d61ec43

Create cart_tab.py

Browse files
Files changed (1) hide show
  1. cart_tab.py +155 -0
cart_tab.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # Initialize cart as a global variable
4
+ cart = []
5
+
6
+ # Function to add items to the cart
7
+ def add_to_cart(item_name, spice_level, extras):
8
+ cart.append({
9
+ "item_name": item_name,
10
+ "spice_level": spice_level,
11
+ "extras": extras
12
+ })
13
+ return f"{item_name} added to cart!"
14
+
15
+ # Function to display cart items
16
+ def display_cart():
17
+ if not cart:
18
+ return "Your cart is empty."
19
+ cart_content = "<h3>Your Cart:</h3><ul>"
20
+ for item in cart:
21
+ cart_content += f"<li>{item['item_name']} - Spice Level: {item['spice_level']} - Extras: {', '.join(item['extras'])}</li>"
22
+ cart_content += "</ul>"
23
+ return cart_content
24
+
25
+ menu_data = [
26
+ {"name": "Veg Burger", "category": "VEGAN",
27
+ "image": "https://huggingface.co/spaces/Rammohan0504/dynamic_menu/resolve/main/images/veg_burger.jpg",
28
+ "description": "A delicious vegan burger with plant-based patty, lettuce, and tomato.",
29
+ "price": "$8.99"},
30
+ # Add other menu items here
31
+ ]
32
+
33
+ # JavaScript for popup modal
34
+ popup_js = """
35
+ <script>
36
+ function showPopup(name, description, price, image) {
37
+ const popup = document.getElementById("custom-popup");
38
+ const overlay = document.getElementById("overlay");
39
+ popup.innerHTML = `
40
+ <img src="${image}" alt="${name}" style="width: 100%; height: 250px; border-radius: 10px; margin-bottom: 10px;">
41
+ <h3 style="margin-top: 10px;">${name}</h3>
42
+ <p>${description}</p>
43
+ <p style="font-weight: bold;">Price: ${price}</p>
44
+ <h4 style="margin-top: 20px;">Choose a Spice Level</h4>
45
+ <select id="spice-level" style="width: 100%; padding: 5px;">
46
+ <option value="American Mild">American Mild</option>
47
+ <option value="American Medium">American Medium</option>
48
+ <option value="American Spicy">American Spicy</option>
49
+ <option value="Indian Mild">Indian Mild</option>
50
+ <option value="Indian Medium">Indian Medium</option>
51
+ <option value="Indian Spicy">Indian Spicy</option>
52
+ <option value="Indian Very Spicy">Indian Very Spicy</option>
53
+ </select>
54
+ <h4 style="margin-top: 20px;">Choose Extras</h4>
55
+ <div>
56
+ <label><input type="checkbox" name="extras" value="Extra Raitha 4oz"> Extra Raitha 4oz</label><br>
57
+ <label><input type="checkbox" name="extras" value="Extra Raitha 8oz"> Extra Raitha 8oz</label><br>
58
+ <label><input type="checkbox" name="extras" value="Extra Onion"> Extra Onion</label><br>
59
+ <label><input type="checkbox" name="extras" value="Extra Onion & Lemon"> Extra Onion & Lemon</label><br>
60
+ </div>
61
+ <button onclick="addToCart('${name}')" style="margin-top: 20px; padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer;">Add to Cart</button>
62
+ <button onclick="closePopup()" style="margin-top: 20px; margin-left: 10px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;">Close</button>
63
+ `;
64
+ popup.style.display = "block";
65
+ overlay.style.display = "block";
66
+ }
67
+
68
+ function closePopup() {
69
+ const popup = document.getElementById("custom-popup");
70
+ const overlay = document.getElementById("overlay");
71
+ popup.style.display = "none";
72
+ overlay.style.display = "none";
73
+ }
74
+
75
+ function addToCart(name) {
76
+ const spiceLevel = document.getElementById("spice-level").value;
77
+ const extras = Array.from(document.querySelectorAll("input[name='extras']:checked")).map(el => el.value);
78
+ google.colab.kernel.invokeFunction('notebook.add_to_cart', [name, spiceLevel, extras], {});
79
+ closePopup();
80
+ }
81
+ </script>
82
+ """
83
+
84
+ # HTML for popup modal
85
+ popup_html = """
86
+ <div id="overlay" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); display: none; z-index: 999;"></div>
87
+ <div id="custom-popup"
88
+ style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 50%; max-width: 400px; background: white; padding: 20px; border-radius: 10px; box-shadow: 0px 4px 6px rgba(0,0,0,0.1); display: none; z-index: 1000; text-align: center;">
89
+ </div>
90
+ """
91
+
92
+ # Generate dish cards
93
+ def display_dishes(category):
94
+ html_content = "<div style='display: flex; flex-direction: column; gap: 20px; width: 80%; margin: 0 auto;'>"
95
+ for dish in menu_data:
96
+ if category == "ALL" or dish["category"] == category:
97
+ html_content += f"""
98
+ <div style='display: flex; align-items: center; justify-content: space-between; padding: 20px; border: 1px solid #ddd; border-radius: 10px; background-color: #f9f9f9;'>
99
+ <div style='flex: 3; text-align: left;'>
100
+ <h3 style='margin: 0; font-size: 20px;'>{dish['name']}</h3>
101
+ <p style='margin: 5px 0; font-size: 14px; color: #555;'>{dish['description']}</p>
102
+ <p style='margin: 0; font-size: 16px; font-weight: bold;'>Price: {dish['price']}</p>
103
+ </div>
104
+ <div style='flex: 1; text-align: center;'>
105
+ <img src='{dish['image']}' alt='{dish['name']}' style='width: 120px; height: 80px; object-fit: cover; border-radius: 10px;'>
106
+ <button onclick="showPopup('{dish['name']}', '{dish['description']}', '{dish['price']}', '{dish['image']}')"
107
+ style='margin-top: 10px; padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;'>
108
+ Add
109
+ </button>
110
+ </div>
111
+ </div>
112
+ """
113
+ html_content += "</div>"
114
+ return html_content
115
+
116
+ # Main Gradio App
117
+ with gr.Blocks() as demo:
118
+ gr.HTML(popup_html) # Add the popup container and overlay
119
+ gr.HTML(popup_js) # Add the JavaScript
120
+
121
+ gr.HTML("<h1 style='text-align: center;'>🍛 Biryani Hub Menu 🍛</h1>")
122
+
123
+ with gr.Tabs():
124
+ with gr.Tab("Menu"):
125
+ with gr.Row():
126
+ btn_all = gr.Button("ALL")
127
+ btn_vegan = gr.Button("VEGAN")
128
+ btn_halal = gr.Button("HALAL")
129
+
130
+ dish_display = gr.HTML(value=display_dishes("ALL"))
131
+
132
+ btn_all.click(
133
+ lambda: gr.update(value=display_dishes("ALL")),
134
+ outputs=dish_display
135
+ )
136
+ btn_vegan.click(
137
+ lambda: gr.update(value=display_dishes("VEGAN")),
138
+ outputs=dish_display
139
+ )
140
+ btn_halal.click(
141
+ lambda: gr.update(value=display_dishes("HALAL")),
142
+ outputs=dish_display
143
+ )
144
+
145
+ with gr.Tab("Cart"):
146
+ cart_display = gr.HTML(value=display_cart())
147
+ gr.Button("Refresh Cart").click(
148
+ lambda: gr.update(value=display_cart()),
149
+ outputs=cart_display
150
+ )
151
+
152
+ # Register function for JavaScript callback
153
+ demo.add_to_cart = add_to_cart
154
+
155
+ demo.launch()