Update app.py
Browse files
app.py
CHANGED
@@ -414,107 +414,112 @@ def load_session_history(selected_session):
|
|
414 |
""", (session_id,))
|
415 |
history = c.fetchall()
|
416 |
conn.close()
|
417 |
-
|
418 |
-
|
419 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
420 |
<style>
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
</style>
|
|
|
446 |
<div class="prompt-grid">
|
447 |
"""
|
448 |
-
|
449 |
-
#
|
450 |
-
|
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 |
-
|
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 |
-
|
|
|
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 |
-
|
509 |
-
|
510 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
511 |
}}
|
512 |
-
}}
|
513 |
</script>
|
514 |
"""
|
515 |
-
|
516 |
-
return gr.HTML(value=
|
517 |
-
|
518 |
except Exception as e:
|
519 |
print(f"Error loading session history: {e}")
|
520 |
return gr.HTML("Error loading history")
|
|
|
414 |
""", (session_id,))
|
415 |
history = c.fetchall()
|
416 |
conn.close()
|
417 |
+
|
418 |
+
# JavaScript 데이터 준비
|
419 |
+
js_data = []
|
420 |
+
for prompt, response, _ in history:
|
421 |
+
js_data.append({
|
422 |
+
'prompt': html.escape(prompt).replace('`', '\\`'),
|
423 |
+
'response': html.escape(response).replace('`', '\\`')
|
424 |
+
})
|
425 |
+
|
426 |
+
# HTML 템플릿 준비
|
427 |
+
html_content = f"""
|
428 |
<style>
|
429 |
+
.prompt-grid {{
|
430 |
+
display: grid;
|
431 |
+
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
|
432 |
+
gap: 20px;
|
433 |
+
padding: 20px;
|
434 |
+
}}
|
435 |
+
.prompt-card {{
|
436 |
+
background: white;
|
437 |
+
border: 1px solid #eee;
|
438 |
+
border-radius: 8px;
|
439 |
+
padding: 15px;
|
440 |
+
cursor: pointer;
|
441 |
+
transition: all 0.3s ease;
|
442 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
443 |
+
}}
|
444 |
+
.prompt-card:hover {{
|
445 |
+
transform: translateY(-2px);
|
446 |
+
box-shadow: 0 4px 10px rgba(0,0,0,0.15);
|
447 |
+
}}
|
448 |
+
.timestamp {{
|
449 |
+
font-size: 0.8em;
|
450 |
+
color: #666;
|
451 |
+
margin-top: 10px;
|
452 |
+
}}
|
453 |
</style>
|
454 |
+
|
455 |
<div class="prompt-grid">
|
456 |
"""
|
457 |
+
|
458 |
+
# 카드 생성
|
459 |
+
for i, (prompt, _, timestamp) in enumerate(history):
|
|
|
|
|
460 |
short_prompt = prompt[:100] + "..." if len(prompt) > 100 else prompt
|
461 |
formatted_time = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M')
|
462 |
|
463 |
+
html_content += f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
464 |
<div class="prompt-card" onclick="executeHistoryItem({i})">
|
465 |
+
<div>{html.escape(short_prompt)}</div>
|
466 |
<div class="timestamp">{formatted_time}</div>
|
467 |
</div>
|
468 |
"""
|
469 |
+
|
470 |
+
# JavaScript 코드 추가
|
471 |
+
html_content += f"""
|
472 |
</div>
|
473 |
<script>
|
474 |
+
const historyData = {json.dumps(js_data)};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
475 |
|
476 |
+
function executeHistoryItem(index) {{
|
477 |
+
const item = historyData[index];
|
478 |
+
|
479 |
+
// 텍스트 영역 업데이트
|
480 |
+
const textarea = document.querySelector('textarea');
|
481 |
+
if (textarea) {{
|
482 |
+
textarea.value = item.prompt;
|
483 |
+
}}
|
484 |
+
|
485 |
+
// 코드 추출 및 실행
|
486 |
+
let code = item.response;
|
487 |
+
if (code.includes('```html')) {{
|
488 |
+
const match = code.match(/```html\\n([\\s\\S]*?)\\n```/);
|
489 |
+
if (match) {{
|
490 |
+
code = match[1];
|
491 |
+
}}
|
492 |
+
}}
|
493 |
+
|
494 |
+
// iframe 업데이트
|
495 |
+
const encodedHtml = btoa(unescape(encodeURIComponent(code.trim())));
|
496 |
+
const dataUri = `data:text/html;charset=utf-8;base64,${{encodedHtml}}`;
|
497 |
+
|
498 |
+
const iframe = document.querySelector('.html_content iframe');
|
499 |
+
if (iframe) {{
|
500 |
+
iframe.src = dataUri;
|
501 |
+
}}
|
502 |
+
|
503 |
+
// 실행 버튼 찾기 및 클릭
|
504 |
+
const buttons = document.querySelectorAll('button');
|
505 |
+
for (const button of buttons) {{
|
506 |
+
if (button.textContent.includes('Code 실행')) {{
|
507 |
+
button.click();
|
508 |
+
break;
|
509 |
+
}}
|
510 |
+
}}
|
511 |
+
|
512 |
+
// 드로어 닫기
|
513 |
+
const drawer = document.querySelector('.session-drawer');
|
514 |
+
if (drawer) {{
|
515 |
+
drawer.style.display = 'none';
|
516 |
+
}}
|
517 |
}}
|
|
|
518 |
</script>
|
519 |
"""
|
520 |
+
|
521 |
+
return gr.HTML(value=html_content)
|
522 |
+
|
523 |
except Exception as e:
|
524 |
print(f"Error loading session history: {e}")
|
525 |
return gr.HTML("Error loading history")
|