mirror of
https://github.com/ParisNeo/lollms-webui.git
synced 2024-12-18 20:17:50 +00:00
Enhanced engines
This commit is contained in:
parent
102bc1acf6
commit
da8ab86154
@ -45,6 +45,8 @@ from utilities.execution_engines.html_execution_engine import execute_html
|
|||||||
|
|
||||||
from utilities.execution_engines.mermaid_execution_engine import execute_mermaid
|
from utilities.execution_engines.mermaid_execution_engine import execute_mermaid
|
||||||
from utilities.execution_engines.graphviz_execution_engine import execute_graphviz
|
from utilities.execution_engines.graphviz_execution_engine import execute_graphviz
|
||||||
|
from utilities.execution_engines.svg_execution_engine import execute_svg
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -91,6 +93,10 @@ async def execute_code(request: CodeRequest):
|
|||||||
ASCIIColors.info("Executing python code:")
|
ASCIIColors.info("Executing python code:")
|
||||||
ASCIIColors.yellow(code)
|
ASCIIColors.yellow(code)
|
||||||
return execute_python(code, client, message_id)
|
return execute_python(code, client, message_id)
|
||||||
|
if language=="svg":
|
||||||
|
ASCIIColors.info("Executing svg code:")
|
||||||
|
ASCIIColors.yellow(code)
|
||||||
|
return execute_svg(code, client, message_id)
|
||||||
if language=="javascript":
|
if language=="javascript":
|
||||||
ASCIIColors.info("Executing javascript code:")
|
ASCIIColors.info("Executing javascript code:")
|
||||||
ASCIIColors.yellow(code)
|
ASCIIColors.yellow(code)
|
||||||
|
@ -30,7 +30,7 @@ def build_graphviz_output(code, ifram_name=None):
|
|||||||
"""
|
"""
|
||||||
# Start the timer.
|
# Start the timer.
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
if ifram_name!=None:
|
if ifram_name is not None:
|
||||||
rendered = "\n".join([
|
rendered = "\n".join([
|
||||||
'<div style="width: 100%; margin: 0 auto;">',
|
'<div style="width: 100%; margin: 0 auto;">',
|
||||||
f'<iframe id="{ifram_name}" srcdoc=\'',
|
f'<iframe id="{ifram_name}" srcdoc=\'',
|
||||||
@ -120,4 +120,4 @@ def execute_graphviz(code, client:Client, message_id, build_file=False):
|
|||||||
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}
|
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}
|
||||||
return output_json
|
return output_json
|
||||||
else:
|
else:
|
||||||
return build_graphviz_output(code)
|
return build_graphviz_output(code, ifram_name="iframe")
|
||||||
|
@ -17,7 +17,7 @@ from lollms.utilities import discussion_path_2_url
|
|||||||
|
|
||||||
lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance()
|
lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance()
|
||||||
|
|
||||||
def build_javascript_output(code, ifram_name="unnamed"):
|
def build_javascript_output(code, ifram_name=None):
|
||||||
"""
|
"""
|
||||||
This function creates an HTML5 iframe with the given HTML content and iframe name.
|
This function creates an HTML5 iframe with the given HTML content and iframe name.
|
||||||
|
|
||||||
@ -29,24 +29,34 @@ def build_javascript_output(code, ifram_name="unnamed"):
|
|||||||
str: The HTML string for the iframe.
|
str: The HTML string for the iframe.
|
||||||
"""
|
"""
|
||||||
# Start the timer.
|
# Start the timer.
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
rendered = "\n".join([
|
if ifram_name is not None:
|
||||||
'<div style="width: 100%; margin: 0 auto;">',
|
rendered = "\n".join([
|
||||||
f'<iframe id="{ifram_name}" srcdoc="',
|
'<div style="width: 100%; margin: 0 auto;">',
|
||||||
'<style>',
|
f'<iframe id="{ifram_name}" srcdoc="',
|
||||||
'iframe {',
|
'<style>',
|
||||||
'width: 100%;',
|
'iframe {',
|
||||||
'height: 100%;',
|
'width: 100%;',
|
||||||
'border: none;',
|
'height: 100%;',
|
||||||
'}',
|
'border: none;',
|
||||||
'</style>',
|
'}',
|
||||||
'<script>',
|
'</style>',
|
||||||
code,
|
'<script>',
|
||||||
'</script>',
|
code,
|
||||||
'" style="width: 100%; height: 600px; border: none;"></iframe>',
|
'</script>',
|
||||||
'</div>'
|
'" style="width: 100%; height: 600px; border: none;"></iframe>',
|
||||||
]
|
'</div>'
|
||||||
)
|
]
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
rendered = "\n".join([
|
||||||
|
'<div style="width: 100%; margin: 0 auto;">',
|
||||||
|
'<script>',
|
||||||
|
code,
|
||||||
|
'</script>',
|
||||||
|
'</div>'
|
||||||
|
]
|
||||||
|
)
|
||||||
execution_time = time.time() - start_time
|
execution_time = time.time() - start_time
|
||||||
return {"output": rendered, "execution_time": execution_time}
|
return {"output": rendered, "execution_time": execution_time}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ from lollms.utilities import discussion_path_2_url
|
|||||||
|
|
||||||
lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance()
|
lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance()
|
||||||
|
|
||||||
def build_mermaid_output(code, ifram_name="unnamed"):
|
def build_mermaid_output(code, ifram_name=None):
|
||||||
"""
|
"""
|
||||||
This function creates an HTML5 iframe with the given HTML content and iframe name.
|
This function creates an HTML5 iframe with the given HTML content and iframe name.
|
||||||
|
|
||||||
@ -29,53 +29,94 @@ def build_mermaid_output(code, ifram_name="unnamed"):
|
|||||||
str: The HTML string for the iframe.
|
str: The HTML string for the iframe.
|
||||||
"""
|
"""
|
||||||
# Start the timer.
|
# Start the timer.
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
rendered = "\n".join([
|
if ifram_name is not None:
|
||||||
'<div style="width: 100%; margin: 0 auto;">',
|
rendered = "\n".join([
|
||||||
f'<iframe id="{ifram_name}" style="width: 100%" srcdoc="',
|
'<div style="width: 100%; margin: 0 auto;">',
|
||||||
'<style>',
|
f'<iframe id="{ifram_name}" style="width: 100%" srcdoc="',
|
||||||
'iframe {',
|
'<style>',
|
||||||
'width: 100%;',
|
'iframe {',
|
||||||
'height: 100%;',
|
'width: 100%;',
|
||||||
'border: none;',
|
'height: 100%;',
|
||||||
'}',
|
'border: none;',
|
||||||
'.mermaid {',
|
'}',
|
||||||
'background-color: transparent;',
|
'.mermaid {',
|
||||||
'padding: 20px;',
|
'background-color: transparent;',
|
||||||
'border-radius: 10px;',
|
'padding: 20px;',
|
||||||
'display: flex;',
|
'border-radius: 10px;',
|
||||||
'justify-content: center;',
|
'display: flex;',
|
||||||
'align-items: center;',
|
'justify-content: center;',
|
||||||
'height: 100%;',
|
'align-items: center;',
|
||||||
'}',
|
'height: 100%;',
|
||||||
'</style>',
|
'}',
|
||||||
'<div class=\'mermaid\'>',
|
'</style>',
|
||||||
"\n".join([c for c in code.split("\n") if c.strip()!=""]),
|
'<div class=\'mermaid\'>',
|
||||||
'</div>',
|
"\n".join([c for c in code.split("\n") if c.strip()!=""]),
|
||||||
'<script src=\'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js\'></script>',
|
'</div>',
|
||||||
'<script>',
|
'<script src=\'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js\'></script>',
|
||||||
'// Initialize the mermaid library and render our diagram',
|
'<script>',
|
||||||
'mermaid.initialize({ startOnLoad: true });',
|
'// Initialize the mermaid library and render our diagram',
|
||||||
'// Function to save SVG content to a file',
|
'mermaid.initialize({ startOnLoad: true });',
|
||||||
'function saveSVG() {',
|
'// Function to save SVG content to a file',
|
||||||
'var svg = document.querySelector(".mermaid > svg");',
|
'function saveSVG() {',
|
||||||
'var serializer = new XMLSerializer();',
|
'var svg = document.querySelector(".mermaid > svg");',
|
||||||
'var source = serializer.serializeToString(svg);',
|
'var serializer = new XMLSerializer();',
|
||||||
'var blob = new Blob([source], {type: "image/svg+xml;charset=utf-8"});',
|
'var source = serializer.serializeToString(svg);',
|
||||||
'var url = URL.createObjectURL(blob);',
|
'var blob = new Blob([source], {type: "image/svg+xml;charset=utf-8"});',
|
||||||
'var a = document.createElement("a");',
|
'var url = URL.createObjectURL(blob);',
|
||||||
'a.href = url;',
|
'var a = document.createElement("a");',
|
||||||
'a.download = "diagram.svg";',
|
'a.href = url;',
|
||||||
'a.click();',
|
'a.download = "diagram.svg";',
|
||||||
'}',
|
'a.click();',
|
||||||
'</script>',
|
'}',
|
||||||
'<div style=\'text-align: center;\'>',
|
'</script>',
|
||||||
'</div>',
|
'<div style=\'text-align: center;\'>',
|
||||||
'<button onclick="saveSVG()">Save SVG</button>',
|
'</div>',
|
||||||
'" style="width: 100%; height: 600px; border: none;"></iframe>',
|
'<button onclick="saveSVG()">Save SVG</button>',
|
||||||
'</div>'
|
'" style="width: 100%; height: 600px; border: none;"></iframe>',
|
||||||
]
|
'</div>'
|
||||||
)
|
]
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
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>'
|
||||||
|
]
|
||||||
|
)
|
||||||
execution_time = time.time() - start_time
|
execution_time = time.time() - start_time
|
||||||
return {"output": rendered, "execution_time": execution_time}
|
return {"output": rendered, "execution_time": execution_time}
|
||||||
|
|
||||||
@ -104,4 +145,4 @@ def execute_mermaid(code, client:Client, message_id, build_file=False):
|
|||||||
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}
|
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}
|
||||||
return output_json
|
return output_json
|
||||||
else:
|
else:
|
||||||
return build_mermaid_output(code)
|
return build_mermaid_output(code, "app_iframe")
|
||||||
|
@ -17,7 +17,7 @@ from lollms.utilities import discussion_path_2_url
|
|||||||
|
|
||||||
lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance()
|
lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance()
|
||||||
|
|
||||||
def build_svg_output(code, ifram_name="unnamed"):
|
def build_svg_output(code, ifram_name=None):
|
||||||
"""
|
"""
|
||||||
This function creates an HTML5 iframe with the given HTML content and iframe name.
|
This function creates an HTML5 iframe with the given HTML content and iframe name.
|
||||||
|
|
||||||
@ -29,53 +29,94 @@ def build_svg_output(code, ifram_name="unnamed"):
|
|||||||
str: The HTML string for the iframe.
|
str: The HTML string for the iframe.
|
||||||
"""
|
"""
|
||||||
# Start the timer.
|
# Start the timer.
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
rendered = "\n".join([
|
if ifram_name is not None:
|
||||||
'<div style="width: 100%; margin: 0 auto;">',
|
rendered = "\n".join([
|
||||||
f'<iframe id="{ifram_name}" style="width: 100%" srcdoc="',
|
'<div style="width: 100%; margin: 0 auto;">',
|
||||||
'<style>',
|
f'<iframe id="{ifram_name}" style="width: 100%" srcdoc="',
|
||||||
'iframe {',
|
'<style>',
|
||||||
'width: 100%;',
|
'iframe {',
|
||||||
'height: 100%;',
|
'width: 100%;',
|
||||||
'border: none;',
|
'height: 100%;',
|
||||||
'}',
|
'border: none;',
|
||||||
'.svg {',
|
'}',
|
||||||
'background-color: transparent;',
|
'.svg {',
|
||||||
'padding: 20px;',
|
'background-color: transparent;',
|
||||||
'border-radius: 10px;',
|
'padding: 20px;',
|
||||||
'display: flex;',
|
'border-radius: 10px;',
|
||||||
'justify-content: center;',
|
'display: flex;',
|
||||||
'align-items: center;',
|
'justify-content: center;',
|
||||||
'height: 100%;',
|
'align-items: center;',
|
||||||
'}',
|
'height: 100%;',
|
||||||
'</style>',
|
'}',
|
||||||
'<div class=\'svg\'>',
|
'</style>',
|
||||||
"\n".join([c for c in code.split("\n") if c.strip()!=""]),
|
'<div class=\'svg\'>',
|
||||||
'</div>',
|
"\n".join([c for c in code.split("\n") if c.strip()!=""]),
|
||||||
'<script src=\'https://cdn.jsdelivr.net/npm/svg/dist/svg.min.js\'></script>',
|
'</div>',
|
||||||
'<script>',
|
'<script src=\'https://cdn.jsdelivr.net/npm/svg/dist/svg.min.js\'></script>',
|
||||||
'// Initialize the svg library and render our diagram',
|
'<script>',
|
||||||
'svg.initialize({ startOnLoad: true });',
|
'// Initialize the svg library and render our diagram',
|
||||||
'// Function to save SVG content to a file',
|
'svg.initialize({ startOnLoad: true });',
|
||||||
'function saveSVG() {',
|
'// Function to save SVG content to a file',
|
||||||
'var svg = document.querySelector(".svg > svg");',
|
'function saveSVG() {',
|
||||||
'var serializer = new XMLSerializer();',
|
'var svg = document.querySelector(".svg > svg");',
|
||||||
'var source = serializer.serializeToString(svg);',
|
'var serializer = new XMLSerializer();',
|
||||||
'var blob = new Blob([source], {type: "image/svg+xml;charset=utf-8"});',
|
'var source = serializer.serializeToString(svg);',
|
||||||
'var url = URL.createObjectURL(blob);',
|
'var blob = new Blob([source], {type: "image/svg+xml;charset=utf-8"});',
|
||||||
'var a = document.createElement("a");',
|
'var url = URL.createObjectURL(blob);',
|
||||||
'a.href = url;',
|
'var a = document.createElement("a");',
|
||||||
'a.download = "diagram.svg";',
|
'a.href = url;',
|
||||||
'a.click();',
|
'a.download = "diagram.svg";',
|
||||||
'}',
|
'a.click();',
|
||||||
'</script>',
|
'}',
|
||||||
'<div style=\'text-align: center;\'>',
|
'</script>',
|
||||||
'</div>',
|
'<div style=\'text-align: center;\'>',
|
||||||
'<button onclick="saveSVG()">Save SVG</button>',
|
'</div>',
|
||||||
'" style="width: 100%; height: 600px; border: none;"></iframe>',
|
'<button onclick="saveSVG()">Save SVG</button>',
|
||||||
'</div>'
|
'" style="width: 100%; height: 600px; border: none;"></iframe>',
|
||||||
]
|
'</div>'
|
||||||
)
|
]
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
rendered = "\n".join([
|
||||||
|
'<div style="width: 100%; margin: 0 auto;">',
|
||||||
|
'<style>',
|
||||||
|
'.svg {',
|
||||||
|
'background-color: transparent;',
|
||||||
|
'padding: 20px;',
|
||||||
|
'border-radius: 10px;',
|
||||||
|
'display: flex;',
|
||||||
|
'justify-content: center;',
|
||||||
|
'align-items: center;',
|
||||||
|
'height: 100%;',
|
||||||
|
'}',
|
||||||
|
'</style>',
|
||||||
|
'<div class=\'svg\'>',
|
||||||
|
"\n".join([c for c in code.split("\n") if c.strip()!=""]),
|
||||||
|
'</div>',
|
||||||
|
'<script src=\'https://cdn.jsdelivr.net/npm/svg/dist/svg.min.js\'></script>',
|
||||||
|
'<script>',
|
||||||
|
'// Initialize the svg library and render our diagram',
|
||||||
|
'svg.initialize({ startOnLoad: true });',
|
||||||
|
'// Function to save SVG content to a file',
|
||||||
|
'function saveSVG() {',
|
||||||
|
'var svg = document.querySelector(".svg > 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>'
|
||||||
|
]
|
||||||
|
)
|
||||||
execution_time = time.time() - start_time
|
execution_time = time.time() - start_time
|
||||||
return {"output": rendered, "execution_time": execution_time}
|
return {"output": rendered, "execution_time": execution_time}
|
||||||
|
|
||||||
@ -95,13 +136,13 @@ def execute_svg(code, client:Client, message_id, build_file=False):
|
|||||||
# Create a temporary file.
|
# Create a temporary file.
|
||||||
root_folder = client.discussion.discussion_folder
|
root_folder = client.discussion.discussion_folder
|
||||||
root_folder.mkdir(parents=True,exist_ok=True)
|
root_folder.mkdir(parents=True,exist_ok=True)
|
||||||
tmp_file = root_folder/f"ai_code_{message_id}.html"
|
tmp_file = root_folder/f"ai_svg_{message_id}.svg"
|
||||||
with open(tmp_file,"w",encoding="utf8") as f:
|
with open(tmp_file,"w",encoding="utf8") as f:
|
||||||
f.write(build_svg_output(code)["output"])
|
f.write(code)
|
||||||
link = f"{host}:{lollmsElfServer.config.port}/{discussion_path_2_url(tmp_file)}"
|
link = f"{host}:{lollmsElfServer.config.port}/{discussion_path_2_url(tmp_file)}"
|
||||||
# Stop the timer.
|
# Stop the timer.
|
||||||
execution_time = time.time() - start_time
|
execution_time = time.time() - start_time
|
||||||
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}
|
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}
|
||||||
return output_json
|
return output_json
|
||||||
else:
|
else:
|
||||||
return build_svg_output(code)
|
return build_svg_output(code, "app_iframe")
|
||||||
|
Loading…
Reference in New Issue
Block a user