Rammohan0504 commited on
Commit
38d896c
·
verified ·
1 Parent(s): 225bb54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -51
app.py CHANGED
@@ -1,22 +1,34 @@
1
  import gradio as gr
2
 
3
- # Menu data
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,56 +41,75 @@ def display_dishes():
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 }));
55
- }
56
- }
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()
 
 
 
 
1
  import gradio as gr
2
 
3
+ # Data Module
4
  menu_data = [
5
+ {
6
+ "name": "Veg Burger",
7
+ "category": "VEGAN",
8
+ "image": "https://upload.wikimedia.org/wikipedia/commons/6/6e/Veggie_burger.jpg",
9
+ "description": "A delicious vegan burger with plant-based patty, lettuce, and tomato."
10
+ },
11
+ {
12
+ "name": "Chicken Biryani",
13
+ "category": "HALAL",
14
+ "image": "https://upload.wikimedia.org/wikipedia/commons/7/79/Chicken_Biryani.jpg",
15
+ "description": "Spicy chicken biryani with aromatic basmati rice and tender chicken pieces."
16
+ },
17
+ {
18
+ "name": "Paneer Butter Masala",
19
+ "category": "VEGAN",
20
+ "image": "https://upload.wikimedia.org/wikipedia/commons/3/3d/Paneer_Butter_Masala.jpg",
21
+ "description": "Paneer cooked in a rich and creamy tomato-based gravy."
22
+ },
23
  ]
24
 
25
+ # HTML Rendering Module
26
+ def render_dish_grid(category):
27
+ filtered_dishes = [
28
+ dish for dish in menu_data if category == "ALL" or dish["category"] == category
29
+ ]
30
  html_content = "<div style='display: flex; flex-wrap: wrap; justify-content: center;'>"
31
+ for dish in filtered_dishes:
32
  html_content += f"""
33
  <div style='margin: 10px; text-align: center; cursor: pointer; border: 1px solid #ddd;
34
  padding: 10px; border-radius: 10px; width: 200px; background-color: #f9f9f9;'
 
41
  html_content += "</div>"
42
  return html_content
43
 
44
+ # Business Logic Module
45
  def get_dish_details(dish_name):
46
  for dish in menu_data:
47
  if dish["name"] == dish_name:
48
  return gr.update(visible=True), dish["image"], dish["name"], dish["description"]
49
  return gr.update(visible=False), "", "Dish Not Found", "No description available."
50
 
 
51
  def close_popup():
52
  return gr.update(visible=False)
53
 
54
+ # Gradio App Module
55
+ def create_app():
56
+ with gr.Blocks() as demo:
57
+ gr.HTML("<h1 style='text-align: center;'>🍛 Biryani Menu 🍛</h1>")
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
+ # JavaScript for dish selection
60
+ gr.HTML("""
61
+ <script>
62
+ function selectDish(dishName) {
63
+ let input = document.querySelector('input[aria-label="selected_dish_name"]');
64
+ if (input) {
65
+ input.value = dishName;
66
+ input.dispatchEvent(new Event('input', { bubbles: true }));
67
+ }
68
+ }
69
+ </script>
70
+ """)
71
 
72
+ gr.Markdown("### Select a Category")
 
 
73
  with gr.Row():
74
+ btn_all = gr.Button("ALL")
75
+ btn_vegan = gr.Button("VEGAN")
76
+ btn_halal = gr.Button("HALAL")
77
+
78
+ gr.Markdown("### Available Dishes")
79
+ dish_display = gr.HTML(value=render_dish_grid("ALL"))
80
+
81
+ # Pop-up for dish details
82
+ popup = gr.Group(visible=False)
83
+ with popup:
84
+ with gr.Row():
85
+ dish_image = gr.Image()
86
+ with gr.Column():
87
+ dish_name = gr.Textbox(label="Dish Name", interactive=False)
88
+ dish_description = gr.Textbox(label="Description", interactive=False)
89
+ close_button = gr.Button("Close")
90
+
91
+ # Hidden textbox to handle JavaScript -> Python communication
92
+ selected_dish_name = gr.Textbox(visible=False)
93
+ selected_dish_name.change(
94
+ fn=get_dish_details,
95
+ inputs=selected_dish_name,
96
+ outputs=[popup, dish_image, dish_name, dish_description]
97
+ )
98
+
99
+ # Close button functionality
100
+ close_button.click(fn=close_popup, outputs=popup)
101
+
102
+ # Update category buttons
103
+ def update_dish_grid(category):
104
+ return gr.update(value=render_dish_grid(category))
105
 
106
+ btn_all.click(fn=lambda: update_dish_grid("ALL"), outputs=dish_display)
107
+ btn_vegan.click(fn=lambda: update_dish_grid("VEGAN"), outputs=dish_display)
108
+ btn_halal.click(fn=lambda: update_dish_grid("HALAL"), outputs=dish_display)
 
 
 
 
109
 
110
+ return demo
 
111
 
112
+ # Main Execution
113
+ if __name__ == "__main__":
114
+ app = create_app()
115
+ app.launch()