dschandra commited on
Commit
fa52bb5
·
verified ·
1 Parent(s): c57f3fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -35
app.py CHANGED
@@ -48,9 +48,9 @@ def display_menu():
48
  <p style="font-size: 12px; color: gray;">{item['description']}</p>
49
  </div>
50
  <div style="margin-left: auto; display: flex; align-items: center;">
51
- <button onclick="decrease_quantity('{item['name']}')" style="background-color: #dc3545; color: white; border: none; padding: 5px; border-radius: 5px;">-</button>
52
  <span style="margin: 0 10px;">{cart[item['name']]['quantity']}</span>
53
- <button onclick="increase_quantity('{item['name']}')" style="background-color: #28a745; color: white; border: none; padding: 5px; border-radius: 5px;">+</button>
54
  </div>
55
  </div>
56
  """
@@ -63,14 +63,13 @@ def display_menu():
63
  <h4 style="margin: 0;">{item['name']} - ${item['price']}</h4>
64
  <p style="font-size: 12px; color: gray;">{item['description']}</p>
65
  </div>
66
- <button name="add-{item['name']}" style="background-color: #28a745; color: white; border: none; padding: 8px 12px; border-radius: 5px; cursor: pointer; margin-left: auto;">Add</button>
 
67
  </div>
68
  """
69
  return menu_html
70
 
71
-
72
  # Function to add items to the cart
73
- # Add to Cart Logic
74
  def add_to_cart(item_name):
75
  """
76
  Add an item to the cart and return the updated menu and cart summary.
@@ -88,42 +87,20 @@ def add_to_cart(item_name):
88
  break
89
  return display_menu(), update_cart_summary()
90
 
91
-
92
  # Function to update the cart summary
93
  def update_cart_summary():
 
 
 
94
  num_items = sum(item["quantity"] for item in cart.values())
95
  total_price = sum(item["price"] * item["quantity"] for item in cart.values())
96
- return f"{num_items} items added", f"Total: ${total_price:.2f}"
97
-
98
- # Function to render the cart page
99
- def display_cart():
100
- if not cart:
101
- return "Your cart is empty."
102
-
103
- cart_html = "<h3>Your Cart:</h3>"
104
- total_price = 0
105
- for item in cart.values():
106
- item_total = item["price"] * item["quantity"]
107
- total_price += item_total
108
- cart_html += f"""
109
- <div style="display: flex; align-items: center; margin-bottom: 15px; border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
110
- <h4 style="margin: 0;">{item['name']}</h4>
111
- <div style="margin-left: auto; display: flex; align-items: center;">
112
- <button onclick="update_quantity('{item['name']}', 'decrease')" style="background-color: #dc3545; color: white; border: none; padding: 5px; border-radius: 5px;">-</button>
113
- <span style="margin: 0 10px;">{item['quantity']}</span>
114
- <button onclick="update_quantity('{item['name']}', 'increase')" style="background-color: #28a745; color: white; border: none; padding: 5px; border-radius: 5px;">+</button>
115
- </div>
116
- <p style="margin: 0 0 0 15px;">${item_total:.2f}</p>
117
- </div>
118
- """
119
- cart_html += f"<h4>Total: ${total_price:.2f}</h4>"
120
- cart_html += '<button style="padding: 10px; background-color: #28a745; color: white; border: none; border-radius: 5px;">Place Order</button>'
121
- return cart_html
122
 
123
  # Gradio Interface
124
  with gr.Blocks() as demo:
125
  gr.Markdown("# Dynamic Food Menu")
126
 
 
127
  menu_html = gr.HTML(value=display_menu())
128
  cart_summary = gr.HTML(value="No items added to cart.")
129
 
@@ -135,8 +112,7 @@ with gr.Blocks() as demo:
135
  add_button.click(fn=add_to_cart, inputs=item_name_input, outputs=[menu_html, cart_summary])
136
 
137
  # Display Menu and Cart Summary
138
- menu_html.render()
139
- cart_summary.render()
140
 
141
  demo.launch()
142
-
 
48
  <p style="font-size: 12px; color: gray;">{item['description']}</p>
49
  </div>
50
  <div style="margin-left: auto; display: flex; align-items: center;">
51
+ <button style="background-color: #dc3545; color: white; border: none; padding: 5px; border-radius: 5px;">-</button>
52
  <span style="margin: 0 10px;">{cart[item['name']]['quantity']}</span>
53
+ <button style="background-color: #28a745; color: white; border: none; padding: 5px; border-radius: 5px;">+</button>
54
  </div>
55
  </div>
56
  """
 
63
  <h4 style="margin: 0;">{item['name']} - ${item['price']}</h4>
64
  <p style="font-size: 12px; color: gray;">{item['description']}</p>
65
  </div>
66
+ <button style="background-color: #28a745; color: white; border: none; padding: 8px 12px; border-radius: 5px; cursor: pointer; margin-left: auto;"
67
+ onclick="add_to_cart('{item['name']}')">Add</button>
68
  </div>
69
  """
70
  return menu_html
71
 
 
72
  # Function to add items to the cart
 
73
  def add_to_cart(item_name):
74
  """
75
  Add an item to the cart and return the updated menu and cart summary.
 
87
  break
88
  return display_menu(), update_cart_summary()
89
 
 
90
  # Function to update the cart summary
91
  def update_cart_summary():
92
+ """
93
+ Update the cart summary with the total number of items and the total price.
94
+ """
95
  num_items = sum(item["quantity"] for item in cart.values())
96
  total_price = sum(item["price"] * item["quantity"] for item in cart.values())
97
+ return f"{num_items} items added to cart. Total: ${total_price:.2f}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  # Gradio Interface
100
  with gr.Blocks() as demo:
101
  gr.Markdown("# Dynamic Food Menu")
102
 
103
+ # Menu display
104
  menu_html = gr.HTML(value=display_menu())
105
  cart_summary = gr.HTML(value="No items added to cart.")
106
 
 
112
  add_button.click(fn=add_to_cart, inputs=item_name_input, outputs=[menu_html, cart_summary])
113
 
114
  # Display Menu and Cart Summary
115
+ demo.append(menu_html)
116
+ demo.append(cart_summary)
117
 
118
  demo.launch()