Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serve workflow templates from custom_nodes #6193

Merged
9 changes: 5 additions & 4 deletions folder_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@

folder_names_and_paths["classifiers"] = ([os.path.join(models_dir, "classifiers")], {""})

output_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), "output")
temp_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), "temp")
input_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), "input")
user_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), "user")
output_directory = os.path.join(base_path, "output")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I don't think these changes are related to this PR although they seem to be good.

Let's revert them for now so that we are sure that this PR does not break anything related.

temp_directory = os.path.join(base_path, "temp")
input_directory = os.path.join(base_path, "input")
user_directory = os.path.join(base_path, "user")
custom_nodes_directory = os.path.join(base_path, "custom_nodes")

filename_list_cache: dict[str, tuple[list[str], dict[str, float], float]] = {}

Expand Down
5 changes: 5 additions & 0 deletions nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2033,6 +2033,9 @@ def expand_image(self, image, left, top, right, bottom, feathering):

EXTENSION_WEB_DIRS = {}

# Dictionary of successfully loaded module names and associated directories.
LOADED_MODULE_DIRS = {}


def get_module_name(module_path: str) -> str:
"""
Expand Down Expand Up @@ -2074,6 +2077,8 @@ def load_custom_node(module_path: str, ignore=set(), module_parent="custom_nodes
sys.modules[module_name] = module
module_spec.loader.exec_module(module)

LOADED_MODULE_DIRS[module_name] = os.path.abspath(module_dir)

if hasattr(module, "WEB_DIRECTORY") and getattr(module, "WEB_DIRECTORY") is not None:
web_dir = os.path.abspath(os.path.join(module_dir, getattr(module, "WEB_DIRECTORY")))
if os.path.isdir(web_dir):
Expand Down
17 changes: 17 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ async def get_extensions(request):
name) + "/" + os.path.relpath(f, dir).replace("\\", "/"), files)))

return web.json_response(extensions)

@routes.get("/workflow_templates")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have a plan to eventually move ComfyUI-Manager's custom node management features to core. Can we move this endpoint to a separate file app/custom_node_manager.py?

async def get_workflow_templates(request):
files = glob.glob(os.path.join(folder_paths.custom_nodes_directory, '*/example_workflows/*.json'))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just call folder_paths.get_folder_paths("custom_nodes") here instead of adding a new global variable in folder_paths.

workflow_templates_dict = {} # custom_nodes folder name -> example workflow names
for file in files:
custom_nodes_name = os.path.basename(os.path.dirname(os.path.dirname(file)))
workflow_name = os.path.splitext(os.path.basename(file))[0]
workflow_templates_dict.setdefault(custom_nodes_name, []).append(workflow_name)
return web.json_response(workflow_templates_dict)

def get_dir_by_type(dir_type):
if dir_type is None:
Expand Down Expand Up @@ -713,6 +723,13 @@ def add_routes(self):
self.app.add_routes(api_routes)
self.app.add_routes(self.routes)

# Add routes for workflow templates in custom nodes.
for module_name, module_dir in nodes.LOADED_MODULE_DIRS.items():
workflows_dir = os.path.join(module_dir, 'example_workflows')
if os.path.exists(workflows_dir):
self.app.add_routes([web.static('/workflow_templates/' + module_name, workflows_dir)])

# Add routes from web extensions.
for name, dir in nodes.EXTENSION_WEB_DIRS.items():
self.app.add_routes([web.static('/extensions/' + name, dir)])

Expand Down
Loading