khanhlinhnguyen commited on
Commit
54adca8
·
verified ·
1 Parent(s): fc83f42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -22
app.py CHANGED
@@ -8,7 +8,7 @@ menu = {
8
  "Starters": [
9
  {
10
  "name": "A1 - Summer Rolls (Vegan)",
11
- "description": (
12
  "Rice paper, fresh carrots, cucumbers, with mint, cilantro rolled with rice noodles and tofu."
13
  " Served with Hoisin and peanut butter sauce."
14
  ),
@@ -240,24 +240,29 @@ with gr.Blocks() as demo:
240
  with gr.TabItem(section):
241
  for item in items:
242
  with gr.Row():
243
- # Show the dish image via HTML so the client downloads it directly
244
- gr.HTML(
245
- f"<img src='{item.get('image', '')}' alt='{item.get('name', '')}' "
246
- "style='height:90px;width:120px;border-radius:8px;'/>"
247
- )
248
- # Show the dish name, description and bold price
249
- with gr.Column():
 
250
  name = item.get("name", "")
251
  desc = item.get("description", "")
252
  price_str = item.get("price", "")
253
- price_html = f"<span style='font-size:1.1em;font-weight:bold;color:green;'>{price_str}</span>"
 
 
254
  gr.Markdown(
255
  f"**{name}**\n\n{desc}\n\n{price_html}",
256
  show_label=False,
257
  )
258
- # Create a blank button to act as the clickable area for adding the item
259
- add_btn = gr.Button(value="")
260
- button_refs.append((item, add_btn))
 
 
261
  # Add spacing before cart
262
  gr.Markdown("<br><br>")
263
  # Cart display section
@@ -274,28 +279,31 @@ with gr.Blocks() as demo:
274
  decision_output = gr.Markdown(visible=False)
275
  confirm_button = gr.Button("✅ I have made my decision")
276
  change_button = gr.Button("🔄 Wait, I want to make change", visible=False)
277
- # Register button click events to add items to cart
 
 
 
 
278
  for _item, _btn in button_refs:
279
  def _on_add(cart_state, item=_item):
 
 
280
  df, total, new_cart = add_item_simple(item, cart_state)
281
- cart_table.update(value=df)
282
- total_display.update(value=total)
283
- return new_cart
284
  _btn.click(
285
  _on_add,
286
  inputs=[cart_state],
287
- outputs=[cart_state],
288
  )
289
- # Update cart when user edits quantities manually
 
290
  def _on_table_edit(df, cart_state):
291
  df_new, total, new_cart = update_cart_from_table(df, cart_state)
292
- cart_table.update(value=df_new)
293
- total_display.update(value=total)
294
- return new_cart
295
  cart_table.change(
296
  _on_table_edit,
297
  inputs=[cart_table, cart_state],
298
- outputs=[cart_state],
299
  )
300
  # Confirm button logic
301
  def _confirm_wrapper(cart_df, note):
 
8
  "Starters": [
9
  {
10
  "name": "A1 - Summer Rolls (Vegan)",
11
+ "description": (
12
  "Rice paper, fresh carrots, cucumbers, with mint, cilantro rolled with rice noodles and tofu."
13
  " Served with Hoisin and peanut butter sauce."
14
  ),
 
240
  with gr.TabItem(section):
241
  for item in items:
242
  with gr.Row():
243
+ # Column for the image
244
+ with gr.Column(scale=1):
245
+ gr.HTML(
246
+ f"<img src='{item.get('image', '')}' alt='{item.get('name', '')}' "
247
+ "style='height:90px;width:120px;border-radius:8px;'/>"
248
+ )
249
+ # Column for the dish details
250
+ with gr.Column(scale=4):
251
  name = item.get("name", "")
252
  desc = item.get("description", "")
253
  price_str = item.get("price", "")
254
+ price_html = (
255
+ f"<span style='font-size:1.1em;font-weight:bold;color:green;'>{price_str}</span>"
256
+ )
257
  gr.Markdown(
258
  f"**{name}**\n\n{desc}\n\n{price_html}",
259
  show_label=False,
260
  )
261
+ # Column for the clickable grey bar (blank button). The scale ensures it fills
262
+ # the remaining horizontal space and is easy to click.
263
+ with gr.Column(scale=2):
264
+ add_btn = gr.Button(value="")
265
+ button_refs.append((item, add_btn))
266
  # Add spacing before cart
267
  gr.Markdown("<br><br>")
268
  # Cart display section
 
279
  decision_output = gr.Markdown(visible=False)
280
  confirm_button = gr.Button("✅ I have made my decision")
281
  change_button = gr.Button("🔄 Wait, I want to make change", visible=False)
282
+ # Register button click events to add items to cart. Each click returns
283
+ # updated cart table, total and cart state so that the UI updates
284
+ # properly. Without specifying the outputs and returning the updates,
285
+ # the click handler may not refresh the displayed DataFrame in some
286
+ # environments (e.g. Hugging Face Spaces).
287
  for _item, _btn in button_refs:
288
  def _on_add(cart_state, item=_item):
289
+ # Add the selected dish to the cart and compute the new
290
+ # DataFrame and total. Return them to update the UI.
291
  df, total, new_cart = add_item_simple(item, cart_state)
292
+ return df, total, new_cart
 
 
293
  _btn.click(
294
  _on_add,
295
  inputs=[cart_state],
296
+ outputs=[cart_table, total_display, cart_state],
297
  )
298
+ # Update cart when user edits quantities manually. Return the updated
299
+ # DataFrame, total and cart state to refresh the display.
300
  def _on_table_edit(df, cart_state):
301
  df_new, total, new_cart = update_cart_from_table(df, cart_state)
302
+ return df_new, total, new_cart
 
 
303
  cart_table.change(
304
  _on_table_edit,
305
  inputs=[cart_table, cart_state],
306
+ outputs=[cart_table, total_display, cart_state],
307
  )
308
  # Confirm button logic
309
  def _confirm_wrapper(cart_df, note):