2024-02-05 23:57:00 +00:00
"""
project : lollms_webui
file : shell_execution_engine . py
author : ParisNeo
description :
This is a utility for executing python code
"""
from lollms_webui import LOLLMSWebUI
from ascii_colors import get_trace_exception , trace_exception
import time
import subprocess
import json
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-02-05 23:57:00 +00:00
lollmsElfServer : LOLLMSWebUI = LOLLMSWebUI . get_instance ( )
def build_html_output ( code , ifram_name = " unnamed " ) :
"""
This function creates an HTML5 iframe with the given HTML content and iframe name .
Args :
html ( str ) : The HTML content to be displayed in the iframe .
ifram_name ( str , optional ) : The name of the iframe . Defaults to " unnamed " .
Returns :
str : The HTML string for the iframe .
"""
# Start the timer.
start_time = time . time ( )
rendered = " \n " . join ( [
' <div style= " width: 100 % ; margin: 0 auto; " > ' ,
f ' <iframe id= " { ifram_name } " srcdoc= \' ' ,
code . replace ( " ' " , " \" " ) ,
' \' style= " width: 100 % ; height: 600px; border: none; " ></iframe> ' ,
' </div> '
]
)
execution_time = time . time ( ) - start_time
return { " output " : rendered , " execution_time " : execution_time }
2024-04-21 00:50:43 +00:00
def execute_html ( code , client : Client , message_id , build_file = False ) :
if build_file :
# Start the timer.
start_time = time . time ( )
if not " http " in lollmsElfServer . config . host and not " https " in lollmsElfServer . config . host :
host = " http:// " + lollmsElfServer . config . host
else :
host = lollmsElfServer . config . host
# Create a temporary file.
root_folder = client . discussion . discussion_folder
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 :
f . write ( code )
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
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
else :
return build_html_output ( code )