ravim254 commited on
Commit
aba6e0e
·
verified ·
1 Parent(s): 637aacb

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -15
app.py CHANGED
@@ -108,10 +108,23 @@ def _grid_html(df: pd.DataFrame) -> str:
108
  </html>
109
  """
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  # ---------- Filtering logic ----------
112
  def filter_offers(country: str, brand: str, dealer: str, limit: int):
113
  df_f = DF.copy()
114
- # normalize to str before comparing
115
  if country and country != "All":
116
  df_f = df_f[df_f["country"].astype(str) == str(country)]
117
  if brand and brand != "All":
@@ -121,13 +134,53 @@ def filter_offers(country: str, brand: str, dealer: str, limit: int):
121
  df_show = df_f.head(int(limit))
122
  return _grid_html(df_show)
123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  # ---------- Build Gradio UI ----------
125
  def app():
126
- countries = ["All"] + sorted(DF["country"].dropna().astype(str).unique().tolist())
127
- brands = ["All"] + sorted(DF["brand"].dropna().astype(str).unique().tolist())
128
- dealers = ["All"] + sorted(DF["dealer"].dropna().astype(str).unique().tolist())
129
 
130
- # One scrolling region (like an iframe) for the HTML
131
  custom_css = """
132
  body, .gradio-container, .gr-block, .gr-button, .gr-input, .gr-dropdown,
133
  .gradio-container * { font-family: Inter, Segoe UI, Roboto, Helvetica, Arial, sans-serif !important; }
@@ -139,20 +192,46 @@ def app():
139
 
140
  with gr.Row():
141
  dd_country = gr.Dropdown(choices=countries, label="Country", value="All")
142
- dd_brand = gr.Dropdown(choices=brands, label="Brand", value="All")
143
- dd_dealer = gr.Dropdown(choices=dealers, label="Dealer", value="All")
144
  s_limit = gr.Slider(6, 200, value=50, step=6, label="Max Offers")
145
 
146
  out_html = gr.HTML(elem_id="cards-pane")
147
 
148
- # Render once on page load using current input values
149
- demo.load(filter_offers, [dd_country, dd_brand, dd_dealer, s_limit], out_html)
150
-
151
- # Auto-update whenever a filter changes (NO button needed)
152
- dd_country.change(filter_offers, [dd_country, dd_brand, dd_dealer, s_limit], out_html)
153
- dd_brand.change( filter_offers, [dd_country, dd_brand, dd_dealer, s_limit], out_html)
154
- dd_dealer.change( filter_offers, [dd_country, dd_brand, dd_dealer, s_limit], out_html)
155
- s_limit.change( filter_offers, [dd_country, dd_brand, dd_dealer, s_limit], out_html)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
157
  return demo
158
 
 
108
  </html>
109
  """
110
 
111
+ # ---------- Helpers for cascading options ----------
112
+ def _opts_with_all(series: pd.Series):
113
+ vals = [x for x in series.dropna().astype(str).unique() if str(x).strip()]
114
+ return ["All"] + sorted(vals)
115
+
116
+ def _brand_and_dealer_options_for_country(country: str):
117
+ df_c = DF if country == "All" else DF[DF["country"].astype(str) == str(country)]
118
+ return _opts_with_all(df_c["brand"]), _opts_with_all(df_c["dealer"])
119
+
120
+ def _dealer_options_for_country_brand(country: str, brand: str):
121
+ df_c = DF if country == "All" else DF[DF["country"].astype(str) == str(country)]
122
+ df_cb = df_c if brand == "All" else df_c[df_c["brand"].astype(str) == str(brand)]
123
+ return _opts_with_all(df_cb["dealer"])
124
+
125
  # ---------- Filtering logic ----------
126
  def filter_offers(country: str, brand: str, dealer: str, limit: int):
127
  df_f = DF.copy()
 
128
  if country and country != "All":
129
  df_f = df_f[df_f["country"].astype(str) == str(country)]
130
  if brand and brand != "All":
 
134
  df_show = df_f.head(int(limit))
135
  return _grid_html(df_show)
136
 
137
+ # --- Gradio event handlers for cascading behavior ---
138
+ def init_on_load(country, brand, dealer, limit):
139
+ # On first render, set brand/dealer choices according to country
140
+ brand_opts, dealer_opts = _brand_and_dealer_options_for_country(country)
141
+ brand_val = brand if brand in brand_opts else "All"
142
+ dealer_val = dealer if dealer in dealer_opts else "All"
143
+ html = filter_offers(country, brand_val, dealer_val, limit)
144
+ return (
145
+ gr.update(choices=brand_opts, value=brand_val),
146
+ gr.update(choices=dealer_opts, value=dealer_val),
147
+ html
148
+ )
149
+
150
+ def on_country_change(country, brand, dealer, limit):
151
+ # Update brand & dealer choices to match selected country
152
+ brand_opts, dealer_opts = _brand_and_dealer_options_for_country(country)
153
+ brand_val = brand if brand in brand_opts else "All"
154
+ dealer_val = dealer if dealer in dealer_opts else "All"
155
+ html = filter_offers(country, brand_val, dealer_val, limit)
156
+ return (
157
+ gr.update(choices=brand_opts, value=brand_val),
158
+ gr.update(choices=dealer_opts, value=dealer_val),
159
+ html
160
+ )
161
+
162
+ def on_brand_change(country, brand, dealer, limit):
163
+ # Update dealer choices to match (country, brand)
164
+ dealer_opts = _dealer_options_for_country_brand(country, brand)
165
+ dealer_val = dealer if dealer in dealer_opts else "All"
166
+ html = filter_offers(country, brand, dealer_val, limit)
167
+ return (
168
+ gr.update(choices=dealer_opts, value=dealer_val),
169
+ html
170
+ )
171
+
172
+ def on_dealer_change(country, brand, dealer, limit):
173
+ return filter_offers(country, brand, dealer, limit)
174
+
175
+ def on_limit_change(country, brand, dealer, limit):
176
+ return filter_offers(country, brand, dealer, limit)
177
+
178
  # ---------- Build Gradio UI ----------
179
  def app():
180
+ countries = _opts_with_all(DF["country"])
181
+ # initial (All) options for brand/dealer
182
+ brands, dealers = _brand_and_dealer_options_for_country("All")
183
 
 
184
  custom_css = """
185
  body, .gradio-container, .gr-block, .gr-button, .gr-input, .gr-dropdown,
186
  .gradio-container * { font-family: Inter, Segoe UI, Roboto, Helvetica, Arial, sans-serif !important; }
 
192
 
193
  with gr.Row():
194
  dd_country = gr.Dropdown(choices=countries, label="Country", value="All")
195
+ dd_brand = gr.Dropdown(choices=brands, label="Brand", value="All")
196
+ dd_dealer = gr.Dropdown(choices=dealers, label="Dealer", value="All")
197
  s_limit = gr.Slider(6, 200, value=50, step=6, label="Max Offers")
198
 
199
  out_html = gr.HTML(elem_id="cards-pane")
200
 
201
+ # Initialize choices + grid on page load
202
+ demo.load(
203
+ init_on_load,
204
+ [dd_country, dd_brand, dd_dealer, s_limit],
205
+ [dd_brand, dd_dealer, out_html]
206
+ )
207
+
208
+ # Country -> update Brand + Dealer choices + grid
209
+ dd_country.change(
210
+ on_country_change,
211
+ [dd_country, dd_brand, dd_dealer, s_limit],
212
+ [dd_brand, dd_dealer, out_html]
213
+ )
214
+
215
+ # Brand -> update Dealer choices + grid
216
+ dd_brand.change(
217
+ on_brand_change,
218
+ [dd_country, dd_brand, dd_dealer, s_limit],
219
+ [dd_dealer, out_html]
220
+ )
221
+
222
+ # Dealer -> update grid
223
+ dd_dealer.change(
224
+ on_dealer_change,
225
+ [dd_country, dd_brand, dd_dealer, s_limit],
226
+ out_html
227
+ )
228
+
229
+ # Limit -> update grid
230
+ s_limit.change(
231
+ on_limit_change,
232
+ [dd_country, dd_brand, dd_dealer, s_limit],
233
+ out_html
234
+ )
235
 
236
  return demo
237