Rammohan0504 commited on
Commit
225bb54
Β·
verified Β·
1 Parent(s): c145638

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -42
app.py CHANGED
@@ -4,27 +4,19 @@ import gradio as gr
4
  menu_data = [
5
  {"name": "Veg Burger", "category": "VEGAN",
6
  "image": "https://upload.wikimedia.org/wikipedia/commons/6/6e/Veggie_burger.jpg",
7
- "description": "A delicious vegan burger with plant-based patty, lettuce, and tomato.",
8
- "spice_level": "🌢 Mild", "portion_size": "πŸ§† Small"},
9
  {"name": "Chicken Biryani", "category": "HALAL",
10
  "image": "https://upload.wikimedia.org/wikipedia/commons/7/79/Chicken_Biryani.jpg",
11
- "description": "Spicy chicken biryani with aromatic basmati rice and tender chicken pieces.",
12
- "spice_level": "🌢🌢 Medium", "portion_size": "πŸ› Large"},
13
  {"name": "Paneer Butter Masala", "category": "VEGAN",
14
  "image": "https://upload.wikimedia.org/wikipedia/commons/3/3d/Paneer_Butter_Masala.jpg",
15
- "description": "Paneer cooked in a rich and creamy tomato-based gravy.",
16
- "spice_level": "🌢🌢🌢 High", "portion_size": "🍽 Medium"},
17
  ]
18
 
19
- # Dropdown options
20
- spice_options = ["🌢 Mild", "🌢🌢 Medium", "🌢🌢🌢 High"]
21
- portion_options = ["πŸ§† Small", "🍽 Medium", "πŸ› Large"]
22
-
23
- # Function to generate dish display HTML
24
- def display_dishes(category):
25
- filtered_dishes = [dish for dish in menu_data if category == "ALL" or dish["category"] == category]
26
  html_content = "<div style='display: flex; flex-wrap: wrap; justify-content: center;'>"
27
- for dish in filtered_dishes:
28
  html_content += f"""
29
  <div style='margin: 10px; text-align: center; cursor: pointer; border: 1px solid #ddd;
30
  padding: 10px; border-radius: 10px; width: 200px; background-color: #f9f9f9;'
@@ -37,22 +29,26 @@ def display_dishes(category):
37
  html_content += "</div>"
38
  return html_content
39
 
40
- # Function to retrieve dish details
41
  def get_dish_details(dish_name):
42
  for dish in menu_data:
43
  if dish["name"] == dish_name:
44
- return dish["image"], dish["name"], dish["description"], dish["spice_level"], dish["portion_size"]
45
- return "https://via.placeholder.com/300x200", "Unknown Dish", "No description available.", "🌢 Mild", "πŸ§† Small"
46
 
47
- # Create the Gradio app
48
- with gr.Blocks() as demo:
49
- gr.HTML("<h1 style='text-align: center;'>πŸ› Biryani Hub Menu πŸ›</h1>")
50
 
 
 
 
 
51
  # JavaScript for dish selection
52
  gr.HTML("""
53
  <script>
54
  function selectDish(dishName) {
55
- const input = document.querySelector('input[aria-label="selected_dish_name"]');
56
  if (input) {
57
  input.value = dishName;
58
  input.dispatchEvent(new Event('input', { bubbles: true }));
@@ -61,41 +57,28 @@ with gr.Blocks() as demo:
61
  </script>
62
  """)
63
 
64
- gr.Markdown("### Select a Category")
65
- with gr.Row():
66
- btn_all = gr.Button("ALL")
67
- btn_vegan = gr.Button("VEGAN")
68
- btn_halal = gr.Button("HALAL")
69
-
70
  gr.Markdown("### Available Dishes")
71
- dish_display = gr.HTML(value=display_dishes("ALL"))
72
 
73
  # Pop-up for dish details
74
  popup = gr.Group(visible=False)
75
  with popup:
76
  with gr.Row():
77
- dish_image = gr.Image(label="Dish Image", height=200, width=200)
78
  with gr.Column():
79
  dish_name = gr.Textbox(label="Dish Name", interactive=False)
80
  dish_description = gr.Textbox(label="Description", interactive=False)
81
- spice_dropdown = gr.Radio(label="Select Spice Level", choices=spice_options, value="🌢 Mild")
82
- portion_dropdown = gr.Radio(label="Select Portion Size", choices=portion_options, value="πŸ§† Small")
83
- close_popup = gr.Button("Close")
84
-
85
- def update_dishes(category):
86
- return gr.update(value=display_dishes(category))
87
-
88
- btn_all.click(fn=lambda: update_dishes("ALL"), outputs=dish_display)
89
- btn_vegan.click(fn=lambda: update_dishes("VEGAN"), outputs=dish_display)
90
- btn_halal.click(fn=lambda: update_dishes("HALAL"), outputs=dish_display)
91
 
 
92
  selected_dish_name = gr.Textbox(visible=False)
93
  selected_dish_name.change(
94
- fn=lambda dish_name: (*get_dish_details(dish_name), gr.update(visible=True)),
95
  inputs=selected_dish_name,
96
- outputs=[dish_image, dish_name, dish_description, spice_dropdown, portion_dropdown, popup],
97
  )
98
 
99
- close_popup.click(fn=lambda: gr.update(visible=False), outputs=popup)
 
100
 
101
  demo.launch()
 
4
  menu_data = [
5
  {"name": "Veg Burger", "category": "VEGAN",
6
  "image": "https://upload.wikimedia.org/wikipedia/commons/6/6e/Veggie_burger.jpg",
7
+ "description": "A delicious vegan burger with plant-based patty, lettuce, and tomato."},
 
8
  {"name": "Chicken Biryani", "category": "HALAL",
9
  "image": "https://upload.wikimedia.org/wikipedia/commons/7/79/Chicken_Biryani.jpg",
10
+ "description": "Spicy chicken biryani with aromatic basmati rice and tender chicken pieces."},
 
11
  {"name": "Paneer Butter Masala", "category": "VEGAN",
12
  "image": "https://upload.wikimedia.org/wikipedia/commons/3/3d/Paneer_Butter_Masala.jpg",
13
+ "description": "Paneer cooked in a rich and creamy tomato-based gravy."},
 
14
  ]
15
 
16
+ # Function to display dishes
17
+ def display_dishes():
 
 
 
 
 
18
  html_content = "<div style='display: flex; flex-wrap: wrap; justify-content: center;'>"
19
+ for dish in menu_data:
20
  html_content += f"""
21
  <div style='margin: 10px; text-align: center; cursor: pointer; border: 1px solid #ddd;
22
  padding: 10px; border-radius: 10px; width: 200px; background-color: #f9f9f9;'
 
29
  html_content += "</div>"
30
  return html_content
31
 
32
+ # Function to show details of a selected dish
33
  def get_dish_details(dish_name):
34
  for dish in menu_data:
35
  if dish["name"] == dish_name:
36
+ return gr.update(visible=True), dish["image"], dish["name"], dish["description"]
37
+ return gr.update(visible=False), "", "Dish Not Found", "No description available."
38
 
39
+ # Function to close the pop-up
40
+ def close_popup():
41
+ return gr.update(visible=False)
42
 
43
+ # Gradio App
44
+ with gr.Blocks() as demo:
45
+ gr.HTML("<h1 style='text-align: center;'>πŸ› Biryani Menu πŸ›</h1>")
46
+
47
  # JavaScript for dish selection
48
  gr.HTML("""
49
  <script>
50
  function selectDish(dishName) {
51
+ let input = document.querySelector('input[aria-label="selected_dish_name"]');
52
  if (input) {
53
  input.value = dishName;
54
  input.dispatchEvent(new Event('input', { bubbles: true }));
 
57
  </script>
58
  """)
59
 
 
 
 
 
 
 
60
  gr.Markdown("### Available Dishes")
61
+ dish_display = gr.HTML(value=display_dishes())
62
 
63
  # Pop-up for dish details
64
  popup = gr.Group(visible=False)
65
  with popup:
66
  with gr.Row():
67
+ dish_image = gr.Image()
68
  with gr.Column():
69
  dish_name = gr.Textbox(label="Dish Name", interactive=False)
70
  dish_description = gr.Textbox(label="Description", interactive=False)
71
+ close_button = gr.Button("Close")
 
 
 
 
 
 
 
 
 
72
 
73
+ # Hidden textbox to handle JavaScript -> Python communication
74
  selected_dish_name = gr.Textbox(visible=False)
75
  selected_dish_name.change(
76
+ fn=get_dish_details,
77
  inputs=selected_dish_name,
78
+ outputs=[popup, dish_image, dish_name, dish_description]
79
  )
80
 
81
+ # Close button functionality
82
+ close_button.click(fn=close_popup, outputs=popup)
83
 
84
  demo.launch()