seawolf2357 commited on
Commit
b7a824d
·
verified ·
1 Parent(s): 6af9062

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -30
app.py CHANGED
@@ -442,58 +442,100 @@ def load_session_history(selected_session):
442
  margin-top: 10px;
443
  }
444
  </style>
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': html.escape(prompt),
456
- 'response': html.escape(response),
457
- 'short_prompt': html.escape(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
  // Code 실행 버튼 찾기 및 클릭
482
- const buttons = document.querySelectorAll('button');
483
- for (const button of buttons) {{
484
- if (button.textContent.includes('Code 실행')) {{
485
- setTimeout(() => button.click(), 100);
486
- break;
487
- }}
488
- }}
 
 
489
 
490
  // 드로어 닫기
491
  const drawer = document.querySelector('.session-drawer');
492
- if (drawer) {{
 
493
  drawer.style.display = 'none';
494
- }}
495
- }}
496
- }}
 
 
 
 
 
 
 
497
  </script>
498
  """
499
 
 
442
  margin-top: 10px;
443
  }
444
  </style>
 
445
  """
446
 
447
+ # jQuery 추가
448
+ html_content += """
449
+ <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
450
+ """
451
+
452
+ html_content += """<div class="prompt-grid">"""
453
+
454
  for i, (prompt, response, timestamp) in enumerate(history):
455
+ short_prompt = prompt[:100] + "..." if len(prompt) > 100 else short_prompt
456
  formatted_time = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M')
457
 
458
+ # HTML 이스케이프 처리
459
+ escaped_prompt = html.escape(prompt).replace('"', '&quot;')
460
+ escaped_response = html.escape(response).replace('"', '&quot;')
 
 
 
461
 
462
  html_content += f"""
463
+ <div class="prompt-card"
464
+ data-prompt="{escaped_prompt}"
465
+ data-response="{escaped_response}"
466
+ onclick="handleCardClick(this)">
467
  <div>{html.escape(short_prompt)}</div>
468
  <div class="timestamp">{formatted_time}</div>
469
  </div>
470
  """
471
 
472
+ html_content += """
473
  </div>
474
  <script>
475
+ function handleCardClick(card) {
476
+ console.log('Card clicked');
477
+
478
+ // 프롬프트와 응답 데이터 가져오기
479
+ const prompt = card.getAttribute('data-prompt');
480
+ const response = card.getAttribute('data-response');
481
+
482
+ console.log('Prompt:', prompt);
483
+ console.log('Response:', response);
484
 
485
+ // 텍스트 영역 찾기 및 업데이트
486
  const textarea = document.querySelector('textarea');
487
+ if (textarea) {
488
+ console.log('Found textarea');
489
+ textarea.value = prompt;
490
+
491
+ // 코드 추출
492
+ let code = response;
493
+ if (response.includes('```html')) {
494
+ const match = response.match(/```html\\n([\\s\\S]*?)\\n```/);
495
+ if (match) {
496
+ code = match[1];
497
+ }
498
+ }
499
+
500
+ console.log('Extracted code:', code);
501
+
502
+ // Base64 인코딩
503
+ const encodedHtml = btoa(unescape(encodeURIComponent(code.trim())));
504
+ const dataUri = `data:text/html;charset=utf-8;base64,${encodedHtml}`;
505
+
506
+ // iframe 업데이트
507
+ const iframe = document.querySelector('.html_content iframe');
508
+ if (iframe) {
509
+ console.log('Found iframe');
510
+ iframe.src = dataUri;
511
+ }
512
 
513
  // Code 실행 버튼 찾기 및 클릭
514
+ setTimeout(() => {
515
+ const executeButton = $('button:contains("Code 실행")');
516
+ if (executeButton.length) {
517
+ console.log('Found execute button');
518
+ executeButton[0].click();
519
+ } else {
520
+ console.log('Execute button not found');
521
+ }
522
+ }, 100);
523
 
524
  // 드로어 닫기
525
  const drawer = document.querySelector('.session-drawer');
526
+ if (drawer) {
527
+ console.log('Closing drawer');
528
  drawer.style.display = 'none';
529
+ }
530
+ } else {
531
+ console.log('Textarea not found');
532
+ }
533
+ }
534
+
535
+ // 페이지 로드 완료 후 실행
536
+ $(document).ready(function() {
537
+ console.log('Session history loaded');
538
+ });
539
  </script>
540
  """
541