dschandra commited on
Commit
e83450b
·
verified ·
1 Parent(s): c06dd10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -33
app.py CHANGED
@@ -31,10 +31,8 @@ def load_menu(file_path="menu.xlsx"):
31
  menu_data = load_menu("menu.xlsx")
32
  cart = {}
33
 
 
34
  def display_menu():
35
- """
36
- Render the menu dynamically as HTML.
37
- """
38
  menu_html = ""
39
  for item in menu_data:
40
  if item["name"] in cart:
@@ -67,10 +65,8 @@ def display_menu():
67
  """
68
  return menu_html
69
 
 
70
  def add_to_cart(item_name):
71
- """
72
- Add an item to the cart.
73
- """
74
  if item_name in cart:
75
  cart[item_name]["quantity"] += 1
76
  else:
@@ -82,40 +78,53 @@ def add_to_cart(item_name):
82
  "quantity": 1
83
  }
84
  break
85
- return display_menu()
86
 
87
- def update_quantity(item_name, operation):
88
- """
89
- Update the quantity of an item in the cart.
90
- """
91
- if item_name in cart:
92
- if operation == "increase":
93
- cart[item_name]["quantity"] += 1
94
- elif operation == "decrease":
95
- cart[item_name]["quantity"] -= 1
96
- if cart[item_name]["quantity"] <= 0:
97
- del cart[item_name]
98
- return display_menu()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
  # Gradio Interface
101
  with gr.Blocks() as demo:
102
  gr.Markdown("# Dynamic Food Menu")
103
 
104
- menu_html = gr.HTML(value=display_menu())
105
-
106
- # Hidden input fields for Gradio's event handling
107
- item_name_input = gr.Textbox(visible=False)
108
- operation_input = gr.Textbox(visible=False)
109
-
110
- # Simulate "Add to Cart" and "Update Quantity"
111
- add_button = gr.Button("Add to Cart", visible=False)
112
- update_button = gr.Button("Update Quantity", visible=False)
113
 
114
- # Update menu after adding or updating quantity
115
- add_button.click(fn=add_to_cart, inputs=item_name_input, outputs=menu_html)
116
- update_button.click(fn=update_quantity, inputs=[item_name_input, operation_input], outputs=menu_html)
 
117
 
118
- # Add components directly without .render()
119
- menu_html
120
 
121
  demo.launch()
 
31
  menu_data = load_menu("menu.xlsx")
32
  cart = {}
33
 
34
+ # Function to render the menu
35
  def display_menu():
 
 
 
36
  menu_html = ""
37
  for item in menu_data:
38
  if item["name"] in cart:
 
65
  """
66
  return menu_html
67
 
68
+ # Function to add items to the cart
69
  def add_to_cart(item_name):
 
 
 
70
  if item_name in cart:
71
  cart[item_name]["quantity"] += 1
72
  else:
 
78
  "quantity": 1
79
  }
80
  break
81
+ return update_cart_summary()
82
 
83
+ # Function to update the cart summary
84
+ def update_cart_summary():
85
+ num_items = sum(item["quantity"] for item in cart.values())
86
+ total_price = sum(item["price"] * item["quantity"] for item in cart.values())
87
+ return f"{num_items} items added", f"Total: ${total_price:.2f}"
88
+
89
+ # Function to render the cart page
90
+ def display_cart():
91
+ if not cart:
92
+ return "Your cart is empty."
93
+
94
+ cart_html = "<h3>Your Cart:</h3>"
95
+ total_price = 0
96
+ for item in cart.values():
97
+ item_total = item["price"] * item["quantity"]
98
+ total_price += item_total
99
+ cart_html += f"""
100
+ <div style="display: flex; align-items: center; margin-bottom: 15px; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
101
+ <h4 style="margin: 0;">{item['name']}</h4>
102
+ <div style="margin-left: auto; display: flex; align-items: center;">
103
+ <button onclick="update_quantity('{item['name']}', 'decrease')" style="background-color: #dc3545; color: white; border: none; padding: 5px; border-radius: 5px;">-</button>
104
+ <span style="margin: 0 10px;">{item['quantity']}</span>
105
+ <button onclick="update_quantity('{item['name']}', 'increase')" style="background-color: #28a745; color: white; border: none; padding: 5px; border-radius: 5px;">+</button>
106
+ </div>
107
+ <p style="margin: 0 0 0 15px;">${item_total:.2f}</p>
108
+ </div>
109
+ """
110
+ cart_html += f"<h4>Total: ${total_price:.2f}</h4>"
111
+ cart_html += '<button style="padding: 10px; background-color: #28a745; color: white; border: none; border-radius: 5px;">Place Order</button>'
112
+ return cart_html
113
 
114
  # Gradio Interface
115
  with gr.Blocks() as demo:
116
  gr.Markdown("# Dynamic Food Menu")
117
 
118
+ with gr.Tab("Menu"):
119
+ menu_html = gr.HTML(value=display_menu())
120
+ cart_summary = gr.HTML(value="No items added to cart.")
 
 
 
 
 
 
121
 
122
+ # Add to Cart buttons
123
+ item_name_input = gr.Textbox(visible=False)
124
+ add_button = gr.Button("Add to Cart", visible=False)
125
+ add_button.click(add_to_cart, inputs=item_name_input, outputs=[menu_html, cart_summary])
126
 
127
+ with gr.Tab("Cart"):
128
+ cart_html = gr.HTML(value=display_cart())
129
 
130
  demo.launch()