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

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -116
app.py CHANGED
@@ -8,7 +8,6 @@ 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:
@@ -108,28 +107,18 @@ def _filtered_df(country: str, dealer: str, brand: str) -> pd.DataFrame:
108
  if brand and brand != ALL: df = df[df["brand"].astype(str) == brand]
109
  return df
110
 
111
- # ---------------- CSS (injected once as a separate HTML) ----------------
112
  GLOBAL_STYLE = """
113
  <style>
114
- /* Unify font */
115
- body, .gradio-container, .gradio-container * {
116
- font-family: Inter, Segoe UI, Roboto, Helvetica, Arial, sans-serif !important;
117
- }
118
- /* Single scroll region */
119
  #cards-pane { height: 820px; overflow: auto; border-radius: 12px; }
120
- /* Colors + layout */
121
  :root{ --bg:#f6f7fb; --card:#ffffff; --ink:#111827; --muted:#6b7280; --line:#e5e7eb; --chip:#eef2f7; --accent:#2563eb; }
122
  #cards-root{ background: var(--bg); padding: 8px; }
123
  .cards{ display:grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap:18px; }
124
  @media (max-width:1300px){ .cards{ grid-template-columns: repeat(3, minmax(0, 1fr)); } }
125
  @media (max-width:1000px){ .cards{ grid-template-columns: repeat(2, minmax(0, 1fr)); } }
126
  @media (max-width:640px){ .cards{ grid-template-columns: 1fr; } }
127
-
128
- .card{
129
- background:var(--card); border:1px solid var(--line); border-radius:16px; overflow:hidden;
130
- box-shadow:0 4px 12px rgba(0,0,0,.06); cursor:pointer; display:flex; flex-direction:column;
131
- transition: transform .08s ease, box-shadow .2s ease;
132
- }
133
  .card:hover{ transform: translateY(-2px); box-shadow:0 8px 20px rgba(0,0,0,.12); }
134
  .thumb{ height:160px; overflow:hidden; background:#000; }
135
  .thumb img{ width:100%; height:100%; object-fit:cover; }
@@ -137,138 +126,62 @@ body, .gradio-container, .gradio-container * {
137
  .sp{ font-size:13px; color:var(--muted); }
138
  .sp strong{ display:block; font-size:20px; color:#e5b000; margin-top:2px; }
139
  .heading{ font-weight:700; color:var(--ink); font-size:15px; line-height:1.25; }
140
- .offer{ color:var(--ink); font-size:13px; line-height:1.45; opacity:.9;
141
- display:-webkit-box; -webkit-line-clamp:4; -webkit-box-orient:vertical; overflow:hidden; }
142
  .meta{ display:flex; gap:6px; flex-wrap:wrap; padding:0 12px 12px; margin-top:auto; }
143
  .chip{ font-size:11px; color:#374151; background:var(--chip); border-radius:999px; padding:4px 8px; }
144
-
145
- /* Tooltip portal */
146
- #tt-portal{
147
- position: fixed; top: 0; left: 0; z-index: 2147483647; display: none;
148
- width: min(520px, 86vw); max-height: 72vh; overflow-x: hidden; overflow-y: auto;
149
- background: rgba(255,255,255,.92);
150
- -webkit-backdrop-filter: saturate(1.4) blur(8px); backdrop-filter: saturate(1.4) blur(8px);
151
- border: 1px solid transparent; border-radius: 14px; box-shadow: 0 18px 50px rgba(2,6,23,.18);
152
- padding: 14px 16px; color: #0f172a; font-size: 13px; line-height: 1.5;
153
- background-clip: padding-box, border-box;
154
- background-image: linear-gradient(rgba(255,255,255,.92), rgba(255,255,255,.92)),
155
- linear-gradient(135deg, rgba(99,102,241,.25), rgba(99,102,241,.05));
156
- }
157
  #tt-portal.show { display:block; animation: tt-fade .16s ease-out; }
158
  @keyframes tt-fade { from { opacity:0; transform: translateY(-2px); } to { opacity:1; transform: translateY(0); } }
159
- #tt-portal::after{
160
- content:""; position:absolute; top:16px; width:12px; height:12px; transform: rotate(45deg);
161
- background: rgba(255,255,255,.92);
162
- border-left: 1px solid rgba(99,102,241,.18); border-top: 1px solid rgba(99,102,241,.18);
163
- }
164
  #tt-portal.right::after{ left:-8px; }
165
- #tt-portal.left::after{ right:-8px; left:auto; border-left:none; border-top:none;
166
- border-right:1px solid rgba(99,102,241,.18); border-bottom:1px solid rgba(99,102,241,.18); }
167
-
168
  .tt-head{ display:flex; align-items:flex-start; gap:10px; margin-bottom:10px; }
169
- .tt-dot{ width:10px; height:10px; border-radius:999px; margin-top:4px;
170
- background: radial-gradient(circle at 30% 30%, #a78bfa, #6366f1); box-shadow: 0 0 0 3px rgba(99,102,241,.12); flex:0 0 auto; }
171
  .tt-title{ font-weight:800; font-size:15px; color:#0b1220; line-height:1.25; }
172
  .tt-meta{ display:flex; gap:6px; flex-wrap:wrap; margin:2px 0 8px; }
173
  .tt-chip{ font-size:11px; color:#475569; background:#f8fafc; border:1px solid #e2e8f0; padding:4px 8px; border-radius:10px; }
174
- .tt-grid{ display:grid; grid-template-columns: 120px minmax(0,1fr); gap:8px 12px;
175
- background: linear-gradient(180deg, rgba(99,102,241,.06), rgba(99,102,241,0) 45%);
176
- border:1px dashed rgba(99,102,241,.15); border-radius:10px; padding:10px 12px; margin:8px 0; }
177
  .tt-k{ color:#334155; font-weight:600; font-size:12px; }
178
  .tt-v{ color:#0f172a; font-size:13px; }
179
  .tt-link{ display:inline-flex; align-items:center; gap:6px; margin-top:8px; color:#2563eb; text-decoration:none; font-weight:600; }
180
  .tt-link:hover{ text-decoration:underline; }
181
-
182
  #tt-portal, #tt-portal *{ box-sizing: border-box; }
183
  #tt-portal img, #tt-portal svg, #tt-portal video{ max-width:100%; height:auto; display:block; }
184
  </style>
185
  """
186
 
187
- # ---------------- JS to bind tooltips (executed via js= on every update) ----------------
188
  TOOLTIP_BIND_JS = r"""
189
  () => {
190
  const root = document.querySelector('#cards-pane #cards-root') || document.querySelector('#cards-root');
191
  if (!root) return;
192
-
193
- // Prepare portal
194
  let portal = root.querySelector('#tt-portal') || document.getElementById('tt-portal');
195
- if (!portal) {
196
- portal = document.createElement('div');
197
- portal.id = 'tt-portal';
198
- root.prepend(portal);
199
- }
200
-
201
  let activeCard = null, overPortal = false, hideTimer = null;
202
  const clamp = (n,min,max)=> Math.max(min, Math.min(max, n));
203
-
204
  function placePortal(anchor, side){
205
- const a = anchor.getBoundingClientRect();
206
- const p = portal.getBoundingClientRect();
207
- const vw = window.innerWidth, vh = window.innerHeight;
208
- const gap = 14; let left = a.right + gap;
209
  portal.classList.remove('left','right');
210
-
211
- if (side === 'left' || left + p.width > vw - 6){
212
- left = Math.max(6, a.left - gap - p.width); portal.classList.add('left');
213
- } else {
214
- left = Math.min(left, vw - 6 - p.width); portal.classList.add('right');
215
- }
216
- const topDesired = a.top + 10;
217
- const top = clamp(topDesired, 6, vh - p.height - 6);
218
  portal.style.left = left + 'px'; portal.style.top = top + 'px';
219
  }
220
-
221
  function showFor(card, prefer='right'){
222
- const tpl = card.querySelector('.card-tooltip-template');
223
- if(!tpl) return;
224
- clearTimeout(hideTimer);
225
- activeCard = card;
226
- portal.innerHTML = tpl.innerHTML;
227
- portal.classList.add('show');
228
- requestAnimationFrame(()=>{
229
- placePortal(card, prefer);
230
- const pr = portal.getBoundingClientRect();
231
- if (pr.right > window.innerWidth - 2){ placePortal(card,'left'); }
232
- });
233
  card.classList.add('active');
234
  }
235
- function hideSoon(){
236
- clearTimeout(hideTimer);
237
- hideTimer = setTimeout(()=>{
238
- const stillOnCard = activeCard && activeCard.matches(':hover');
239
- if(!overPortal && !stillOnCard){ hideNow(); }
240
- }, 120);
241
- }
242
- function hideNow(){
243
- portal.classList.remove('show');
244
- if(activeCard){ activeCard.classList.remove('active'); activeCard = null; }
245
- }
246
-
247
- // (Re)bind all cards each time this JS runs
248
- root.querySelectorAll('.card').forEach(card=>{
249
- card.onmouseenter = ()=>showFor(card);
250
- card.onmouseleave = hideSoon;
251
- card.onfocusin = ()=>showFor(card);
252
- card.onfocusout = hideSoon;
253
- card.onclick = (e)=>{
254
- if(activeCard === card){ hideNow(); e.stopPropagation(); return; }
255
- showFor(card); e.stopPropagation();
256
- };
257
- });
258
-
259
- function maybeReposition(){
260
- if(!portal.classList.contains('show') || !activeCard) return;
261
- const side = portal.classList.contains('left') ? 'left' : 'right';
262
- placePortal(activeCard, side);
263
- }
264
- window.addEventListener('scroll', maybeReposition, {passive:true});
265
- window.addEventListener('resize', maybeReposition, {passive:true});
266
-
267
- portal.onmouseenter = ()=>{ overPortal = true; clearTimeout(hideTimer); };
268
- portal.onmouseleave = ()=>{ overPortal = false; hideSoon(); };
269
  document.addEventListener('click', (e)=>{ if(!portal.contains(e.target)) hideNow(); }, {once:true});
270
-
271
- // Done
272
  return null;
273
  }
274
  """
@@ -315,7 +228,7 @@ def app():
315
 
316
  with gr.Blocks(title="Vehicle Offers", css=".gradio-container{max-width:1400px;margin:0 auto;}") as demo:
317
  gr.Markdown("## 🚗 Vehicle Offers")
318
- gr.HTML(GLOBAL_STYLE) # inject styles once
319
 
320
  with gr.Row():
321
  dd_country = gr.Dropdown(choices=countries, value=ALL, label="Country")
@@ -323,10 +236,13 @@ def app():
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
 
328
- # Initial render + bind tooltips via JS
329
- demo.load(render_cards, [dd_country, dd_dealer, dd_brand, s_limit], out_html, js=TOOLTIP_BIND_JS)
330
 
331
  # Country change → reset dealer & brand + rebind JS
332
  dd_country.change(on_country_change,
 
8
  DEFAULT_LIMIT = 50 # fallback when slider momentarily returns None
9
 
10
  def _norm_limit(limit):
 
11
  try:
12
  return int(limit) if limit is not None else DEFAULT_LIMIT
13
  except Exception:
 
107
  if brand and brand != ALL: df = df[df["brand"].astype(str) == brand]
108
  return df
109
 
110
+ # ---------------- CSS (injected once) ----------------
111
  GLOBAL_STYLE = """
112
  <style>
113
+ body, .gradio-container, .gradio-container * { font-family: Inter, Segoe UI, Roboto, Helvetica, Arial, sans-serif !important; }
 
 
 
 
114
  #cards-pane { height: 820px; overflow: auto; border-radius: 12px; }
 
115
  :root{ --bg:#f6f7fb; --card:#ffffff; --ink:#111827; --muted:#6b7280; --line:#e5e7eb; --chip:#eef2f7; --accent:#2563eb; }
116
  #cards-root{ background: var(--bg); padding: 8px; }
117
  .cards{ display:grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap:18px; }
118
  @media (max-width:1300px){ .cards{ grid-template-columns: repeat(3, minmax(0, 1fr)); } }
119
  @media (max-width:1000px){ .cards{ grid-template-columns: repeat(2, minmax(0, 1fr)); } }
120
  @media (max-width:640px){ .cards{ grid-template-columns: 1fr; } }
121
+ .card{ background:var(--card); border:1px solid var(--line); border-radius:16px; overflow:hidden; box-shadow:0 4px 12px rgba(0,0,0,.06); cursor:pointer; display:flex; flex-direction:column; transition: transform .08s ease, box-shadow .2s ease; }
 
 
 
 
 
122
  .card:hover{ transform: translateY(-2px); box-shadow:0 8px 20px rgba(0,0,0,.12); }
123
  .thumb{ height:160px; overflow:hidden; background:#000; }
124
  .thumb img{ width:100%; height:100%; object-fit:cover; }
 
126
  .sp{ font-size:13px; color:var(--muted); }
127
  .sp strong{ display:block; font-size:20px; color:#e5b000; margin-top:2px; }
128
  .heading{ font-weight:700; color:var(--ink); font-size:15px; line-height:1.25; }
129
+ .offer{ color:var(--ink); font-size:13px; line-height:1.45; opacity:.9; display:-webkit-box; -webkit-line-clamp:4; -webkit-box-orient:vertical; overflow:hidden; }
 
130
  .meta{ display:flex; gap:6px; flex-wrap:wrap; padding:0 12px 12px; margin-top:auto; }
131
  .chip{ font-size:11px; color:#374151; background:var(--chip); border-radius:999px; padding:4px 8px; }
132
+ #tt-portal{ position: fixed; top: 0; left: 0; z-index: 2147483647; display: none; width: min(520px, 86vw); max-height: 72vh; overflow-x: hidden; overflow-y: auto; background: rgba(255,255,255,.92); -webkit-backdrop-filter: saturate(1.4) blur(8px); backdrop-filter: saturate(1.4) blur(8px); border: 1px solid transparent; border-radius: 14px; box-shadow: 0 18px 50px rgba(2,6,23,.18); padding: 14px 16px; color: #0f172a; font-size: 13px; line-height: 1.5; background-clip: padding-box, border-box; background-image: linear-gradient(rgba(255,255,255,.92), rgba(255,255,255,.92)), linear-gradient(135deg, rgba(99,102,241,.25), rgba(99,102,241,.05)); }
 
 
 
 
 
 
 
 
 
 
 
 
133
  #tt-portal.show { display:block; animation: tt-fade .16s ease-out; }
134
  @keyframes tt-fade { from { opacity:0; transform: translateY(-2px); } to { opacity:1; transform: translateY(0); } }
135
+ #tt-portal::after{ content:""; position:absolute; top:16px; width:12px; height:12px; transform: rotate(45deg); background: rgba(255,255,255,.92); border-left: 1px solid rgba(99,102,241,.18); border-top: 1px solid rgba(99,102,241,.18); }
 
 
 
 
136
  #tt-portal.right::after{ left:-8px; }
137
+ #tt-portal.left::after{ right:-8px; left:auto; border-left:none; border-top:none; border-right:1px solid rgba(99,102,241,.18); border-bottom:1px solid rgba(99,102,241,.18); }
 
 
138
  .tt-head{ display:flex; align-items:flex-start; gap:10px; margin-bottom:10px; }
139
+ .tt-dot{ width:10px; height:10px; border-radius:999px; margin-top:4px; background: radial-gradient(circle at 30% 30%, #a78bfa, #6366f1); box-shadow: 0 0 0 3px rgba(99,102,241,.12); flex:0 0 auto; }
 
140
  .tt-title{ font-weight:800; font-size:15px; color:#0b1220; line-height:1.25; }
141
  .tt-meta{ display:flex; gap:6px; flex-wrap:wrap; margin:2px 0 8px; }
142
  .tt-chip{ font-size:11px; color:#475569; background:#f8fafc; border:1px solid #e2e8f0; padding:4px 8px; border-radius:10px; }
143
+ .tt-grid{ display:grid; grid-template-columns: 120px minmax(0,1fr); gap:8px 12px; background: linear-gradient(180deg, rgba(99,102,241,.06), rgba(99,102,241,0) 45%); border:1px dashed rgba(99,102,241,.15); border-radius:10px; padding:10px 12px; margin:8px 0; }
 
 
144
  .tt-k{ color:#334155; font-weight:600; font-size:12px; }
145
  .tt-v{ color:#0f172a; font-size:13px; }
146
  .tt-link{ display:inline-flex; align-items:center; gap:6px; margin-top:8px; color:#2563eb; text-decoration:none; font-weight:600; }
147
  .tt-link:hover{ text-decoration:underline; }
 
148
  #tt-portal, #tt-portal *{ box-sizing: border-box; }
149
  #tt-portal img, #tt-portal svg, #tt-portal video{ max-width:100%; height:auto; display:block; }
150
  </style>
151
  """
152
 
153
+ # ---------------- JS to bind tooltips ----------------
154
  TOOLTIP_BIND_JS = r"""
155
  () => {
156
  const root = document.querySelector('#cards-pane #cards-root') || document.querySelector('#cards-root');
157
  if (!root) return;
 
 
158
  let portal = root.querySelector('#tt-portal') || document.getElementById('tt-portal');
159
+ if (!portal) { portal = document.createElement('div'); portal.id = 'tt-portal'; root.prepend(portal); }
 
 
 
 
 
160
  let activeCard = null, overPortal = false, hideTimer = null;
161
  const clamp = (n,min,max)=> Math.max(min, Math.min(max, n));
 
162
  function placePortal(anchor, side){
163
+ const a = anchor.getBoundingClientRect(); const p = portal.getBoundingClientRect();
164
+ const vw = window.innerWidth, vh = window.innerHeight; const gap = 14; let left = a.right + gap;
 
 
165
  portal.classList.remove('left','right');
166
+ if (side === 'left' || left + p.width > vw - 6){ left = Math.max(6, a.left - gap - p.width); portal.classList.add('left'); }
167
+ else { left = Math.min(left, vw - 6 - p.width); portal.classList.add('right'); }
168
+ const topDesired = a.top + 10; const top = clamp(topDesired, 6, vh - p.height - 6);
 
 
 
 
 
169
  portal.style.left = left + 'px'; portal.style.top = top + 'px';
170
  }
 
171
  function showFor(card, prefer='right'){
172
+ const tpl = card.querySelector('.card-tooltip-template'); if(!tpl) return;
173
+ clearTimeout(hideTimer); activeCard = card; portal.innerHTML = tpl.innerHTML; portal.classList.add('show');
174
+ requestAnimationFrame(()=>{ placePortal(card, prefer); const pr = portal.getBoundingClientRect();
175
+ if (pr.right > window.innerWidth - 2){ placePortal(card,'left'); }});
 
 
 
 
 
 
 
176
  card.classList.add('active');
177
  }
178
+ function hideSoon(){ clearTimeout(hideTimer); hideTimer = setTimeout(()=>{ const still = activeCard && activeCard.matches(':hover'); if(!overPortal && !still){ hideNow(); } }, 120); }
179
+ function hideNow(){ portal.classList.remove('show'); if(activeCard){ activeCard.classList.remove('active'); activeCard = null; } }
180
+ root.querySelectorAll('.card').forEach(card=>{ card.onmouseenter = ()=>showFor(card); card.onmouseleave = hideSoon; card.onfocusin = ()=>showFor(card); card.onfocusout = hideSoon; });
181
+ function maybeReposition(){ if(!portal.classList.contains('show') || !activeCard) return; const side = portal.classList.contains('left') ? 'left' : 'right'; placePortal(activeCard, side); }
182
+ window.addEventListener('scroll', maybeReposition, {passive:true}); window.addEventListener('resize', maybeReposition, {passive:true});
183
+ portal.onmouseenter = ()=>{ overPortal = true; clearTimeout(hideTimer); }; portal.onmouseleave = ()=>{ overPortal = false; hideSoon(); };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  document.addEventListener('click', (e)=>{ if(!portal.contains(e.target)) hideNow(); }, {once:true});
 
 
185
  return null;
186
  }
187
  """
 
228
 
229
  with gr.Blocks(title="Vehicle Offers", css=".gradio-container{max-width:1400px;margin:0 auto;}") as demo:
230
  gr.Markdown("## 🚗 Vehicle Offers")
231
+ gr.HTML(GLOBAL_STYLE)
232
 
233
  with gr.Row():
234
  dd_country = gr.Dropdown(choices=countries, value=ALL, label="Country")
 
236
  dd_brand = gr.Dropdown(choices=brands, value=ALL, label="Brand")
237
  s_limit = gr.Slider(6, 200, value=DEFAULT_LIMIT, step=6, label="Max Offers")
238
 
239
+ # State used ONLY for initial load to avoid None from slider
240
+ limit_state = gr.State(DEFAULT_LIMIT)
241
+
242
  out_html = gr.HTML(elem_id="cards-pane")
243
 
244
+ # Initial render uses limit_state (not the Slider) -> prevents None crash
245
+ demo.load(render_cards, [dd_country, dd_dealer, dd_brand, limit_state], out_html, js=TOOLTIP_BIND_JS)
246
 
247
  # Country change → reset dealer & brand + rebind JS
248
  dd_country.change(on_country_change,