Update app.py
Browse files
app.py
CHANGED
@@ -415,24 +415,15 @@ def load_session_history(selected_session):
|
|
415 |
history = c.fetchall()
|
416 |
conn.close()
|
417 |
|
418 |
-
|
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;
|
@@ -440,90 +431,90 @@ def load_session_history(selected_session):
|
|
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"
|
|
|
|
|
|
|
465 |
<div>{html.escape(short_prompt)}</div>
|
466 |
<div class="timestamp">{formatted_time}</div>
|
467 |
</div>
|
468 |
"""
|
469 |
|
470 |
-
|
471 |
-
html_content += f"""
|
472 |
</div>
|
473 |
<script>
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
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")
|
526 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
527 |
def save_chat(session_id, prompt, response):
|
528 |
print(f"Starting save_chat with session_id: {session_id}")
|
529 |
conn = None
|
|
|
415 |
history = c.fetchall()
|
416 |
conn.close()
|
417 |
|
418 |
+
html_content = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
419 |
<style>
|
420 |
+
.prompt-grid {
|
421 |
display: grid;
|
422 |
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
|
423 |
gap: 20px;
|
424 |
padding: 20px;
|
425 |
+
}
|
426 |
+
.prompt-card {
|
427 |
background: white;
|
428 |
border: 1px solid #eee;
|
429 |
border-radius: 8px;
|
|
|
431 |
cursor: pointer;
|
432 |
transition: all 0.3s ease;
|
433 |
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
434 |
+
}
|
435 |
+
.prompt-card:hover {
|
436 |
transform: translateY(-2px);
|
437 |
box-shadow: 0 4px 10px rgba(0,0,0,0.15);
|
438 |
+
}
|
439 |
+
.timestamp {
|
440 |
font-size: 0.8em;
|
441 |
color: #666;
|
442 |
margin-top: 10px;
|
443 |
+
}
|
444 |
</style>
|
|
|
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 |
+
# data 속성에 정보 저장
|
453 |
html_content += f"""
|
454 |
+
<div class="prompt-card"
|
455 |
+
data-prompt="{html.escape(prompt)}"
|
456 |
+
data-response="{html.escape(response)}"
|
457 |
+
onclick="selectHistoryItem(this)">
|
458 |
<div>{html.escape(short_prompt)}</div>
|
459 |
<div class="timestamp">{formatted_time}</div>
|
460 |
</div>
|
461 |
"""
|
462 |
|
463 |
+
html_content += """
|
|
|
464 |
</div>
|
465 |
<script>
|
466 |
+
function selectHistoryItem(element) {
|
467 |
+
const event = new CustomEvent('history-item-selected', {
|
468 |
+
detail: {
|
469 |
+
prompt: element.getAttribute('data-prompt'),
|
470 |
+
response: element.getAttribute('data-response')
|
471 |
+
}
|
472 |
+
});
|
473 |
+
document.dispatchEvent(event);
|
474 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
475 |
</script>
|
476 |
"""
|
477 |
+
|
478 |
return gr.HTML(value=html_content)
|
479 |
+
|
480 |
except Exception as e:
|
481 |
print(f"Error loading session history: {e}")
|
482 |
return gr.HTML("Error loading history")
|
483 |
|
484 |
+
# 히스��리 아이템 선택 처리 함수
|
485 |
+
def handle_history_selection(evt: gr.SelectData):
|
486 |
+
try:
|
487 |
+
prompt = evt.value["prompt"]
|
488 |
+
response = evt.value["response"]
|
489 |
+
|
490 |
+
# 코드 추출
|
491 |
+
code = response
|
492 |
+
if "```html" in response:
|
493 |
+
match = re.search(r'```html\n(.*?)\n```', response, re.DOTALL)
|
494 |
+
if match:
|
495 |
+
code = match.group(1)
|
496 |
+
|
497 |
+
return (
|
498 |
+
prompt, # 입력 필드에 프롬프트 설정
|
499 |
+
send_to_sandbox(code), # 코드 실행
|
500 |
+
gr.update(active_key="render"), # 탭 상태 업데이트
|
501 |
+
gr.update(open=False) # 세션 드로어 닫기
|
502 |
+
)
|
503 |
+
except Exception as e:
|
504 |
+
print(f"Error handling history selection: {e}")
|
505 |
+
return None, None, gr.update(active_key="empty"), gr.update(open=True)
|
506 |
+
|
507 |
+
# Gradio 인터페이스에서 이벤트 핸들러 연결
|
508 |
+
with gr.Blocks(css_paths="app.css",theme=theme) as demo:
|
509 |
+
# ... (기존 코드) ...
|
510 |
+
|
511 |
+
# 세션 히스토리 이벤트 핸들러
|
512 |
+
session_history.select(
|
513 |
+
fn=handle_history_selection,
|
514 |
+
inputs=[],
|
515 |
+
outputs=[input, sandbox, state_tab, session_drawer]
|
516 |
+
)
|
517 |
+
|
518 |
def save_chat(session_id, prompt, response):
|
519 |
print(f"Starting save_chat with session_id: {session_id}")
|
520 |
conn = None
|