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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -39
app.py CHANGED
@@ -45,9 +45,9 @@ def display_menu():
45
  <p style="font-size: 12px; color: gray;">{item['description']}</p>
46
  </div>
47
  <div style="margin-left: auto; display: flex; align-items: center;">
48
- <button onclick="decrease_quantity('{item['name']}')" style="background-color: #dc3545; color: white; border: none; padding: 5px; border-radius: 5px;">-</button>
49
  <span style="margin: 0 10px;">{cart[item['name']]['quantity']}</span>
50
- <button onclick="increase_quantity('{item['name']}')" style="background-color: #28a745; color: white; border: none; padding: 5px; border-radius: 5px;">+</button>
51
  </div>
52
  </div>
53
  """
@@ -60,7 +60,8 @@ def display_menu():
60
  <h4 style="margin: 0;">{item['name']} - ${item['price']}</h4>
61
  <p style="font-size: 12px; color: gray;">{item['description']}</p>
62
  </div>
63
- <button id="add-{item['name']}" style="background-color: #28a745; color: white; border: none; padding: 8px 12px; border-radius: 5px; cursor: pointer; margin-left: auto;">Add</button>
 
64
  </div>
65
  """
66
  return menu_html
@@ -78,53 +79,30 @@ def add_to_cart(item_name):
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()
 
45
  <p style="font-size: 12px; color: gray;">{item['description']}</p>
46
  </div>
47
  <div style="margin-left: auto; display: flex; align-items: center;">
48
+ <button onclick="update_quantity('{item['name']}', 'decrease')" style="background-color: #dc3545; color: white; border: none; padding: 5px; border-radius: 5px;">-</button>
49
  <span style="margin: 0 10px;">{cart[item['name']]['quantity']}</span>
50
+ <button onclick="update_quantity('{item['name']}', 'increase')" style="background-color: #28a745; color: white; border: none; padding: 5px; border-radius: 5px;">+</button>
51
  </div>
52
  </div>
53
  """
 
60
  <h4 style="margin: 0;">{item['name']} - ${item['price']}</h4>
61
  <p style="font-size: 12px; color: gray;">{item['description']}</p>
62
  </div>
63
+ <button style="background-color: #28a745; color: white; border: none; padding: 8px 12px; border-radius: 5px; cursor: pointer; margin-left: auto;"
64
+ onclick="add_to_cart('{item['name']}')">Add</button>
65
  </div>
66
  """
67
  return menu_html
 
79
  "quantity": 1
80
  }
81
  break
82
+ return display_menu(), update_cart_summary()
83
 
84
  # Function to update the cart summary
85
  def update_cart_summary():
86
  num_items = sum(item["quantity"] for item in cart.values())
87
  total_price = sum(item["price"] * item["quantity"] for item in cart.values())
88
+ return f"{num_items} items added to cart. Total: ${total_price:.2f}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  # Gradio Interface
91
  with gr.Blocks() as demo:
92
  gr.Markdown("# Dynamic Food Menu")
93
 
94
+ menu_html = gr.HTML(value=display_menu())
95
+ cart_summary = gr.HTML(value="No items added to cart.")
96
+
97
+ # Hidden input fields for Gradio's event handling
98
+ item_name_input = gr.Textbox(visible=False)
99
 
100
+ # Add to Cart button logic
101
+ add_button = gr.Button("Add to Cart", visible=False)
102
+ add_button.click(fn=add_to_cart, inputs=item_name_input, outputs=[menu_html, cart_summary])
 
103
 
104
+ # Add the menu and cart summary to the interface
105
+ menu_html.render()
106
+ cart_summary.render()
107
 
108
  demo.launch()