Skip to content

Commit

Permalink
feat: comfy launch
Browse files Browse the repository at this point in the history
  • Loading branch information
ltdrdata committed Apr 22, 2024
1 parent 16a6263 commit aed1d86
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 10 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<path>`: Install ComfyUI into `<path>/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 <path>`

- 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:
Expand Down
48 changes: 39 additions & 9 deletions comfy_cli/cmdline.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

import typer
from typing_extensions import Annotated
from comfy_cli.command.models import models
Expand Down Expand Up @@ -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)
Expand All @@ -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 <path>]")
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.")
Expand Down
5 changes: 5 additions & 0 deletions comfy_cli/command/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""))
Expand Down Expand Up @@ -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()


4 changes: 3 additions & 1 deletion comfy_cli/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

# TODO: figure out a better way to check if this is a comfy repo
COMFY_ORIGIN_URL_CHOICES = [
"[email protected]:comfyanonymous/ComfyUI.git"
"[email protected]:comfyanonymous/ComfyUI.git",
"[email protected]: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
Expand Down
34 changes: 34 additions & 0 deletions comfy_cli/env_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from comfy_cli import constants
from comfy_cli.utils import singleton
import configparser

console = Console()

Expand Down Expand Up @@ -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")
Expand All @@ -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:
Expand Down

0 comments on commit aed1d86

Please sign in to comment.