seawolf2357 commited on
Commit
a7e0405
ยท
verified ยท
1 Parent(s): 7dfb1c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -22
app.py CHANGED
@@ -445,47 +445,80 @@ def load_session_history(selected_session):
445
  <div class="prompt-grid">
446
  """
447
 
 
 
448
  for i, (prompt, response, timestamp) in enumerate(history):
449
  short_prompt = prompt[:100] + "..." if len(prompt) > 100 else prompt
450
  formatted_time = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M')
451
 
 
 
 
 
 
 
 
452
  html_content += f"""
453
- <div class="prompt-card"
454
- data-prompt="{html.escape(prompt)}"
455
- data-response="{html.escape(response)}"
456
- onclick="gradioHandleClick(this)">
457
  <div>{html.escape(short_prompt)}</div>
458
  <div class="timestamp">{formatted_time}</div>
459
  </div>
460
  """
461
 
462
- html_content += """
463
  </div>
464
  <script>
465
- function gradioHandleClick(element) {
466
- const data = {
467
- prompt: element.getAttribute('data-prompt'),
468
- response: element.getAttribute('data-response')
469
- };
470
- // Gradio์˜ select ์ด๋ฒคํŠธ ํŠธ๋ฆฌ๊ฑฐ
471
- document.querySelector('.session-history').dispatchEvent(
472
- new CustomEvent('select', {
473
- detail: {
474
- value: data,
475
- index: 0,
476
- target: element
477
- }
478
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
479
  );
480
- }
 
 
 
 
 
 
 
 
 
481
  </script>
482
  """
483
 
484
- return gr.HTML(value=html_content, interactive=True)
485
 
486
  except Exception as e:
487
  print(f"Error loading session history: {e}")
488
- return gr.HTML("Error loading history", interactive=True)
489
 
490
  # ํžˆ์Šคํ† ๋ฆฌ ์•„์ดํ…œ ์„ ํƒ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜
491
  def handle_history_selection(evt: gr.SelectData):
 
445
  <div class="prompt-grid">
446
  """
447
 
448
+ # ํžˆ์Šคํ† ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ JavaScript ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜
449
+ js_data = []
450
  for i, (prompt, response, timestamp) in enumerate(history):
451
  short_prompt = prompt[:100] + "..." if len(prompt) > 100 else prompt
452
  formatted_time = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M')
453
 
454
+ js_data.append({
455
+ 'prompt': prompt,
456
+ 'response': response,
457
+ 'short_prompt': short_prompt,
458
+ 'timestamp': formatted_time
459
+ })
460
+
461
  html_content += f"""
462
+ <div class="prompt-card" onclick="cardClicked({i})">
 
 
 
463
  <div>{html.escape(short_prompt)}</div>
464
  <div class="timestamp">{formatted_time}</div>
465
  </div>
466
  """
467
 
468
+ html_content += f"""
469
  </div>
470
  <script>
471
+ const historyData = {json.dumps(js_data)};
472
+
473
+ function cardClicked(index) {{
474
+ const item = historyData[index];
475
+
476
+ // ํ…์ŠคํŠธ ์˜์—ญ ์—…๋ฐ์ดํŠธ
477
+ const textarea = document.querySelector('textarea');
478
+ if (textarea) {{
479
+ textarea.value = item.prompt;
480
+ }}
481
+
482
+ // ์ฝ”๋“œ ์ถ”์ถœ ๋ฐ ์‹คํ–‰
483
+ let code = item.response;
484
+ if (code.includes('```html')) {{
485
+ const match = code.match(/```html\\n([\\s\\S]*?)\\n```/);
486
+ if (match) {{
487
+ code = match[1];
488
+ }}
489
+ }}
490
+
491
+ // iframe ์—…๋ฐ์ดํŠธ
492
+ const encodedHtml = btoa(unescape(encodeURIComponent(code.trim())));
493
+ const dataUri = `data:text/html;charset=utf-8;base64,${{encodedHtml}}`;
494
+
495
+ const iframe = document.querySelector('.html_content iframe');
496
+ if (iframe) {{
497
+ iframe.src = dataUri;
498
+ }}
499
+
500
+ // ์‹คํ–‰ ๋ฒ„ํŠผ ํด๋ฆญ
501
+ const executeButton = Array.from(document.querySelectorAll('button')).find(
502
+ button => button.textContent.includes('Code ์‹คํ–‰')
503
  );
504
+ if (executeButton) {{
505
+ executeButton.click();
506
+ }}
507
+
508
+ // ๋“œ๋กœ์–ด ๋‹ซ๊ธฐ
509
+ const drawer = document.querySelector('.session-drawer');
510
+ if (drawer) {{
511
+ drawer.style.display = 'none';
512
+ }}
513
+ }}
514
  </script>
515
  """
516
 
517
+ return gr.HTML(value=html_content)
518
 
519
  except Exception as e:
520
  print(f"Error loading session history: {e}")
521
+ return gr.HTML("Error loading history")
522
 
523
  # ํžˆ์Šคํ† ๋ฆฌ ์•„์ดํ…œ ์„ ํƒ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜
524
  def handle_history_selection(evt: gr.SelectData):