diff --git a/README.md b/README.md index 597b53d..3ec179c 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,23 @@ will simply update the comfy.yaml file to reflect the local setup * `comfy install --skip-manager`: Install ComfyUI without ComfyUI-Manager. * `comfy install --workspace=`: Install ComfyUI into `/ComfyUI`. + +### Launch ComfyUI + +Comfy provides commands that allow you to easily run the installed ComfyUI. + +- To execute specifying the path of the workspace where ComfyUI is installed: + + `comfy launch --workspace ` + +- To run ComfyUI from the current directory, if you are inside the ComfyUI repository: + + `comfy launch` + +- To execute the ComfyUI that was last run or last installed, if you are outside of the ComfyUI repository: + + `comfy launch` + ### Managing Packages comfy allows you to easily install, update, and remove packages for ComfyUI. Here are some examples: diff --git a/comfy_cli/cmdline.py b/comfy_cli/cmdline.py index d43ae2a..06dfb5f 100644 --- a/comfy_cli/cmdline.py +++ b/comfy_cli/cmdline.py @@ -1,3 +1,5 @@ +import sys + import typer from typing_extensions import Annotated from comfy_cli.command.models import models @@ -74,7 +76,6 @@ def install( install_inner.execute(url, manager_url, workspace, skip_manager) - def update(self): print(f"Updating ComfyUI in {self.workspace}...") os.chdir(self.workspace) @@ -84,21 +85,50 @@ def update(self): @app.command(help="Run workflow file") def run( - workflow_file: Annotated[str, typer.Option(help="Path to the workflow file.")], - ): + workflow_file: Annotated[str, typer.Option(help="Path to the workflow file.")], + ): run_inner.execute(workflow_file) -@app.command(help="Launch ComfyUI") -def launch(): - os.chdir(self.workspace) - run_inner.execute(workflow_file) +def launch_comfyui(_env_checker): + _env_checker.config['DEFAULT']['recent_path'] = os.getcwd() + _env_checker.write_config() + subprocess.run(["python", "main.py"]) + + +@app.command(help="Launch ComfyUI: ?[--workspace ]") +def launch(workspace: Annotated[ + str, + typer.Option( + show_default=False, + help="Path to ComfyUI workspace") + ] = None): + _env_checker = EnvChecker() + if workspace is not None: + comfyui_path = os.path.join(workspace, 'ComfyUI') + if os.path.exists(comfyui_path): + os.chdir(comfyui_path) + print(f"\nLaunch ComfyUI from repo: {_env_checker.comfy_repo.working_dir}\n") + launch_comfyui(_env_checker) + else: + print(f"\nInvalid ComfyUI not found in specified workspace: {workspace}\n", file=sys.stderr) + + elif _env_checker.comfy_repo is not None: + print(f"\nLaunch ComfyUI from current repo: {_env_checker.comfy_repo.working_dir}\n") + launch_comfyui(_env_checker) + elif _env_checker.config['DEFAULT'].get('recent_path') is not None: + comfy_path = _env_checker.config['DEFAULT'].get('recent_path') + print(f"\nLaunch ComfyUI from recent repo: {comfy_path}\n") + os.chdir(comfy_path) + launch_comfyui(_env_checker) + else: + print(f"\nComfyUI is not available.\n", file=sys.stderr) @app.command(help="Print out current environment variables.") def env(): - env_checker = EnvChecker() - env_checker.print() + _env_checker = EnvChecker() + _env_checker.print() app.add_typer(models.app, name="models", help="Manage models.") diff --git a/comfy_cli/command/install.py b/comfy_cli/command/install.py index e489847..879d5af 100644 --- a/comfy_cli/command/install.py +++ b/comfy_cli/command/install.py @@ -13,6 +13,7 @@ def execute(url: str, manager_url: str, comfy_workspace: str, skip_manager: bool # install ComfyUI if checker.currently_in_comfy_repo: print(f"Already in comfy repo. Skipping installation.") + repo_dir = os.getcwd() else: working_dir = os.path.expanduser(comfy_workspace) repo_dir = os.path.join(working_dir, os.path.basename(url).replace(".git", "")) @@ -46,3 +47,7 @@ def execute(url: str, manager_url: str, comfy_workspace: str, skip_manager: bool subprocess.run(["pip", "install", "-r", "requirements.txt"]) os.chdir(os.path.join('..', '..')) + checker.config['DEFAULT']['recent_path'] = repo_dir + checker.write_config() + + diff --git a/comfy_cli/constants.py b/comfy_cli/constants.py index 45252d4..7a08e3c 100644 --- a/comfy_cli/constants.py +++ b/comfy_cli/constants.py @@ -5,10 +5,12 @@ # TODO: figure out a better way to check if this is a comfy repo COMFY_ORIGIN_URL_CHOICES = [ - "git@github.com:comfyanonymous/ComfyUI.git" + "git@github.com:comfyanonymous/ComfyUI.git", "git@github.com:drip-art/comfy.git", "https://github.com/comfyanonymous/ComfyUI.git", "https://github.com/drip-art/ComfyUI.git", + "https://github.com/comfyanonymous/ComfyUI", + "https://github.com/drip-art/ComfyUI", ] # Referencing supported pt extension from ComfyUI diff --git a/comfy_cli/env_checker.py b/comfy_cli/env_checker.py index a26da2b..86cb741 100644 --- a/comfy_cli/env_checker.py +++ b/comfy_cli/env_checker.py @@ -12,6 +12,7 @@ from comfy_cli import constants from comfy_cli.utils import singleton +import configparser console = Console() @@ -74,8 +75,36 @@ def __init__(self): self.python_version: None = None self.currently_in_comfy_repo = False self.comfy_repo = None + self.config = configparser.ConfigParser() self.check() + def is_isolated_env(self): + return self.virtualenv_path or self.conda_env + + def get_isolated_env(self): + if self.virtualenv_path: + return self.virtualenv_path + + if self.conda_env: + return self.conda_env + + return None + + def get_config_path(self): + env_path = self.get_isolated_env() + if env_path: + return os.path.join(env_path, 'comfy-cli', 'config.json') + return None + + def write_config(self): + env_path = self.get_isolated_env() + cli_path = os.path.join(env_path, 'comfy-cli') + if not os.path.exists(cli_path): + os.mkdir(cli_path) + + with open(self.get_config_path(), 'w') as configfile: + self.config.write(configfile) + def check(self): self.virtualenv_path = ( os.environ.get("VIRTUAL_ENV") @@ -99,12 +128,17 @@ def check(self): except git.exc.InvalidGitRepositoryError: self.currently_in_comfy_repo = False + config_path = self.get_config_path() + if os.path.exists(config_path): + self.config = configparser.ConfigParser() + self.config.read(config_path) def print(self): table = Table(":laptop_computer: Environment", "Value") table.add_row("Python Version", format_python_version(sys.version_info)) table.add_row("Virtualenv Path", self.virtualenv_path) table.add_row("Conda Env", self.conda_env) + table.add_row("Recent ComfyUI", self.config['DEFAULT']['recent_path']) if check_comfy_server_running(): table.add_row("Comfy Server Running", "[bold green]Yes[/bold green]\nhttp://localhost:8188") else: