Rammohan0504 commited on
Commit
73238bb
·
verified ·
1 Parent(s): 5642ef4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -40
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
 
3
- # Extended menu data with dish details
4
  menu_data = [
5
  {"name": "Veg Burger", "category": "VEGAN",
6
  "image": "https://upload.wikimedia.org/wikipedia/commons/6/6e/Veggie_burger.jpg",
@@ -30,54 +30,31 @@ def filter_dishes(category):
30
  return menu_data
31
  return [dish for dish in menu_data if dish["category"] == category]
32
 
33
- # Function to display dishes with images
34
- def display_dishes(category):
35
- filtered_dishes = filter_dishes(category)
36
- html_content = "<div style='display: flex; flex-wrap: wrap; justify-content: center;'>"
37
- for dish in filtered_dishes:
38
- html_content += f"""
39
- <div style='margin: 10px; text-align: center; cursor: pointer; border: 1px solid #ddd;
40
- padding: 10px; border-radius: 10px; width: 200px;'
41
- onclick='selectDish("{dish['name']}")'>
42
- <img src='{dish['image']}' alt='{dish['name']}'
43
- style='width: 180px; height: 120px; object-fit: cover; border-radius: 10px;'>
44
- <h4 style='margin-top: 10px; font-size: 16px;'>{dish['name']}</h4>
45
- </div>
46
- """
47
- html_content += "</div>"
48
- return html_content
49
-
50
- # Function to fetch details of the selected dish
51
  def get_dish_details(dish_name):
52
  for dish in menu_data:
53
  if dish["name"] == dish_name:
54
- return (gr.update(visible=True), dish["image"], dish["name"], dish["description"],
55
- dish["spice_level"], dish["portion_size"])
56
- return (gr.update(visible=True), "https://via.placeholder.com/300x200", "Unknown Dish",
57
- "No description available.", "N/A", "N/A")
58
-
59
- # Function to simulate placing an order
60
- def place_order(dish_name):
61
- return f"Your order for '{dish_name}' has been placed successfully!"
62
 
63
  # Main Gradio App
64
  with gr.Blocks() as demo:
65
  gr.HTML("<h1 style='text-align: center;'>Biryani Hub Menu</h1>")
66
 
67
- # Buttons for category selection
68
  gr.Markdown("### Select a Category")
69
  with gr.Row():
70
  btn_all = gr.Button("ALL")
71
  btn_vegan = gr.Button("VEGAN")
72
  btn_halal = gr.Button("HALAL")
73
 
74
- # Dish display section
75
  gr.Markdown("### Available Dishes")
76
- dish_display = gr.HTML(value=display_dishes("ALL"))
77
 
78
- # Dish details section (hidden initially)
79
  with gr.Row(visible=False) as dish_details:
80
- dish_image = gr.Image(label="Dish Image", type="filepath", width=200, height=200)
81
  with gr.Column():
82
  dish_name = gr.Textbox(label="Dish Name", interactive=False)
83
  dish_description = gr.Textbox(label="Description", interactive=False)
@@ -86,29 +63,51 @@ with gr.Blocks() as demo:
86
  place_order_btn = gr.Button("Place Order")
87
  order_status = gr.Textbox(label="Order Status", interactive=False)
88
 
89
- # Event: Update dishes for selected category
90
  def update_dishes(category):
91
- return gr.update(value=display_dishes(category)), gr.update(visible=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
 
93
  btn_all.click(fn=lambda: update_dishes("ALL"), outputs=[dish_display, dish_details])
94
  btn_vegan.click(fn=lambda: update_dishes("VEGAN"), outputs=[dish_display, dish_details])
95
  btn_halal.click(fn=lambda: update_dishes("HALAL"), outputs=[dish_display, dish_details])
96
 
97
- # Bind the selected dish name to details display
98
  selected_dish_name = gr.Textbox(visible=False)
99
- selected_dish_name.change(fn=get_dish_details, inputs=selected_dish_name,
100
  outputs=[dish_details, dish_image, dish_name, dish_description, dish_spice, dish_portion])
101
 
102
- # Place order button event
103
  place_order_btn.click(fn=place_order, inputs=dish_name, outputs=order_status)
104
 
105
  # Inject JavaScript for dish selection
106
  gr.HTML("""
107
  <script>
108
  function selectDish(dishName) {
109
- const textbox = document.querySelector('input[aria-label="selected_dish_name"]');
110
- textbox.value = dishName;
111
- textbox.dispatchEvent(new Event('input'));
112
  }
113
  </script>
114
  """)
 
1
  import gradio as gr
2
 
3
+ # Menu data with dish details
4
  menu_data = [
5
  {"name": "Veg Burger", "category": "VEGAN",
6
  "image": "https://upload.wikimedia.org/wikipedia/commons/6/6e/Veggie_burger.jpg",
 
30
  return menu_data
31
  return [dish for dish in menu_data if dish["category"] == category]
32
 
33
+ # Function to fetch dish details
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  def get_dish_details(dish_name):
35
  for dish in menu_data:
36
  if dish["name"] == dish_name:
37
+ return (dish["image"], dish["name"], dish["description"], dish["spice_level"], dish["portion_size"])
38
+ return ("https://via.placeholder.com/300x200", "Unknown Dish", "No description available.", "N/A", "N/A")
 
 
 
 
 
 
39
 
40
  # Main Gradio App
41
  with gr.Blocks() as demo:
42
  gr.HTML("<h1 style='text-align: center;'>Biryani Hub Menu</h1>")
43
 
44
+ # Category Buttons
45
  gr.Markdown("### Select a Category")
46
  with gr.Row():
47
  btn_all = gr.Button("ALL")
48
  btn_vegan = gr.Button("VEGAN")
49
  btn_halal = gr.Button("HALAL")
50
 
51
+ # Dish Display Section
52
  gr.Markdown("### Available Dishes")
53
+ dish_display = gr.HTML(value="", interactive=False)
54
 
55
+ # Dish Details Section (Hidden Initially)
56
  with gr.Row(visible=False) as dish_details:
57
+ dish_image = gr.Image(label="Dish Image", height=200, width=200)
58
  with gr.Column():
59
  dish_name = gr.Textbox(label="Dish Name", interactive=False)
60
  dish_description = gr.Textbox(label="Description", interactive=False)
 
63
  place_order_btn = gr.Button("Place Order")
64
  order_status = gr.Textbox(label="Order Status", interactive=False)
65
 
66
+ # Function to update the dish display based on category
67
  def update_dishes(category):
68
+ dishes = filter_dishes(category)
69
+ html_content = "<div style='display: flex; flex-wrap: wrap; justify-content: center;'>"
70
+ for dish in dishes:
71
+ html_content += f"""
72
+ <div style='margin: 10px; text-align: center; cursor: pointer; border: 1px solid #ddd;
73
+ padding: 10px; border-radius: 10px; width: 200px;'
74
+ onclick='selectDish("{dish['name']}")'>
75
+ <img src='{dish['image']}' alt='{dish['name']}'
76
+ style='width: 180px; height: 120px; object-fit: cover; border-radius: 10px;'>
77
+ <h4 style='margin-top: 10px; font-size: 16px;'>{dish['name']}</h4>
78
+ </div>
79
+ """
80
+ html_content += "</div>"
81
+ return html_content, gr.update(visible=False)
82
+
83
+ # Event: Show dish details when a dish is selected
84
+ def show_dish_details(dish_name):
85
+ image, name, description, spice, portion = get_dish_details(dish_name)
86
+ return gr.update(visible=True), image, name, description, spice, portion
87
+
88
+ # Event: Place Order
89
+ def place_order(dish_name):
90
+ return f"Your order for '{dish_name}' has been placed successfully!"
91
 
92
+ # Buttons for category filtering
93
  btn_all.click(fn=lambda: update_dishes("ALL"), outputs=[dish_display, dish_details])
94
  btn_vegan.click(fn=lambda: update_dishes("VEGAN"), outputs=[dish_display, dish_details])
95
  btn_halal.click(fn=lambda: update_dishes("HALAL"), outputs=[dish_display, dish_details])
96
 
97
+ # Hidden input for JS to Gradio interaction
98
  selected_dish_name = gr.Textbox(visible=False)
99
+ selected_dish_name.change(fn=show_dish_details, inputs=selected_dish_name,
100
  outputs=[dish_details, dish_image, dish_name, dish_description, dish_spice, dish_portion])
101
 
102
+ # Place order button
103
  place_order_btn.click(fn=place_order, inputs=dish_name, outputs=order_status)
104
 
105
  # Inject JavaScript for dish selection
106
  gr.HTML("""
107
  <script>
108
  function selectDish(dishName) {
109
+ document.querySelector('input[aria-label="selected_dish_name"]').value = dishName;
110
+ document.querySelector('input[aria-label="selected_dish_name"]').dispatchEvent(new Event('input'));
 
111
  }
112
  </script>
113
  """)