From d7883af040388e2d5f7bead9f97a370db5cb843f Mon Sep 17 00:00:00 2001 From: Karl Date: Tue, 24 Jun 2025 16:44:02 +0100 Subject: [PATCH] working settings page logic for all cfg items --- libs/comfyui.py | 9 ++-- libs/generic.py | 11 ++-- routes/create_routes.py | 6 +-- routes/settings_routes.py | 102 +++++++++++++++++++++++++++++--------- templates/index.html | 16 +++++- templates/settings.html | 27 +++++++++- 6 files changed, 131 insertions(+), 40 deletions(-) diff --git a/libs/comfyui.py b/libs/comfyui.py index 5bf6731..4438959 100644 --- a/libs/comfyui.py +++ b/libs/comfyui.py @@ -32,12 +32,9 @@ def get_available_models() -> list: response = requests.get(url) if response.status_code == 200: data = response.json() - return ( - data.get("CheckpointLoaderSimple", {}) - .get("input", {}) - .get("required", {}) - .get("ckpt_name", [])[0] - ) + general = data.get("CheckpointLoaderSimple", {}).get("input", {}).get("required", {}).get("ckpt_name", [])[0] + flux = data.get("UnetLoaderGGUF", {}).get("input", {}).get("required", {}).get("unet_name", [])[0] + return general + flux else: print(f"Failed to fetch models: {response.status_code}") return [] diff --git a/libs/generic.py b/libs/generic.py index 6e2a678..fed2e33 100644 --- a/libs/generic.py +++ b/libs/generic.py @@ -102,12 +102,15 @@ def get_current_version(): def load_models_from_config(): flux_models = load_config()["comfyui:flux"]["models"].split(",") sdxl_models = load_config()["comfyui"]["models"].split(",") - all_models = flux_models + sdxl_models - return all_models + sorted_flux_models = sorted(flux_models, key=str.lower) + sorted_sdxl_models = sorted(sdxl_models, key=str.lower) + return sorted_sdxl_models, sorted_flux_models + def load_topics_from_config(): - topics = load_config()["comfyui"]["topics"].split(", ") - return topics + topics = load_config()["comfyui"]["topics"].split(",") + sorted_topics = sorted(topics, key=str.lower) + return sorted_topics user_config = load_config() output_folder = user_config["comfyui"]["output_dir"] \ No newline at end of file diff --git a/routes/create_routes.py b/routes/create_routes.py index a2d813a..b92476d 100644 --- a/routes/create_routes.py +++ b/routes/create_routes.py @@ -1,6 +1,6 @@ from flask import Blueprint, request, render_template, redirect, url_for, session import threading -from libs.comfyui import create_image, select_model +from libs.comfyui import create_image, select_model, get_available_models from libs.ollama import create_prompt_on_openwebui from libs.generic import load_models_from_config, load_topics_from_config import os @@ -21,7 +21,7 @@ def create(): threading.Thread(target=lambda: create_image(prompt, model)).start() return redirect(url_for("create_routes.image_queued", prompt=prompt, model=model.split(".")[0])) - return render_template("create_image.html", models=load_models_from_config(), topics=load_topics_from_config()) + return render_template("create_image.html", models=load_models_from_config()[0]+load_models_from_config()[1], topics=load_topics_from_config()) @bp.route("/image_queued") def image_queued(): @@ -33,7 +33,7 @@ def image_queued(): def create_image_page(): if user_config["frame"]["create_requires_auth"] == "True" and not session.get("authenticated"): return redirect(url_for("auth_routes.login", next=request.path)) - return render_template("create_image.html", models=load_models_from_config(), topics=load_topics_from_config()) + return render_template("create_image.html", models=load_models_from_config()[0]+load_models_from_config()[1], topics=load_topics_from_config()) def init_app(config): diff --git a/routes/settings_routes.py b/routes/settings_routes.py index d95a4da..efa7a7d 100644 --- a/routes/settings_routes.py +++ b/routes/settings_routes.py @@ -1,40 +1,94 @@ from flask import Blueprint, render_template, request, redirect, url_for, session import configparser -from libs.generic import load_topics_from_config +from libs.generic import load_topics_from_config, load_models_from_config bp = Blueprint('settings_route', __name__) CONFIG_PATH = "./user_config.cfg" -def save_items(items): - config = configparser.ConfigParser() - config.read(CONFIG_PATH) - - # Make sure the section exists - if not config.has_section('comfyui'): - config.add_section('comfyui') - - # Save updated list to the 'topics' key - config.set('comfyui', 'topics', ', '.join(items)) - - with open(CONFIG_PATH, 'w') as configfile: - config.write(configfile) - @bp.route('/settings', methods=['GET', 'POST']) def config_editor(): if not session.get("authenticated"): return redirect(url_for("auth_routes.login", next=request.path)) - items = load_topics_from_config() # should return list[str] + + config = configparser.ConfigParser() + config.read(CONFIG_PATH) + + # Load from config directly — no helper functions needed anymore + topics = config.get('comfyui', 'topics', fallback='').split(',') + general_models = config.get('comfyui', 'models', fallback='').split(',') + flux_models = config.get('comfyui:flux', 'models', fallback='').split(',') + + topics = [t.strip() for t in topics if t.strip()] + general_models = [m.strip() for m in general_models if m.strip()] + flux_models = [m.strip() for m in flux_models if m.strip()] if request.method == 'POST': if 'new_topic' in request.form: - new_item = request.form.get('new_topic', '').strip() - if new_item and new_item not in items: - items.append(new_item) - elif 'delete_topic' in request.form: - to_delete = request.form.getlist('delete_topic') - items = [item for item in items if item not in to_delete] + new_topic = request.form.get('new_topic', '').strip() + if new_topic and new_topic not in topics: + topics.append(new_topic) + + if 'delete_topic' in request.form: + to_delete = request.form.getlist('delete_topic') + topics = [topic for topic in topics if topic not in to_delete] + + if 'new_model' in request.form: + new_model = request.form.get('new_model', '').strip() + if new_model: + if 'flux' in new_model and new_model not in flux_models: + flux_models.append(new_model) + elif 'flux' not in new_model and new_model not in general_models: + general_models.append(new_model) + + if 'delete_model' in request.form: + to_delete = request.form.getlist('delete_model') + general_models = [m for m in general_models if m not in to_delete] + flux_models = [m for m in flux_models if m not in to_delete] + + # Save models/topics into the shared config object + if not config.has_section('comfyui'): + config.add_section('comfyui') + if not config.has_section('comfyui:flux'): + config.add_section('comfyui:flux') + + config.set('comfyui', 'models', ','.join(general_models)) + config.set('comfyui:flux', 'models', ','.join(flux_models)) + config.set('comfyui', 'topics', ','.join(topics)) + + # Handle dynamic CFG field updates (excluding DEFAULT and protected keys) + for section in config.sections(): + for key in config[section]: + if key == 'models' and section in ('comfyui', 'comfyui:flux'): + continue + if key == 'topics' and section == 'comfyui': + continue + form_key = f"{section}:{key}" + if form_key in request.form: + config[section][key] = request.form[form_key] + + # Save everything at once + with open(CONFIG_PATH, 'w') as configfile: + config.write(configfile) - save_items(items) return redirect(url_for('settings_route.config_editor')) - return render_template('settings.html', topics=items) + # Prepare filtered config for display + filtered_config = {} + for section in config.sections(): + items = { + k: v for k, v in config[section].items() + if not ( + (k == 'models' and section in ('comfyui', 'comfyui:flux')) or + (k == 'topics' and section == 'comfyui') + ) + } + if items: # only include non-empty sections + filtered_config[section] = items + + return render_template( + 'settings.html', + topics=topics, + models=general_models + flux_models, + config_sections=filtered_config.keys(), + config_values=filtered_config + ) diff --git a/templates/index.html b/templates/index.html index c23b42e..29c7a2a 100644 --- a/templates/index.html +++ b/templates/index.html @@ -92,10 +92,20 @@ font-size: 12px; font-family: monospace; user-select: none; - pointer-events: none; opacity: 0.6; } + .version a { + color: inherit; + text-decoration: none; + cursor: pointer; + } + + .version a:hover { + text-decoration: underline; + } + + @media (max-width: 768px) { .image-container { max-width: 100vw; @@ -148,7 +158,9 @@ {% endif %} -
v{{ version }}
+
+ v{{ version }} +
\ No newline at end of file diff --git a/templates/settings.html b/templates/settings.html index 2fb4ffc..591b582 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -147,6 +147,31 @@ +
+

Config Values

+
+ {% for section in config_sections %} +
+

[{{ section }}]

+ {% for key, value in config_values[section].items() %} + + {% if value.lower() in ['true', 'false'] %} + + {% else %} + + {% endif %} + + {% endfor %} +
+ {% endfor %} +
+ +
+
+
Back to Home @@ -154,4 +179,4 @@ - + \ No newline at end of file