benresnic commited on
Commit
5ca7459
·
verified ·
1 Parent(s): 399a90c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -67
app.py CHANGED
@@ -48,10 +48,6 @@ def _new_card() -> dict:
48
 
49
 
50
  def _normalize_state(obj: dict) -> dict:
51
- """
52
- Backward-compatible normalization:
53
- - If older keys exist (title/resource_url/etc.), map into new schema.
54
- """
55
  if not isinstance(obj, dict):
56
  obj = {}
57
 
@@ -68,25 +64,10 @@ def _normalize_state(obj: dict) -> dict:
68
  if not isinstance(c, dict):
69
  continue
70
 
71
- # Backward compat maps
72
- model = c.get("model")
73
- if model is None:
74
- model = c.get("title", "")
75
-
76
- how_it_works = c.get("how_it_works")
77
- if how_it_works is None:
78
- how_it_works = c.get("how", c.get("howItWorks", "")) or c.get("how_it_work", "")
79
-
80
- use_cases = c.get("use_cases")
81
- if use_cases is None:
82
- use_cases = c.get("use", c.get("useCases", "")) or c.get("use_case", "")
83
- if not use_cases:
84
- use_cases = c.get("use_cases", c.get("use_cases", "")) or c.get("use_cases", "")
85
-
86
- link = c.get("link")
87
- if link is None:
88
- # old resource_url field
89
- link = c.get("resource_url", "")
90
 
91
  normalized.append(
92
  {
@@ -187,7 +168,7 @@ async def api_card_id(request: Request) -> Response:
187
 
188
 
189
  # -----------------------------------------------------------------------------
190
- # Shiny UI (client uses fetch() to call the API above)
191
  # -----------------------------------------------------------------------------
192
 
193
  app_ui = ui.page_fluid(
@@ -225,9 +206,7 @@ app_ui = ui.page_fluid(
225
  cursor: pointer;
226
  box-shadow: 0 2px 8px rgba(0,0,0,.14);
227
  }
228
- #add-btn:hover{ filter: brightness(.97); }
229
 
230
- /* Error banner */
231
  .err{
232
  margin: 0 6px 10px 6px;
233
  padding: 10px 12px;
@@ -245,6 +224,7 @@ app_ui = ui.page_fluid(
245
  grid-template-columns: repeat(4, minmax(260px, 1fr));
246
  gap: 16px;
247
  padding: 2px 6px 18px 6px;
 
248
  }
249
  @media (max-width: 1200px){
250
  .cards-grid{ grid-template-columns: repeat(3, minmax(240px, 1fr)); }
@@ -256,17 +236,20 @@ app_ui = ui.page_fluid(
256
  .cards-grid{ grid-template-columns: 1fr; }
257
  }
258
 
259
- /* Card (square) */
260
  .card{
261
  background: var(--cardBg);
262
  border: 1px solid var(--cardBorder);
263
  border-radius: 12px;
264
  box-shadow: 0 1px 0 rgba(0,0,0,.02);
265
- aspect-ratio: 1 / 1; /* makes it square */
266
- display:flex;
267
- flex-direction:column;
268
- overflow:hidden;
 
 
269
  }
 
270
  .card-header{
271
  display:flex;
272
  align-items:center;
@@ -275,10 +258,21 @@ app_ui = ui.page_fluid(
275
  border-bottom: 1px solid #eef2f8;
276
  flex: 0 0 auto;
277
  }
278
- .card-body{
 
279
  padding: 10px;
280
- overflow:auto; /* internal scroll if needed */
 
 
281
  flex: 1 1 auto;
 
 
 
 
 
 
 
 
282
  }
283
 
284
  .del-btn{
@@ -290,15 +284,15 @@ app_ui = ui.page_fluid(
290
  cursor:pointer;
291
  padding: 0 6px;
292
  }
293
- .del-btn:hover{ color:#1c2b44; }
294
 
295
  label{
296
  display:block;
297
  font-size: 12px;
298
  color: var(--muted);
299
- margin: 10px 0 4px;
300
  font-weight: 650;
301
  }
 
302
  input, textarea{
303
  width:100%;
304
  box-sizing:border-box;
@@ -309,11 +303,24 @@ app_ui = ui.page_fluid(
309
  outline: none;
310
  background: white;
311
  }
312
- textarea{ min-height: 84px; resize: vertical; }
 
 
 
 
 
 
 
 
 
 
 
 
 
313
 
314
  .resource-link{
315
  display:inline-block;
316
- margin-top: 10px;
317
  font-size: 13px;
318
  }
319
 
@@ -326,7 +333,7 @@ app_ui = ui.page_fluid(
326
  color: #41516b;
327
  }
328
 
329
- /* Modal for delete confirmation */
330
  .modal-backdrop{
331
  position: fixed;
332
  inset: 0;
@@ -403,7 +410,7 @@ app_ui = ui.page_fluid(
403
  const errBox = document.getElementById("err");
404
 
405
  const state = { version: null, cards: [] };
406
- const saveTimers = new Map(); // cardId -> timeout
407
 
408
  function showErr(msg){
409
  if(!msg){ errBox.style.display="none"; errBox.textContent=""; return; }
@@ -446,7 +453,7 @@ app_ui = ui.page_fluid(
446
  backdrop.innerHTML = `
447
  <div class="modal" role="dialog" aria-modal="true">
448
  <h3>Delete card?</h3>
449
- <div class="muted">This is permanent and will affect everyone viewing this Space.</div>
450
 
451
  <div class="checks">
452
  <label><input type="checkbox" id="chk1" /> I understand this is permanent</label>
@@ -471,21 +478,15 @@ app_ui = ui.page_fluid(
471
  const confirm = backdrop.querySelector("#confirm");
472
 
473
  function updateEnabled(){
474
- const ok = chk1.checked && chk2.checked && (typed.value || "") === "DELETE";
475
- confirm.disabled = !ok;
476
  }
477
  chk1.addEventListener("change", updateEnabled);
478
  chk2.addEventListener("change", updateEnabled);
479
  typed.addEventListener("input", updateEnabled);
480
 
481
- function close(){
482
- backdrop.remove();
483
- }
484
-
485
  cancel.addEventListener("click", close);
486
- backdrop.addEventListener("click", (e) => {
487
- if(e.target === backdrop) close();
488
- });
489
 
490
  confirm.addEventListener("click", async () => {
491
  try{
@@ -501,7 +502,6 @@ app_ui = ui.page_fluid(
501
  }
502
  });
503
 
504
- // focus typing field
505
  typed.focus();
506
  }
507
 
@@ -525,19 +525,26 @@ app_ui = ui.page_fluid(
525
  <button class="del-btn" title="Delete">×</button>
526
  </div>
527
 
528
- <div class="card-body">
529
- <label>Model</label>
530
- <input type="text" class="model" value="${escapeHtml(c.model)}" />
531
-
532
- <label>How it Works</label>
533
- <textarea class="how">${escapeHtml(c.how_it_works)}</textarea>
534
-
535
- <label>Use Cases</label>
536
- <textarea class="use">${escapeHtml(c.use_cases)}</textarea>
 
 
 
 
 
 
 
537
 
 
538
  <label>Link</label>
539
  <input type="text" class="link" value="${escapeHtml(c.link)}" />
540
-
541
  <div class="linkwrap"></div>
542
  </div>
543
  `;
@@ -581,13 +588,10 @@ app_ui = ui.page_fluid(
581
  updateLink();
582
  const existing = saveTimers.get(c.id);
583
  if(existing) clearTimeout(existing);
584
- saveTimers.set(c.id, setTimeout(saveNow, 400));
585
  }
586
 
587
- [modelEl, howEl, useEl, linkEl].forEach(el => {
588
- el.addEventListener("input", scheduleSave);
589
- });
590
-
591
  delBtn.addEventListener("click", () => openDeleteModal(c.id));
592
 
593
  updateLink();
@@ -625,7 +629,6 @@ app_ui = ui.page_fluid(
625
  }
626
  });
627
 
628
- // initial load + polling
629
  refresh().then(render);
630
  setInterval(refresh, 3000);
631
  })();
@@ -646,5 +649,4 @@ routes = [
646
  Mount("/", app=app_shiny),
647
  ]
648
 
649
- # Exported ASGI app
650
  app = Starlette(routes=routes)
 
48
 
49
 
50
  def _normalize_state(obj: dict) -> dict:
 
 
 
 
51
  if not isinstance(obj, dict):
52
  obj = {}
53
 
 
64
  if not isinstance(c, dict):
65
  continue
66
 
67
+ model = c.get("model", c.get("title", ""))
68
+ how_it_works = c.get("how_it_works", c.get("how", ""))
69
+ use_cases = c.get("use_cases", c.get("use", ""))
70
+ link = c.get("link", c.get("resource_url", ""))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  normalized.append(
73
  {
 
168
 
169
 
170
  # -----------------------------------------------------------------------------
171
+ # Shiny UI
172
  # -----------------------------------------------------------------------------
173
 
174
  app_ui = ui.page_fluid(
 
206
  cursor: pointer;
207
  box-shadow: 0 2px 8px rgba(0,0,0,.14);
208
  }
 
209
 
 
210
  .err{
211
  margin: 0 6px 10px 6px;
212
  padding: 10px 12px;
 
224
  grid-template-columns: repeat(4, minmax(260px, 1fr));
225
  gap: 16px;
226
  padding: 2px 6px 18px 6px;
227
+ align-items: start;
228
  }
229
  @media (max-width: 1200px){
230
  .cards-grid{ grid-template-columns: repeat(3, minmax(240px, 1fr)); }
 
236
  .cards-grid{ grid-template-columns: 1fr; }
237
  }
238
 
239
+ /* Card = TRUE square; content cannot force it taller */
240
  .card{
241
  background: var(--cardBg);
242
  border: 1px solid var(--cardBorder);
243
  border-radius: 12px;
244
  box-shadow: 0 1px 0 rgba(0,0,0,.02);
245
+ aspect-ratio: 1 / 1;
246
+ width: 100%;
247
+ overflow: hidden;
248
+ display: flex;
249
+ flex-direction: column;
250
+ min-height: 0; /* critical so children don't "push" height */
251
  }
252
+
253
  .card-header{
254
  display:flex;
255
  align-items:center;
 
258
  border-bottom: 1px solid #eef2f8;
259
  flex: 0 0 auto;
260
  }
261
+
262
+ .card-main{
263
  padding: 10px;
264
+ display:flex;
265
+ flex-direction:column;
266
+ gap: 8px;
267
  flex: 1 1 auto;
268
+ min-height: 0; /* allow flex children to shrink */
269
+ overflow: hidden;/* NO card scrolling */
270
+ }
271
+
272
+ .card-footer{
273
+ padding: 10px;
274
+ border-top: 1px solid #eef2f8;
275
+ flex: 0 0 auto; /* always visible */
276
  }
277
 
278
  .del-btn{
 
284
  cursor:pointer;
285
  padding: 0 6px;
286
  }
 
287
 
288
  label{
289
  display:block;
290
  font-size: 12px;
291
  color: var(--muted);
292
+ margin: 0 0 4px;
293
  font-weight: 650;
294
  }
295
+
296
  input, textarea{
297
  width:100%;
298
  box-sizing:border-box;
 
303
  outline: none;
304
  background: white;
305
  }
306
+
307
+ /* Flex textareas so they fit inside the square; scroll inside textarea only */
308
+ .grow{
309
+ display:flex;
310
+ flex-direction:column;
311
+ flex: 1 1 0;
312
+ min-height: 0;
313
+ }
314
+ .grow textarea{
315
+ flex: 1 1 0;
316
+ min-height: 0;
317
+ resize: none;
318
+ overflow: auto;
319
+ }
320
 
321
  .resource-link{
322
  display:inline-block;
323
+ margin-top: 8px;
324
  font-size: 13px;
325
  }
326
 
 
333
  color: #41516b;
334
  }
335
 
336
+ /* Delete modal */
337
  .modal-backdrop{
338
  position: fixed;
339
  inset: 0;
 
410
  const errBox = document.getElementById("err");
411
 
412
  const state = { version: null, cards: [] };
413
+ const saveTimers = new Map();
414
 
415
  function showErr(msg){
416
  if(!msg){ errBox.style.display="none"; errBox.textContent=""; return; }
 
453
  backdrop.innerHTML = `
454
  <div class="modal" role="dialog" aria-modal="true">
455
  <h3>Delete card?</h3>
456
+ <div class="muted">This is permanent and affects everyone viewing this Space.</div>
457
 
458
  <div class="checks">
459
  <label><input type="checkbox" id="chk1" /> I understand this is permanent</label>
 
478
  const confirm = backdrop.querySelector("#confirm");
479
 
480
  function updateEnabled(){
481
+ confirm.disabled = !(chk1.checked && chk2.checked && (typed.value || "") === "DELETE");
 
482
  }
483
  chk1.addEventListener("change", updateEnabled);
484
  chk2.addEventListener("change", updateEnabled);
485
  typed.addEventListener("input", updateEnabled);
486
 
487
+ function close(){ backdrop.remove(); }
 
 
 
488
  cancel.addEventListener("click", close);
489
+ backdrop.addEventListener("click", (e) => { if(e.target === backdrop) close(); });
 
 
490
 
491
  confirm.addEventListener("click", async () => {
492
  try{
 
502
  }
503
  });
504
 
 
505
  typed.focus();
506
  }
507
 
 
525
  <button class="del-btn" title="Delete">×</button>
526
  </div>
527
 
528
+ <div class="card-main">
529
+ <div>
530
+ <label>Model</label>
531
+ <input type="text" class="model" value="${escapeHtml(c.model)}" />
532
+ </div>
533
+
534
+ <div class="grow">
535
+ <label>How it Works</label>
536
+ <textarea class="how">${escapeHtml(c.how_it_works)}</textarea>
537
+ </div>
538
+
539
+ <div class="grow">
540
+ <label>Use Cases</label>
541
+ <textarea class="use">${escapeHtml(c.use_cases)}</textarea>
542
+ </div>
543
+ </div>
544
 
545
+ <div class="card-footer">
546
  <label>Link</label>
547
  <input type="text" class="link" value="${escapeHtml(c.link)}" />
 
548
  <div class="linkwrap"></div>
549
  </div>
550
  `;
 
588
  updateLink();
589
  const existing = saveTimers.get(c.id);
590
  if(existing) clearTimeout(existing);
591
+ saveTimers.set(c.id, setTimeout(saveNow, 350));
592
  }
593
 
594
+ [modelEl, howEl, useEl, linkEl].forEach(el => el.addEventListener("input", scheduleSave));
 
 
 
595
  delBtn.addEventListener("click", () => openDeleteModal(c.id));
596
 
597
  updateLink();
 
629
  }
630
  });
631
 
 
632
  refresh().then(render);
633
  setInterval(refresh, 3000);
634
  })();
 
649
  Mount("/", app=app_shiny),
650
  ]
651
 
 
652
  app = Starlette(routes=routes)