Rammohan0504 commited on
Commit
e6da0b9
·
verified ·
1 Parent(s): fb87a2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -58
app.py CHANGED
@@ -46,9 +46,7 @@ def display_dishes(category):
46
  <h3 style='margin: 10px 0; font-size: 18px;'>{dish['name']}</h3>
47
  <p style='margin: 5px 0; font-size: 14px; color: #555;'>{dish['description']}</p>
48
  <p style='margin: 10px 0; font-size: 16px; font-weight: bold;'>Price: {dish['price']}</p>
49
- <button style='padding: 10px 20px; background-color: #28a745; color: white; border: none;
50
- border-radius: 5px; cursor: pointer;'
51
- onclick="showPopup('{dish['name']}', '{dish['description']}', '{dish['price']}', '{dish['image']}')">
52
  Add to Cart
53
  </button>
54
  </div>
@@ -56,72 +54,37 @@ def display_dishes(category):
56
  html_content += "</div>"
57
  return html_content
58
 
59
- # JavaScript for handling the popup
60
  popup_js = """
61
  <script>
62
- async function showPopup(name, description, price, image) {
63
- const popup = document.getElementById("custom-popup");
64
- const overlay = document.getElementById("overlay");
65
- popup.innerHTML = `
66
- <button onclick="closePopup()" style="position: absolute; top: 10px; right: 10px; background: none; border: none; font-size: 20px; cursor: pointer;">&times;</button>
67
- <img src="${image}" alt="${name}" style="width: 100%; height: 200px; object-fit: cover; border-radius: 10px; margin-bottom: 10px;">
68
- <h3>${name}</h3>
69
- <p>${description}</p>
70
- <p style="font-weight: bold;">Price: ${price}</p>
71
- <button id="add-to-cart-button" style="padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;">
72
- Add to Cart
73
- </button>
74
- `;
75
- popup.style.display = "block";
76
- overlay.style.display = "block";
77
-
78
- document.getElementById("add-to-cart-button").onclick = async function () {
79
- try {
80
- const response = await fetch("/add_to_cart", {
81
- method: "POST",
82
- headers: { "Content-Type": "application/json" },
83
- body: JSON.stringify({ item_name: name })
84
- });
85
-
86
- const result = await response.json();
87
-
88
- if (result.status === "success") {
89
- alert(result.message); // Show success message
90
- } else {
91
- alert("Error adding item to cart: " + result.message);
92
- }
93
- } catch (error) {
94
- console.error("Error:", error);
95
- alert("Something went wrong while adding the item.");
96
  }
97
-
98
- closePopup();
99
- };
100
- }
101
-
102
- function closePopup() {
103
- document.getElementById("custom-popup").style.display = "none";
104
- document.getElementById("overlay").style.display = "none";
105
  }
106
  </script>
107
  """
108
 
109
- # HTML structure for the popup
110
- popup_html = """
111
- <div id="overlay" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); display: none; z-index: 999;"></div>
112
- <div id="custom-popup"
113
- style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 400px; max-width: 90%; background: white; padding: 20px; border-radius: 10px; box-shadow: 0px 4px 6px rgba(0,0,0,0.1); display: none; z-index: 1000; position: relative;">
114
- </div>
115
- """
116
-
117
-
118
  # Main Gradio app
119
  with gr.Blocks() as demo:
120
- gr.HTML(popup_html) # Add popup container
121
- gr.HTML(popup_js) # Add popup JavaScript
122
-
123
  gr.HTML("<h1 style='text-align: center;'>🍛 Welcome to the Biryani Hub 🍛</h1>")
 
124
 
 
125
  with gr.Tabs():
126
  # Menu Tab
127
  with gr.Tab("Menu"):
@@ -154,4 +117,5 @@ with gr.Blocks() as demo:
154
  outputs=cart_display
155
  )
156
 
 
157
  demo.launch()
 
46
  <h3 style='margin: 10px 0; font-size: 18px;'>{dish['name']}</h3>
47
  <p style='margin: 5px 0; font-size: 14px; color: #555;'>{dish['description']}</p>
48
  <p style='margin: 10px 0; font-size: 16px; font-weight: bold;'>Price: {dish['price']}</p>
49
+ <button onclick="addItemToCart('{dish['name']}')" style='padding: 10px 20px; background-color: #28a745; color: white; border: none; border-radius: 5px; cursor: pointer;'>
 
 
50
  Add to Cart
51
  </button>
52
  </div>
 
54
  html_content += "</div>"
55
  return html_content
56
 
57
+ # JavaScript for Popup Handling
58
  popup_js = """
59
  <script>
60
+ async function addItemToCart(itemName) {
61
+ try {
62
+ const response = await fetch("/add_to_cart", {
63
+ method: "POST",
64
+ headers: { "Content-Type": "application/json" },
65
+ body: JSON.stringify({ item_name: itemName })
66
+ });
67
+ const result = await response.json();
68
+
69
+ if (result.status === "success") {
70
+ alert(result.message); // Show success message
71
+ } else {
72
+ alert("Error: " + result.message); // Show error message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  }
74
+ } catch (error) {
75
+ console.error("Error:", error);
76
+ alert("Something went wrong!");
77
+ }
 
 
 
 
78
  }
79
  </script>
80
  """
81
 
 
 
 
 
 
 
 
 
 
82
  # Main Gradio app
83
  with gr.Blocks() as demo:
 
 
 
84
  gr.HTML("<h1 style='text-align: center;'>🍛 Welcome to the Biryani Hub 🍛</h1>")
85
+ gr.HTML(popup_js) # Add JavaScript for handling interactions
86
 
87
+ # Tabs for Menu and Cart
88
  with gr.Tabs():
89
  # Menu Tab
90
  with gr.Tab("Menu"):
 
117
  outputs=cart_display
118
  )
119
 
120
+ # Launch Gradio app
121
  demo.launch()