Update app.py
Browse files
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 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
//
|
471 |
-
document.querySelector('
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
479 |
);
|
480 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
481 |
</script>
|
482 |
"""
|
483 |
|
484 |
-
return gr.HTML(value=html_content
|
485 |
|
486 |
except Exception as e:
|
487 |
print(f"Error loading session history: {e}")
|
488 |
-
return gr.HTML("Error loading history"
|
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):
|