lollms-webui/utilities/execution_engines/mermaid_execution_engine.py

157 lines
5.8 KiB
Python
Raw Normal View History

2024-02-05 22:50:40 +00:00
"""
project: lollms_webui
file: shell_execution_engine.py
author: ParisNeo
description:
This is a utility for executing python code
"""
2024-12-19 12:48:57 +00:00
2024-02-05 22:50:40 +00:00
import json
2024-12-19 12:48:57 +00:00
import subprocess
import time
from ascii_colors import get_trace_exception, trace_exception
2024-02-28 00:06:59 +00:00
from lollms.client_session import Client
2024-06-23 20:27:27 +00:00
from lollms.utilities import discussion_path_to_url
2024-02-28 00:06:59 +00:00
2024-12-19 12:48:57 +00:00
from lollms_webui import LOLLMSWebUI
lollmsElfServer: LOLLMSWebUI = LOLLMSWebUI.get_instance()
2024-02-05 22:50:40 +00:00
2024-04-21 15:14:00 +00:00
def build_mermaid_output(code, ifram_name=None):
2024-02-05 22:50:40 +00:00
"""
This function creates an HTML5 iframe with the given HTML content and iframe name.
Args:
2024-02-08 22:04:27 +00:00
code (str): The mermaid code
2024-02-05 22:50:40 +00:00
ifram_name (str, optional): The name of the iframe. Defaults to "unnamed".
Returns:
str: The HTML string for the iframe.
"""
# Start the timer.
2024-04-21 15:14:00 +00:00
start_time = time.time()
if ifram_name is not None:
2024-12-19 12:48:57 +00:00
rendered = "\n".join(
[
'<div style="width: 100%; margin: 0 auto;">',
f'<iframe id="{ifram_name}" style="width: 100%" srcdoc="',
"<style>",
"iframe {",
"width: 100%;",
"height: 100%;",
"border: none;",
"}",
".mermaid {",
"background-color: transparent;",
"padding: 20px;",
"border-radius: 10px;",
"display: flex;",
"justify-content: center;",
"align-items: center;",
"height: 100%;",
"}",
"</style>",
"<div class='mermaid'>",
"\n".join([c for c in code.split("\n") if c.strip() != ""]),
"</div>",
"<script src='https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js'></script>",
"<script>",
"// Initialize the mermaid library and render our diagram",
"mermaid.initialize({ startOnLoad: true });",
"// Function to save SVG content to a file",
"function saveSVG() {",
'var svg = document.querySelector(".mermaid > svg");',
"var serializer = new XMLSerializer();",
"var source = serializer.serializeToString(svg);",
'var blob = new Blob([source], {type: "image/svg+xml;charset=utf-8"});',
"var url = URL.createObjectURL(blob);",
'var a = document.createElement("a");',
"a.href = url;",
'a.download = "diagram.svg";',
"a.click();",
"}",
"</script>",
"<div style='text-align: center;'>",
"</div>",
'<button onclick="saveSVG()">Save SVG</button>',
'" style="width: 100%; height: 600px; border: none;"></iframe>',
"</div>",
2024-04-21 15:14:00 +00:00
]
)
else:
2024-12-19 12:48:57 +00:00
rendered = "\n".join(
[
'<div style="width: 100%; margin: 0 auto;">',
"<style>",
".mermaid {",
"background-color: transparent;",
"padding: 20px;",
"border-radius: 10px;",
"display: flex;",
"justify-content: center;",
"align-items: center;",
"height: 100%;",
"}",
"</style>",
"<div class='mermaid'>",
"\n".join([c for c in code.split("\n") if c.strip() != ""]),
"</div>",
"<script src='https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js'></script>",
"<script>",
"// Initialize the mermaid library and render our diagram",
"mermaid.initialize({ startOnLoad: true });",
"// Function to save SVG content to a file",
"function saveSVG() {",
'var svg = document.querySelector(".mermaid > svg");',
"var serializer = new XMLSerializer();",
"var source = serializer.serializeToString(svg);",
'var blob = new Blob([source], {type: "image/svg+xml;charset=utf-8"});',
"var url = URL.createObjectURL(blob);",
'var a = document.createElement("a");',
"a.href = url;",
'a.download = "diagram.svg";',
"a.click();",
"}",
"</script>",
"<div style='text-align: center;'>",
"</div>",
'<button onclick="saveSVG()">Save SVG</button>',
"</div>",
2024-04-21 15:14:00 +00:00
]
)
2024-02-05 22:50:40 +00:00
execution_time = time.time() - start_time
return {"output": rendered, "execution_time": execution_time}
2024-02-08 22:04:27 +00:00
2024-12-19 12:48:57 +00:00
def execute_mermaid(code, client: Client, message_id, build_file=False):
2024-04-21 00:50:43 +00:00
if build_file:
# Start the timer.
start_time = time.time()
2024-12-19 12:48:57 +00:00
if (
not "http" in lollmsElfServer.config.host
and not "https" in lollmsElfServer.config.host
):
host = "http://" + lollmsElfServer.config.host
2024-04-21 00:50:43 +00:00
else:
host = lollmsElfServer.config.host
2024-02-05 22:50:40 +00:00
2024-04-21 00:50:43 +00:00
# Create a temporary file.
root_folder = client.discussion.discussion_folder
2024-12-19 12:48:57 +00:00
root_folder.mkdir(parents=True, exist_ok=True)
tmp_file = root_folder / f"ai_code_{message_id}.html"
with open(tmp_file, "w", encoding="utf8") as f:
2024-04-21 02:05:53 +00:00
f.write(build_mermaid_output(code)["output"])
2024-07-05 04:53:17 +00:00
link = f"{host}:{lollmsElfServer.config.port}{discussion_path_to_url(tmp_file)}"
2024-04-21 00:50:43 +00:00
# Stop the timer.
execution_time = time.time() - start_time
2024-12-19 12:48:57 +00:00
output_json = {
"output": f'<b>Page built successfully</b><br><a href="{link}" target="_blank">Press here to view the page</a>',
"execution_time": execution_time,
}
2024-04-21 00:50:43 +00:00
return output_json
else:
2024-04-21 15:14:00 +00:00
return build_mermaid_output(code, "app_iframe")