ravim254 commited on
Commit
7a65e89
·
verified ·
1 Parent(s): e92ee1c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -3
app.py CHANGED
@@ -5,6 +5,14 @@ from html import escape as esc
5
  import re, json
6
 
7
  ALL = "All"
 
 
 
 
 
 
 
 
8
 
9
  # ---------------- Load & normalize ----------------
10
  DF = pd.read_csv("offers_with_prices.csv")
@@ -266,11 +274,13 @@ TOOLTIP_BIND_JS = r"""
266
  """
267
 
268
  # ---------------- Render functions ----------------
269
- def render_cards(country: str, dealer: str, brand: str, limit: int):
270
- df = _filtered_df(country, dealer, brand).head(int(limit))
 
271
  return _grid_html(df)
272
 
273
  def on_country_change(country, dealer, brand, limit):
 
274
  df_c = _filtered_df(country, ALL, ALL)
275
  dealer_choices = _choices(df_c["dealer"]); brand_choices = _choices(df_c["brand"])
276
  dealer_val = brand_val = ALL
@@ -278,6 +288,7 @@ def on_country_change(country, dealer, brand, limit):
278
  return gr.update(choices=dealer_choices, value=dealer_val), gr.update(choices=brand_choices, value=brand_val), html
279
 
280
  def on_dealer_change(country, dealer, brand, limit):
 
281
  df_cd = _filtered_df(country, dealer, ALL)
282
  brand_choices = _choices(df_cd["brand"])
283
  brand_val = brand if brand in brand_choices else ALL
@@ -285,6 +296,7 @@ def on_dealer_change(country, dealer, brand, limit):
285
  return gr.update(choices=brand_choices, value=brand_val), html
286
 
287
  def on_brand_change(country, dealer, brand, limit):
 
288
  df_cb = _filtered_df(country, ALL, brand)
289
  dealer_choices = _choices(df_cb["dealer"])
290
  dealer_val = dealer if dealer in dealer_choices else ALL
@@ -292,6 +304,7 @@ def on_brand_change(country, dealer, brand, limit):
292
  return gr.update(choices=dealer_choices, value=dealer_val), html
293
 
294
  def on_limit_change(country, dealer, brand, limit):
 
295
  return render_cards(country, dealer, brand, limit)
296
 
297
  # ---------------- Build Gradio UI ----------------
@@ -308,7 +321,7 @@ def app():
308
  dd_country = gr.Dropdown(choices=countries, value=ALL, label="Country")
309
  dd_dealer = gr.Dropdown(choices=dealers, value=ALL, label="Dealer")
310
  dd_brand = gr.Dropdown(choices=brands, value=ALL, label="Brand")
311
- s_limit = gr.Slider(6, 200, value=50, step=6, label="Max Offers")
312
 
313
  out_html = gr.HTML(elem_id="cards-pane")
314
 
 
5
  import re, json
6
 
7
  ALL = "All"
8
+ DEFAULT_LIMIT = 50 # fallback when slider momentarily returns None
9
+
10
+ def _norm_limit(limit):
11
+ """Ensure we always have a valid int for limit (guards None/invalid)."""
12
+ try:
13
+ return int(limit) if limit is not None else DEFAULT_LIMIT
14
+ except Exception:
15
+ return DEFAULT_LIMIT
16
 
17
  # ---------------- Load & normalize ----------------
18
  DF = pd.read_csv("offers_with_prices.csv")
 
274
  """
275
 
276
  # ---------------- Render functions ----------------
277
+ def render_cards(country: str, dealer: str, brand: str, limit):
278
+ limit = _norm_limit(limit)
279
+ df = _filtered_df(country, dealer, brand).head(limit)
280
  return _grid_html(df)
281
 
282
  def on_country_change(country, dealer, brand, limit):
283
+ limit = _norm_limit(limit)
284
  df_c = _filtered_df(country, ALL, ALL)
285
  dealer_choices = _choices(df_c["dealer"]); brand_choices = _choices(df_c["brand"])
286
  dealer_val = brand_val = ALL
 
288
  return gr.update(choices=dealer_choices, value=dealer_val), gr.update(choices=brand_choices, value=brand_val), html
289
 
290
  def on_dealer_change(country, dealer, brand, limit):
291
+ limit = _norm_limit(limit)
292
  df_cd = _filtered_df(country, dealer, ALL)
293
  brand_choices = _choices(df_cd["brand"])
294
  brand_val = brand if brand in brand_choices else ALL
 
296
  return gr.update(choices=brand_choices, value=brand_val), html
297
 
298
  def on_brand_change(country, dealer, brand, limit):
299
+ limit = _norm_limit(limit)
300
  df_cb = _filtered_df(country, ALL, brand)
301
  dealer_choices = _choices(df_cb["dealer"])
302
  dealer_val = dealer if dealer in dealer_choices else ALL
 
304
  return gr.update(choices=dealer_choices, value=dealer_val), html
305
 
306
  def on_limit_change(country, dealer, brand, limit):
307
+ limit = _norm_limit(limit)
308
  return render_cards(country, dealer, brand, limit)
309
 
310
  # ---------------- Build Gradio UI ----------------
 
321
  dd_country = gr.Dropdown(choices=countries, value=ALL, label="Country")
322
  dd_dealer = gr.Dropdown(choices=dealers, value=ALL, label="Dealer")
323
  dd_brand = gr.Dropdown(choices=brands, value=ALL, label="Brand")
324
+ s_limit = gr.Slider(6, 200, value=DEFAULT_LIMIT, step=6, label="Max Offers")
325
 
326
  out_html = gr.HTML(elem_id="cards-pane")
327