Skip to content

Commit d7b7b84

Browse files
committed
Merge branch 'feat/callable_script' into dev
2 parents 26f224f + 334a4c4 commit d7b7b84

File tree

1 file changed

+60
-11
lines changed

1 file changed

+60
-11
lines changed

comfyui_to_python.py

+60-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import random
77
import sys
88
import re
9-
from typing import Dict, List, Any, Callable, Tuple
9+
from typing import Dict, List, Callable, Tuple
10+
from argparse import ArgumentParser
1011

1112
import black
1213

@@ -23,6 +24,11 @@
2324
from nodes import NODE_CLASS_MAPPINGS
2425

2526

27+
DEFAULT_INPUT_FILE = "workflow_api.json"
28+
DEFAULT_OUTPUT_FILE = "workflow_api.py"
29+
DEFAULT_QUEUE_SIZE = 1
30+
31+
2632
class FileHandler:
2733
"""Handles reading and writing files.
2834
@@ -224,7 +230,6 @@ def generate_workflow(
224230
custom_nodes = False
225231
# Loop over each dictionary in the load order list
226232
for idx, data, is_special_function in load_order:
227-
228233
# Generate class definition and inputs from the data
229234
inputs, class_type = data["inputs"], data["class_type"]
230235
class_def = self.node_class_mappings[class_type]()
@@ -552,13 +557,57 @@ def execute(self):
552557
print(f"Code successfully generated and written to {self.output_file}")
553558

554559

555-
if __name__ == "__main__":
556-
# Update class parameters here
557-
input_file = "workflow_api.json"
558-
output_file = "workflow_api.py"
559-
queue_size = 10
560-
561-
# Convert ComfyUI workflow to Python
562-
ComfyUItoPython(
563-
input_file=input_file, output_file=output_file, queue_size=queue_size
560+
def run(
561+
input_file: str = DEFAULT_INPUT_FILE,
562+
output_file: str = DEFAULT_OUTPUT_FILE,
563+
queue_size: int = DEFAULT_QUEUE_SIZE,
564+
) -> None:
565+
"""Generate Python code from a ComfyUI workflow_api.json file.
566+
567+
Args:
568+
input_file (str): Path to the input JSON file. Defaults to "workflow_api.json".
569+
output_file (str): Path to the output Python file.
570+
Defaults to "workflow_api.py".
571+
queue_size (int): The number of times a workflow will be executed by the script.
572+
Defaults to 1.
573+
574+
Returns:
575+
None
576+
"""
577+
ComfyUItoPython(input_file, output_file, queue_size)
578+
579+
580+
def main() -> None:
581+
"""Main function to generate Python code from a ComfyUI workflow_api.json file."""
582+
parser = ArgumentParser(
583+
description="Generate Python code from a ComfyUI workflow_api.json file."
584+
)
585+
parser.add_argument(
586+
"-f",
587+
"--input_file",
588+
type=str,
589+
help="path to the input JSON file",
590+
default=DEFAULT_INPUT_FILE,
591+
)
592+
parser.add_argument(
593+
"-o",
594+
"--output_file",
595+
type=str,
596+
help="path to the output Python file",
597+
default=DEFAULT_OUTPUT_FILE,
564598
)
599+
parser.add_argument(
600+
"-q",
601+
"--queue_size",
602+
type=int,
603+
help="number of times the workflow will be executed by default",
604+
default=DEFAULT_QUEUE_SIZE,
605+
)
606+
pargs = parser.parse_args()
607+
ComfyUItoPython(**vars(pargs))
608+
print("Done.")
609+
610+
611+
if __name__ == "__main__":
612+
"""Run the main function."""
613+
main()

0 commit comments

Comments
 (0)