Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fzumstein committed Feb 11, 2025
1 parent c4335d0 commit 49db135
Showing 1 changed file with 25 additions and 42 deletions.
67 changes: 25 additions & 42 deletions app/lite/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ def __init__(self, div_id):
self.buffer = StringIO()

def write(self, text):
# Write to buffer and update div content
self.buffer.write(text)
content = self.buffer.getvalue()
js.document.getElementById(self.div_id).innerHTML = f"<pre>{content}</pre>"
div = js.document.getElementById(self.div_id)
if div:
js.document.getElementById(self.div_id).innerHTML = f"<pre>{content}</pre>"

def flush(self):
pass
Expand All @@ -61,17 +62,28 @@ def flush(self):
)


def create_module_from_string(module_string, module_name, html_output):
spec = importlib.util.spec_from_loader(
module_name,
loader=None,
)
module = importlib.util.module_from_spec(spec)
if html_output:
with (
contextlib.redirect_stdout(html_output),
contextlib.redirect_stderr(html_output),
):
exec(module_string, module.__dict__)
else:
exec(module_string, module.__dict__)
sys.modules[module_name] = module
return module


async def custom_functions_call(data, module_string=None):
module_name = "main" # TODO
if module_string:
html_output = HtmlOutput("output")
spec = importlib.util.spec_from_loader(
module_name,
loader=None,
)
module = importlib.util.module_from_spec(spec)
exec(module_string, module.__dict__)
sys.modules[module_name] = module
module = create_module_from_string(module_string, "main")
else:
module = custom_functions

Expand All @@ -98,23 +110,9 @@ async def custom_functions_call(data, module_string=None):


async def custom_scripts_call(data, script_name, module_string=None):
module_name = "main" # TODO
if module_string:
html_output = HtmlOutput("output")
spec = importlib.util.spec_from_loader(
module_name,
loader=None,
)
module = importlib.util.module_from_spec(spec)
if module_string:
with (
contextlib.redirect_stdout(html_output),
contextlib.redirect_stderr(html_output),
):
exec(module_string, module.__dict__)
else:
exec(module_string, module.__dict__)
sys.modules[module_name] = module
module = create_module_from_string(module_string, "main", html_output)
else:
module = custom_scripts

Expand Down Expand Up @@ -162,14 +160,7 @@ def get_xlwings_scripts(code_string):

def custom_functions_meta(module_string):
try:
module_name = "main"
spec = importlib.util.spec_from_loader(
module_name,
loader=None,
)
module = importlib.util.module_from_spec(spec)
exec(module_string, module.__dict__)
sys.modules[module_name] = module
module = create_module_from_string(module_string, "main")
except: # noqa: E722
return to_js({}, dict_converter=js.Object.fromEntries)
return to_js(
Expand All @@ -178,16 +169,8 @@ def custom_functions_meta(module_string):


def custom_functions_code(module_string):
# TODO: factor out
try:
module_name = "main"
spec = importlib.util.spec_from_loader(
module_name,
loader=None,
)
module = importlib.util.module_from_spec(spec)
exec(module_string, module.__dict__)
sys.modules[module_name] = module
module = create_module_from_string(module_string, "main")
except: # noqa: E722
return ""
js = ""
Expand Down

0 comments on commit 49db135

Please sign in to comment.