Author SHA1 Message Date
David Fischer 6d827d968a reverting old changes 2025-05-16 11:08:08 +02:00
David Vincent Fischer e346fd764c Merge pull request #18 from theUpsider/csv-header-columns
csv loader error handeling
2025-05-15 16:12:47 +02:00
+23 -61
View File
@@ -2,74 +2,39 @@ import os
import re
import folder_paths
class StylesCSVLoader:
"""
Loads csv file with styles. For migration purposes from automatic1111 webui.
Loads csv file with styles. For migration purposes from automatic11111 webui.
"""
@staticmethod
def load_styles_csv(styles_path: str):
"""Loads csv file with styles. Each row has three columns:
- style_name: string (labeled as "name" in the CSV)
- positive_prompt: string (labeled as "prompt" in the CSV)
- negative_prompt: string (may be empty or missing)
Returns:
dict: Dictionary of styles, where each key is style_name and value is [positive_prompt, negative_prompt]
"""
error_styles = {
"Error loading styles.csv, check the console": ["", ""]}
"""Loads csv file with styles. It has only one column.
Ignore the first row (header).
positive_prompt are strings separated by comma. Each string is a prompt.
negative_prompt are strings separated by comma. Each string is a prompt.
Returns:
list: List of styles. Each style is a dict with keys: style_name and value: [positive_prompt, negative_prompt]
"""
styles = {"Error loading styles.csv, check the console": ["", ""]}
if not os.path.exists(styles_path):
print(f"""Error. No styles.csv found. Put your styles.csv in the root directory of ComfyUI. Then press "Refresh".
Your current root directory is: {folder_paths.base_path}
""")
return error_styles
try:
styles = {}
with open(styles_path, "r", encoding="utf-8") as f:
reader = csv.reader(f)
next(reader) # Skip header row
# Start counting from 2 (after header)
for i, row in enumerate(reader, start=2):
try:
# Initialize variables to avoid "used before assigned" errors
style_name = ""
positive_prompt = ""
negative_prompt = ""
# Handle rows with fewer than 3 columns
if len(row) == 0:
# Skip empty rows
continue
elif len(row) == 1:
print(
f"Error parsing line {i}: {','.join(row)} -> Expected at least 2 columns, got {len(row)}")
continue
elif len(row) == 2:
# Missing negative prompt - treat as empty string
style_name, positive_prompt = row
# negative_prompt already initialized as empty string
elif len(row) >= 3:
# Normal case or extra columns (ignore extras)
style_name, positive_prompt, negative_prompt = row[0], row[1], row[2]
styles[style_name] = [positive_prompt, negative_prompt]
except Exception as e:
# Use a safe version of the row for error reporting
row_str = ','.join(row) if isinstance(
row, list) else str(row)
print(f"Error parsing line {i}: {row_str} -> {e}")
Your current root directory is: {folder_paths.base_path}
""")
return styles
try:
with open(styles_path, "r", encoding="utf-8") as f:
styles = [[x.replace('"', '').replace('\n', '') for x in re.split(
',(?=(?:[^"]*"[^"]*")*[^"]*$)', line)] for line in f.readlines()[1:]]
styles = {x[0]: [x[1], x[2]] for x in styles}
except Exception as e:
print(f"""Error loading styles.csv. Make sure it is in the root directory of ComfyUI. Then press "Refresh".
Your current root directory is: {folder_paths.base_path}
Error: {e}
""")
return error_styles
Your current root directory is: {folder_paths.base_path}
Error: {e}
""")
return styles
@classmethod
def INPUT_TYPES(cls):
@@ -79,6 +44,7 @@ Error: {e}
"required": {
"styles": (list(cls.styles_csv.keys()),),
},
}
RETURN_TYPES = ("STRING", "STRING")
@@ -87,16 +53,12 @@ Error: {e}
CATEGORY = "loaders"
def execute(self, styles):
return (
self.styles_csv[styles][0],
self.styles_csv[styles][1]
)
return (self.styles_csv[styles][0], self.styles_csv[styles][1])
NODE_CLASS_MAPPINGS = {
"Load Styles CSV": StylesCSVLoader
}
NODE_DISPLAY_NAME_MAPPINGS = {
"StylesCSVLoader": "Load Styles CSV Node"
}