Skip to content

Commit

Permalink
IdeNode Implemented add output variables and types
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekPet committed Aug 28, 2024
1 parent b777679 commit 8f6dd0c
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 37 deletions.
4 changes: 4 additions & 0 deletions IDENode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

**IDE Node** is an experimental node that allows you to run code written in **Python** or **Javascript** directly in the node, it is also possible to receive input data, which is stored in variables, process and output the result.

# Changelog:

- 2024.08.28: Implemented add output variables and types

# Features:

- Selecting a syntax highlighting theme
Expand Down
58 changes: 46 additions & 12 deletions IDENode/ide_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from aiohttp import web
from asyncio import sleep, run
import json
import re

remove_type_name = re.compile(r"(\{.*\})", re.I | re.M)

# Hack: string type that is always equal in not equal comparisons, thanks pythongosssss
class AnyType(str):
Expand Down Expand Up @@ -49,6 +51,24 @@ async def wait_js_complete(unique_id, time_out=40):
return False


# - Thank you very much for the class -> Trung0246 -
# - https://github.com/Trung0246/ComfyUI-0246/blob/main/utils.py#L51
class TautologyStr(str):
def __ne__(self, other):
return False


class ByPassTypeTuple(tuple):
def __getitem__(self, index):
if index > 0:
index = 0
item = super().__getitem__(index)
if isinstance(item, str):
return TautologyStr(item)
return item
# ---------------------------


class IDENode:
def __init__(self):
self.js_complete = False
Expand Down Expand Up @@ -78,43 +98,57 @@ def runCode():
},
),
},
"hidden": {"unique_id": "UNIQUE_ID"},
"hidden": {"unique_id": "UNIQUE_ID", "extra_pnginfo": "EXTRA_PNGINFO"},
}

RETURN_TYPES = (PY_CODE,)
RETURN_NAMES = ("any",)
RETURN_TYPES = ByPassTypeTuple((PY_CODE,))
RETURN_NAMES = ("result{ANY}",)
FUNCTION = "exec_py"

CATEGORY = "AlekPet Nodes/experiments"

def exec_py(self, pycode, language, unique_id, **kwargs):
def exec_py(self, pycode, language, unique_id, extra_pnginfo, **kwargs):
if unique_id not in IDEs_DICT:
IDEs_DICT[unique_id] = self

if language == "python":
my_namespace = types.SimpleNamespace()
my_namespace.__dict__.update(kwargs)
my_namespace.__dict__.setdefault("result", "The result variable is not assigned")

outputs = {}

for node in extra_pnginfo['workflow']['nodes']:
if node['id'] == int(unique_id):
outputs_valid = [ouput for ouput in node.get('outputs', []) if ouput.get('name','') != '' and ouput.get('type','') != '']
outputs = {re.sub(remove_type_name, "", ouput['name']): None for ouput in outputs_valid}
self.RETURN_TYPES = ByPassTypeTuple(out["type"] for out in outputs_valid)
self.RETURN_NAMES = tuple(name for name in outputs.keys())

my_namespace = types.SimpleNamespace()
my_namespace.__dict__.update(outputs)
my_namespace.__dict__.update({re.sub(remove_type_name, "", prop): kwargs[prop] for prop in kwargs})
my_namespace.__dict__.setdefault("result", "The result variable is not assigned")

if language == "python":
try:
exec(pycode, my_namespace.__dict__)
except Exception as e:
my_namespace.result = f"Error in python code: {e}"

return (my_namespace.result,)
new_dict = {key: my_namespace.__dict__[key] for key in my_namespace.__dict__ if key not in ['__builtins__', *kwargs.keys()] and not callable(my_namespace.__dict__[key])}

return (*new_dict.values(),)

else:
IDEs_DICT[unique_id].js_complete = False
IDEs_DICT[unique_id].js_result = None

new_dict = {key: my_namespace.__dict__[key] for key in my_namespace.__dict__ if key not in ['__builtins__', *kwargs.keys()] and not callable(my_namespace.__dict__[key])}

PromptServer.instance.send_sync(
"alekpet_js_result",
{"unique_id": unique_id, "vars": json.dumps(kwargs)},
{"unique_id": unique_id, "vars": json.dumps(new_dict)},
)
if not run(wait_js_complete(unique_id)):
print(f"IDENode_{unique_id}: Failed to get data!")
else:
print(f"IDENode_{unique_id}: Data received successful!")

print(IDEs_DICT[unique_id].js_result)
return (IDEs_DICT[unique_id].js_result,)
return (*IDEs_DICT[unique_id].js_result,)
Binary file modified IDENode/ide_node_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8f6dd0c

Please sign in to comment.