seawolf2357 commited on
Commit
3f03a1d
ยท
verified ยท
1 Parent(s): 5f4126d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -72
app.py CHANGED
@@ -442,96 +442,75 @@ def load_session_history(selected_session):
442
  color: #666;
443
  margin-top: 10px;
444
  }
445
- .code-display {
446
- margin-top: 20px;
447
- padding: 20px;
448
- background: #f5f5f5;
449
- border-radius: 8px;
450
- display: none;
451
- }
452
  </style>
453
  <div class="prompt-grid">
454
  """
455
 
 
 
 
456
  for i, (prompt, response, timestamp) in enumerate(history):
457
  short_prompt = prompt[:100] + "..." if len(prompt) > 100 else prompt
458
  formatted_time = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M')
459
 
460
- # HTML ์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌ
461
- escaped_prompt = html.escape(prompt)
462
- escaped_response = html.escape(response)
 
 
463
 
464
  cards_html += f"""
465
- <div class="prompt-card"
466
- onclick="executeHistoryItem(decodeURIComponent('{urllib.parse.quote(escaped_prompt)}'),
467
- decodeURIComponent('{urllib.parse.quote(escaped_response)}'))">
468
  <div>{short_prompt}</div>
469
  <div class="timestamp">{formatted_time}</div>
470
  </div>
471
  """
472
 
473
- cards_html += """
474
  </div>
475
  <script>
476
- function executeHistoryItem(prompt, response) {
477
- try {
478
- // ์ž…๋ ฅ ํ•„๋“œ ์—…๋ฐ์ดํŠธ
479
- const textarea = document.querySelector('textarea');
480
- if (textarea) {
481
- textarea.value = prompt;
482
-
483
- // ์ฝ”๋“œ ์‹คํ–‰
484
- let code = response;
485
- if (response.includes('```html')) {
486
- const match = response.match(/```html\\n([\\s\\S]*?)\\n```/);
487
- if (match) {
488
- code = match[1];
489
- }
490
- }
491
-
492
- // Base64 ์ธ์ฝ”๋”ฉ
493
- const encodedHtml = btoa(unescape(encodeURIComponent(code.trim())));
494
- const dataUri = `data:text/html;charset=utf-8;base64,${encodedHtml}`;
495
-
496
- // iframe ์—…๋ฐ์ดํŠธ
497
- const iframe = document.querySelector('.html_content iframe');
498
- if (iframe) {
499
- iframe.src = dataUri;
500
- }
501
-
502
- // ์„ธ์…˜ ๋“œ๋กœ์–ด ๋‹ซ๊ธฐ
503
- const drawer = document.querySelector('.session-drawer');
504
- if (drawer) {
505
- drawer.style.display = 'none';
506
- }
507
-
508
- // Code ์‹คํ–‰ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ๋ฎฌ๋ ˆ์ด์…˜
509
- setTimeout(() => {
510
- const executeBtn = document.querySelector('button:contains("Code ์‹คํ–‰")');
511
- if (executeBtn) {
512
- executeBtn.click();
513
- }
514
-
515
- // ๋ Œ๋” ํƒญ์œผ๋กœ ๋ณ€๊ฒฝ
516
- const tabs = document.querySelectorAll('[role="tab"]');
517
- const renderTab = Array.from(tabs).find(tab => tab.getAttribute('data-value') === 'render');
518
- if (renderTab) {
519
- renderTab.click();
520
- }
521
- }, 100);
522
- }
523
- } catch (error) {
524
- console.error('Error executing history item:', error);
525
- }
526
- }
527
-
528
- // jQuery๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ํฌํ•จ ์—ฌ๋ถ€๋กœ ๋ฒ„ํŠผ ์ฐพ๊ธฐ
529
- jQuery.expr[':'].contains = function(a, i, m) {
530
- return jQuery(a).text().toUpperCase()
531
- .indexOf(m[3].toUpperCase()) >= 0;
532
- };
533
  </script>
534
- <div class="code-display"></div>
535
  """
536
 
537
  return gr.HTML(value=cards_html)
 
442
  color: #666;
443
  margin-top: 10px;
444
  }
 
 
 
 
 
 
 
445
  </style>
446
  <div class="prompt-grid">
447
  """
448
 
449
+ # ๋ฐ์ดํ„ฐ๋ฅผ JavaScript ๋ฐฐ์—ด๋กœ ์ค€๋น„
450
+ js_data = []
451
+
452
  for i, (prompt, response, timestamp) in enumerate(history):
453
  short_prompt = prompt[:100] + "..." if len(prompt) > 100 else prompt
454
  formatted_time = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M')
455
 
456
+ # JavaScript ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์— ์ถ”๊ฐ€
457
+ js_data.append({
458
+ 'prompt': prompt,
459
+ 'response': response
460
+ })
461
 
462
  cards_html += f"""
463
+ <div class="prompt-card" onclick="executeHistoryItem({i})">
 
 
464
  <div>{short_prompt}</div>
465
  <div class="timestamp">{formatted_time}</div>
466
  </div>
467
  """
468
 
469
+ cards_html += f"""
470
  </div>
471
  <script>
472
+ // ๋ฐ์ดํ„ฐ ์ €์žฅ
473
+ const historyData = {json.dumps(js_data)};
474
+
475
+ function executeHistoryItem(index) {{
476
+ const item = historyData[index];
477
+
478
+ // ํ…์ŠคํŠธ ์˜์—ญ ์—…๋ฐ์ดํŠธ
479
+ const textarea = document.querySelector('textarea');
480
+ if (textarea) {{
481
+ textarea.value = item.prompt;
482
+ }}
483
+
484
+ // ์ฝ”๋“œ ์ถ”์ถœ ๋ฐ ์‹คํ–‰
485
+ let code = item.response;
486
+ if (code.includes('```html')) {{
487
+ code = code.match(/```html\\n([\\s\\S]*?)\\n```/)[1];
488
+ }}
489
+
490
+ // iframe ์—…๋ฐ์ดํŠธ
491
+ const encodedHtml = btoa(unescape(encodeURIComponent(code.trim())));
492
+ const dataUri = `data:text/html;charset=utf-8;base64,${{encodedHtml}}`;
493
+
494
+ const iframe = document.querySelector('.html_content iframe');
495
+ if (iframe) {{
496
+ iframe.src = dataUri;
497
+ }}
498
+
499
+ // ์‹คํ–‰ ๋ฒ„ํŠผ ํด๋ฆญ
500
+ const executeButton = Array.from(document.querySelectorAll('button')).find(
501
+ button => button.textContent.includes('Code ์‹คํ–‰')
502
+ );
503
+ if (executeButton) {{
504
+ executeButton.click();
505
+ }}
506
+
507
+ // ๋“œ๋กœ์–ด ๋‹ซ๊ธฐ
508
+ const drawer = document.querySelector('.session-drawer');
509
+ if (drawer) {{
510
+ drawer.style.display = 'none';
511
+ }}
512
+ }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
513
  </script>
 
514
  """
515
 
516
  return gr.HTML(value=cards_html)