buzzbandit commited on
Commit
bc0444f
Β·
verified Β·
1 Parent(s): bd7d50d

add convenience buttons

Browse files
Files changed (1) hide show
  1. app.py +100 -16
app.py CHANGED
@@ -255,17 +255,45 @@ def run_query(query_text, category, country, capacity, refresh):
255
  return query_inventory(query_text, category, country, capacity, refresh)
256
 
257
  # ---------------- Gradio UI ----------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
  with gr.Blocks(title="🧳 Torn Inventory Viewer") as iface:
259
  gr.Markdown("## 🧳 Torn Inventory Viewer")
260
  gr.Markdown("_Search Torn YATA travel stocks with semantic matching._")
261
 
 
 
 
 
 
 
 
 
262
  query_box = gr.Textbox(label="Search (semantic, e.g. 'flowers in England')", elem_id="qbox")
263
  category_drop = gr.Dropdown(label="Category (optional exact match)", choices=[""] + ALL_CATEGORIES, elem_id="catdrop")
264
  country_box = gr.Textbox(label="Country (optional, e.g. UK, Cayman, Japan)", elem_id="countrybox")
265
  capacity_slider = gr.Number(label="Travel Capacity", value=10, minimum=5, maximum=88, precision=0, elem_id="cap_num")
266
  refresh_check = gr.Checkbox(label="Force refresh (ignore cache)", value=False)
267
 
268
- # Hidden bridge for capacity β†’ backend init
269
  cap_bridge = gr.Textbox(visible=False, elem_id="cap_bridge")
270
 
271
  def apply_bridge(cap_text):
@@ -275,9 +303,8 @@ with gr.Blocks(title="🧳 Torn Inventory Viewer") as iface:
275
  return gr.Number.update(value=v)
276
  except:
277
  pass
278
- return gr.Number.update() # no change
279
 
280
- # When the bridge receives a value, update the slider (so backend sees it)
281
  cap_bridge.change(apply_bridge, inputs=[cap_bridge], outputs=[capacity_slider])
282
 
283
  run_btn = gr.Button("πŸ” Search / Refresh")
@@ -288,7 +315,75 @@ with gr.Blocks(title="🧳 Torn Inventory Viewer") as iface:
288
  inputs=[query_box, category_drop, country_box, capacity_slider, refresh_check],
289
  outputs=[result_df, meta_box])
290
 
291
- # --- Shadow-DOM aware JS: restore capacity & push into hidden bridge BEFORE first search ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
  gr.HTML("""
293
  <script>
294
  (function () {
@@ -302,21 +397,15 @@ with gr.Blocks(title="🧳 Torn Inventory Viewer") as iface:
302
  const saved = localStorage.getItem('travel_capacity');
303
  const capField = el('#cap_num input[type=number]');
304
  const bridge = el('#cap_bridge textarea');
305
-
306
  if (saved && capField) {
307
- // Update visible field
308
  capField.value = saved;
309
- // Update frontend state
310
  capField.dispatchEvent(new Event('input', { bubbles: true }));
311
- // Push into hidden bridge -> triggers backend .change -> updates slider value server-side
312
  if (bridge) {
313
  bridge.value = saved;
314
  bridge.dispatchEvent(new Event('input', { bubbles: true }));
315
  bridge.dispatchEvent(new Event('change', { bubbles: true }));
316
  }
317
  }
318
-
319
- // Save on changes
320
  if (capField && !capField.dataset.synced) {
321
  capField.dataset.synced = '1';
322
  capField.addEventListener('input', () => {
@@ -324,12 +413,7 @@ with gr.Blocks(title="🧳 Torn Inventory Viewer") as iface:
324
  });
325
  }
326
  }
327
-
328
- window.addEventListener('DOMContentLoaded', () => {
329
- setTimeout(restoreCapacity, 500);
330
- });
331
-
332
- // Keep trying on re-renders
333
  const obs = new MutationObserver(() => setTimeout(restoreCapacity, 100));
334
  obs.observe(document.documentElement, { childList: true, subtree: true });
335
  })();
 
255
  return query_inventory(query_text, category, country, capacity, refresh)
256
 
257
  # ---------------- Gradio UI ----------------
258
+ def run_query(query_text, category, country, capacity, refresh):
259
+ return query_inventory(query_text, category, country, capacity, refresh)
260
+
261
+ def run_multi(queries, capacity, refresh):
262
+ """Execute multiple queries and merge all results."""
263
+ dfs = []
264
+ for qtext in queries:
265
+ print(f"⚑ Running convenience subquery: {qtext}")
266
+ df, _ = query_inventory(qtext, "", "", capacity, refresh)
267
+ if "Result" not in df.columns:
268
+ dfs.append(df)
269
+ if not dfs:
270
+ return pd.DataFrame([{"Result": "No results for that set."}]), "No results."
271
+ merged = pd.concat(dfs, ignore_index=True)
272
+ merged = merged.sort_values(by=["Country", "Item"])
273
+ for col in ["Quantity", "Cost", "Max Capacity Cost"]:
274
+ if col in merged.columns:
275
+ merged[col] = merged[col].apply(lambda x: f"{x:,.0f}" if isinstance(x, (int, float)) and x != "" else x)
276
+ return merged, f"Last update: {time.strftime('%Y-%m-%d %H:%M:%S')}"
277
+
278
  with gr.Blocks(title="🧳 Torn Inventory Viewer") as iface:
279
  gr.Markdown("## 🧳 Torn Inventory Viewer")
280
  gr.Markdown("_Search Torn YATA travel stocks with semantic matching._")
281
 
282
+ # --- Convenience Buttons ---
283
+ with gr.Row():
284
+ btn_short = gr.Button("🌸 Flushies (Short Haul)")
285
+ btn_med = gr.Button("✈️ Flushies (Medium Haul)")
286
+ btn_long = gr.Button("πŸ›« Flushies (Long Haul)")
287
+ btn_xanax = gr.Button("πŸ’Š Xanax")
288
+ btn_temps = gr.Button("πŸ’£ Temps")
289
+
290
  query_box = gr.Textbox(label="Search (semantic, e.g. 'flowers in England')", elem_id="qbox")
291
  category_drop = gr.Dropdown(label="Category (optional exact match)", choices=[""] + ALL_CATEGORIES, elem_id="catdrop")
292
  country_box = gr.Textbox(label="Country (optional, e.g. UK, Cayman, Japan)", elem_id="countrybox")
293
  capacity_slider = gr.Number(label="Travel Capacity", value=10, minimum=5, maximum=88, precision=0, elem_id="cap_num")
294
  refresh_check = gr.Checkbox(label="Force refresh (ignore cache)", value=False)
295
 
296
+ # Hidden bridge for JS sync
297
  cap_bridge = gr.Textbox(visible=False, elem_id="cap_bridge")
298
 
299
  def apply_bridge(cap_text):
 
303
  return gr.Number.update(value=v)
304
  except:
305
  pass
306
+ return gr.Number.update()
307
 
 
308
  cap_bridge.change(apply_bridge, inputs=[cap_bridge], outputs=[capacity_slider])
309
 
310
  run_btn = gr.Button("πŸ” Search / Refresh")
 
315
  inputs=[query_box, category_drop, country_box, capacity_slider, refresh_check],
316
  outputs=[result_df, meta_box])
317
 
318
+ # --- Convenience button handlers ---
319
+ btn_short.click(
320
+ fn=lambda capacity, refresh: run_multi(
321
+ [
322
+ "flowers in mexico",
323
+ "flowers in cayman islands",
324
+ "flowers in canada",
325
+ "plushies in mexico",
326
+ "plushies in cayman islands",
327
+ "plushies in canada",
328
+ ],
329
+ capacity, refresh),
330
+ inputs=[capacity_slider, refresh_check],
331
+ outputs=[result_df, meta_box],
332
+ )
333
+
334
+ btn_med.click(
335
+ fn=lambda capacity, refresh: run_multi(
336
+ [
337
+ "flowers in hawaii",
338
+ "flowers in united kingdom",
339
+ "flowers in argentina",
340
+ "flowers in switzerland",
341
+ "flowers in japan",
342
+ "plushies in hawaii",
343
+ "plushies in united kingdom",
344
+ "plushies in argentina",
345
+ "plushies in switzerland",
346
+ "plushies in japan",
347
+ ],
348
+ capacity, refresh),
349
+ inputs=[capacity_slider, refresh_check],
350
+ outputs=[result_df, meta_box],
351
+ )
352
+
353
+ btn_long.click(
354
+ fn=lambda capacity, refresh: run_multi(
355
+ [
356
+ "flowers in uae",
357
+ "flowers in china",
358
+ "flowers in south africa",
359
+ "plushies in uae",
360
+ "plushies in china",
361
+ "plushies in south africa",
362
+ ],
363
+ capacity, refresh),
364
+ inputs=[capacity_slider, refresh_check],
365
+ outputs=[result_df, meta_box],
366
+ )
367
+
368
+ btn_xanax.click(
369
+ fn=lambda capacity, refresh: run_multi(["xanax in south africa"], capacity, refresh),
370
+ inputs=[capacity_slider, refresh_check],
371
+ outputs=[result_df, meta_box],
372
+ )
373
+
374
+ btn_temps.click(
375
+ fn=lambda capacity, refresh: run_multi(
376
+ [
377
+ "tear gas in argentina",
378
+ "smoke grenade in south africa",
379
+ "flash grenade in switzerland",
380
+ ],
381
+ capacity, refresh),
382
+ inputs=[capacity_slider, refresh_check],
383
+ outputs=[result_df, meta_box],
384
+ )
385
+
386
+ # --- JS: restore capacity ---
387
  gr.HTML("""
388
  <script>
389
  (function () {
 
397
  const saved = localStorage.getItem('travel_capacity');
398
  const capField = el('#cap_num input[type=number]');
399
  const bridge = el('#cap_bridge textarea');
 
400
  if (saved && capField) {
 
401
  capField.value = saved;
 
402
  capField.dispatchEvent(new Event('input', { bubbles: true }));
 
403
  if (bridge) {
404
  bridge.value = saved;
405
  bridge.dispatchEvent(new Event('input', { bubbles: true }));
406
  bridge.dispatchEvent(new Event('change', { bubbles: true }));
407
  }
408
  }
 
 
409
  if (capField && !capField.dataset.synced) {
410
  capField.dataset.synced = '1';
411
  capField.addEventListener('input', () => {
 
413
  });
414
  }
415
  }
416
+ window.addEventListener('DOMContentLoaded', () => setTimeout(restoreCapacity, 500));
 
 
 
 
 
417
  const obs = new MutationObserver(() => setTimeout(restoreCapacity, 100));
418
  obs.observe(document.documentElement, { childList: true, subtree: true });
419
  })();