The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.
Update server templates handler to use new multi-package distribution (comfyui-workflow-templates versions >=0.3) (#10791)
Christian Byrne87b035939221
diff --git a/app/frontend_management.py b/app/frontend_management.pyindex cce0c117d..bdaa85812 100644--- a/app/frontend_management.py+++ b/app/frontend_management.py@@ -10,7 +10,8 @@ import importlibfrom dataclasses import dataclassfrom functools import cached_propertyfrom pathlib import Path-from typing import TypedDict, Optional+from typing import Dict, TypedDict, Optional+from aiohttp import webfrom importlib.metadata import versionimport requests@@ -257,7 +258,54 @@ comfyui-frontend-package is not installed.sys.exit(-1)@classmethod- def templates_path(cls) -> str:+ def template_asset_map(cls) -> Optional[Dict[str, str]]:+ """Return a mapping of template asset names to their absolute paths."""+ try:+ from comfyui_workflow_templates import (+ get_asset_path,+ iter_templates,+ )+ except ImportError:+ logging.error(+ f"""+********** ERROR ***********++comfyui-workflow-templates is not installed.++{frontend_install_warning_message()}++********** ERROR ***********+""".strip()+ )+ return None++ try:+ template_entries = list(iter_templates())+ except Exception as exc:+ logging.error(f"Failed to enumerate workflow templates: {exc}")+ return None++ asset_map: Dict[str, str] = {}+ try:+ for entry in template_entries:+ for asset in entry.assets:+ asset_map[asset.filename] = get_asset_path(+ entry.template_id, asset.filename+ )+ except Exception as exc:+ logging.error(f"Failed to resolve template asset paths: {exc}")+ return None++ if not asset_map:+ logging.error("No workflow template assets found. Did the packages install correctly?")+ return None++ return asset_map+++ @classmethod+ def legacy_templates_path(cls) -> Optional[str]:+ """Return the legacy templates directory shipped inside the meta package."""try:import comfyui_workflow_templates@@ -276,6 +324,7 @@ comfyui-workflow-templates is not installed.********** ERROR ***********""".strip())+ return None@classmethoddef embedded_docs_path(cls) -> str:@@ -392,3 +441,17 @@ comfyui-workflow-templates is not installed.logging.info("Falling back to the default frontend.")check_frontend_version()return cls.default_frontend_path()+ @classmethod+ def template_asset_handler(cls):+ assets = cls.template_asset_map()+ if not assets:+ return None++ async def serve_template(request: web.Request) -> web.StreamResponse:+ rel_path = request.match_info.get("path", "")+ target = assets.get(rel_path)+ if target is None:+ raise web.HTTPNotFound()+ return web.FileResponse(target)++ return serve_templatediff --git a/requirements.txt b/requirements.txtindex 249c36dee..36c39f338 100644--- a/requirements.txt+++ b/requirements.txt@@ -1,5 +1,5 @@comfyui-frontend-package==1.28.8-comfyui-workflow-templates==0.2.11+comfyui-workflow-templates==0.3.1comfyui-embedded-docs==0.3.1torchtorchsdediff --git a/server.py b/server.pyindex d059d3dc9..d9d5c491f 100644--- a/server.py+++ b/server.py@@ -30,7 +30,7 @@ import comfy.model_managementfrom comfy_api import feature_flagsimport node_helpersfrom comfyui_version import __version__-from app.frontend_management import FrontendManager+from app.frontend_management import FrontendManager, parse_versionfrom comfy_api.internal import _ComfyNodeInternalfrom app.user_manager import UserManager@@ -849,11 +849,31 @@ class PromptServer():for name, dir in nodes.EXTENSION_WEB_DIRS.items():self.app.add_routes([web.static('/extensions/' + name, dir)])- workflow_templates_path = FrontendManager.templates_path()- if workflow_templates_path:- self.app.add_routes([- web.static('/templates', workflow_templates_path)- ])+ installed_templates_version = FrontendManager.get_installed_templates_version()+ use_legacy_templates = True+ if installed_templates_version:+ try:+ use_legacy_templates = (+ parse_version(installed_templates_version)+ < parse_version("0.3.0")+ )+ except Exception as exc:+ logging.warning(+ "Unable to parse templates version '%s': %s",+ installed_templates_version,+ exc,+ )++ if use_legacy_templates:+ workflow_templates_path = FrontendManager.legacy_templates_path()+ if workflow_templates_path:+ self.app.add_routes([+ web.static('/templates', workflow_templates_path)+ ])+ else:+ handler = FrontendManager.template_asset_handler()+ if handler:+ self.app.router.add_get("/templates/{path:.*}", handler)# Serve embedded documentation from the packageembedded_docs_path = FrontendManager.embedded_docs_path()