-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomfy_gallery.py
1794 lines (1520 loc) · 76.5 KB
/
comfy_gallery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import base64
import ctypes
import hashlib
import io
import json
import os
import re
import shutil
import sys
import urllib
from datetime import datetime
from PIL import Image, PngImagePlugin
import importlib
import requests
from aiohttp import web
from tqdm import tqdm
import webbrowser
# SERVERs
DOMAIN = '127.0.0.1'
PORT = 8189
ROOT = os.path.dirname(os.path.abspath(__file__))
# FEATURES
NO_BROWSER = False
PURGE_CACHE = False
# GENERAL GLOBALS
ALLOWED_EXTENSIONS = [".png", ".jpg", ".jpeg", ".gif", ".webp"]
CP_FILE = 'web'+os.sep+'extensions'+os.sep+'core'+os.sep+'colorPalette.js'
DB_CACHED = False
IMAGE_PATHS = [
"output",
"input"
]
THUMBNAIL_DIRECTORY = os.path.join(ROOT, "temp")
TITLE = "ComfyGallery"
# FUNCTIONS
class cstr(str):
class color:
END = '\033[0m'
BOLD, ITALIC, UNDERLINE, BLINK, BLINK2, SELECTED = ['\033[%dm' % (i,) for i in range(1, 7)]
BLACK, RED, GREEN, YELLOW, BLUE, VIOLET, BEIGE, WHITE = ['\033[%dm' % (i,) for i in range(30, 38)]
BLACKBG, REDBG, GREENBG, YELLOWBG, BLUEBG, VIOLETBG, BEIGEBG, WHITEBG = ['\033[%dm' % (i,) for i in range(40, 48)]
GREY, LIGHTRED, LIGHTGREEN, LIGHTYELLOW, LIGHTBLUE, LIGHTVIOLET, LIGHTBEIGE, LIGHTWHITE = ['\033[%dm' % (i,) for i in range(90, 98)]
GREYBG, LIGHTREDBG, LIGHTGREENBG, LIGHTYELLOWBG, LIGHTBLUEBG, LIGHTVIOLETBG, LIGHTBEIGEBG, LIGHTWHITEBG = ['\033[%dm' % (i,) for i in range(100, 108)]
@staticmethod
def add_code(name, code):
if not hasattr(cstr.color, name.upper()):
setattr(cstr.color, name.upper(), code)
else:
raise ValueError(f"'cstr' object already contains a code with the name '{name}'.")
def __new__(cls, text):
return super().__new__(cls, text)
def __getattr__(self, attr):
if attr.lower().startswith("_cstr"):
code = getattr(self.color, attr.upper().lstrip("_cstr"))
modified_text = self.replace(f"__{attr[1:]}__", f"{code}")
return cstr(modified_text)
elif attr.upper() in dir(self.color):
code = getattr(self.color, attr.upper())
modified_text = f"{code}{self}{self.color.END}"
return cstr(modified_text)
elif attr.lower() in dir(cstr):
return getattr(cstr, attr.lower())
else:
raise AttributeError(f"'cstr' object has no attribute '{attr}'")
def print(self, **kwargs):
print(self, **kwargs)
#! MESSAGE TEMPLATES
cstr.color.add_code("msg", f"{cstr.color.LIGHTVIOLET}ComfyGallery{cstr.color.END}: ")
cstr.color.add_code("access", f"{cstr.color.LIGHTVIOLET}ComfyGallery{cstr.color.END} ({cstr.color.LIGHTYELLOW}Access-Log{cstr.color.END}): ")
cstr.color.add_code("warning", f"{cstr.color.LIGHTVIOLET}ComfyGallery{cstr.color.END} {cstr.color.LIGHTYELLOW}Warning: {cstr.color.END}")
cstr.color.add_code("error", f"{cstr.color.LIGHTVIOLET}ComfyGallery{cstr.color.END} {cstr.color.RED}Error: {cstr.color.END}")
def window_title(title):
ctypes.windll.kernel32.SetConsoleTitleW(title)
def get_full_path(category, path):
full_path = None
for base_path in IMAGE_PATHS:
if os.path.basename(base_path) == category:
base_folder = base_path
full_path = os.path.join(base_folder, path)
break
if not full_path:
cstr(f"Unable to determine image path from category {cstr.color.LIGHTYELLOW}{category}{cstr.color.END} and path {cstr.color.BOLD}{path}{cstr.color.END}").error.print()
return None
if not os.path.exists(full_path):
cstr(f"Unable to find image at requested path: {cstr.color.BOLD}{full_path}{cstr.color.END}").error.print()
return None
return full_path
def filter_arguments(allowed_args):
filtered_args = [arg for arg in sys.argv if arg in allowed_args]
sys.argv = [sys.argv[0]] + filtered_args
def get_color_palettes(path):
if os.path.exists(path):
with open(path, 'r') as file:
theme_code = file.read()
else:
with open(os.path.join(ROOT, 'cg_default_themes.json'), 'r') as file:
theme_code = file.read()
color_palettes = json.loads(theme_code)
cstr(f"Unable to locate ComfyUI color palette file at {cstr.color.BOLD}{path}{cstr.color.END}, defaulting to built-in light/dark themes.").warning.print()
return color_palettes
match = re.search(r'const colorPalettes = ({.*?});', theme_code, re.DOTALL)
if match:
color_palettes = match.group(1)
color_palettes = re.sub(r'\s\/\/\s.*$', '', color_palettes, flags=re.MULTILINE)
color_palettes = re.sub(r',(\s*?})', r'\1', color_palettes)
color_palettes = json.loads(color_palettes)
return color_palettes
return {}
def split_paths(arg):
paths = arg.split(',')
paths = [path.strip() for path in paths]
return paths
def create_cors_middleware(allowed_origin: str):
@web.middleware
async def cors_middleware(request: web.Request, handler):
if request.method == "OPTIONS":
# Pre-flight request. Reply successfully:
response = web.Response()
else:
response = await handler(request)
response.headers['Access-Control-Allow-Origin'] = allowed_origin
response.headers['Access-Control-Allow-Methods'] = 'POST, GET, DELETE, PUT, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
response.headers['Access-Control-Allow-Credentials'] = 'true'
return response
return cors_middleware
def get_paths(category, path):
result = {
"directories": [],
"images": []
}
for image_path in IMAGE_PATHS:
if os.path.basename(image_path) == category:
base_folder = image_path
path = path if not path.startswith('/') else ''
path = path.replace('/', os.path.sep)
target_folder = os.path.join(base_folder, path)
combined_path = os.path.abspath(target_folder)
if not combined_path.startswith(base_folder):
return json.dumps(result, indent=4)
try:
if not path or path == "":
for item in os.listdir(image_path):
item_path = os.path.join(image_path, item)
if os.path.isdir(item_path):
relative_path = os.path.relpath(item_path, base_folder).replace("\\", "/")
result["directories"].append(relative_path)
else:
if os.path.splitext(item)[1] in ['.png', '.webp', '.jpg', '.jpeg', '.gif']:
image_relative_path = os.path.join(path, item).replace("\\", "/")
result["images"].append(image_relative_path)
else:
for item in os.listdir(combined_path):
item_path = os.path.join(combined_path, item)
if os.path.isdir(item_path):
relative_path = os.path.relpath(item_path, base_folder).replace("\\", "/")
result["directories"].append(relative_path)
else:
if os.path.splitext(item)[1] in ['.png', '.webp', '.jpg', '.jpeg', '.gif']:
image_relative_path = os.path.join(path, item).replace("\\", "/")
result["images"].append(image_relative_path)
except Exception as e:
cstr(f"There was an error with a path request for category `{category}` and path `{path}`").error.print()
print(e)
break
if result['directories']:
result['directories'] = sorted(result['directories'])
if result['images']:
result['images'] = sorted(result['images'])
return json.dumps(result)
def compress_image(category, path):
full_path = get_full_path(category, path)
if full_path:
with open(full_path, "rb") as image_file:
image_data = image_file.read()
thumbnail_filename = os.path.basename(full_path)
thumbnail_extension = os.path.splitext(thumbnail_filename)[1]
hash_object = hashlib.sha256(image_data)
hash_hex = hash_object.hexdigest()
cleaned_hash = re.sub(r"[^a-zA-Z0-9]", "", hash_hex)
thumbnail_filename = f"{thumbnail_filename}_{cleaned_hash}{thumbnail_extension}"
thumbnail_path = os.path.join(THUMBNAIL_DIRECTORY, thumbnail_filename)
if os.path.exists(thumbnail_path):
with open(thumbnail_path, "rb") as thumbnail_file:
return thumbnail_file.read()
try:
image = Image.open(full_path)
if image.mode != "RGB":
image = image.convert("RGB")
width, height = image.size
aspect_ratio = min(200 / width, 400 / height)
new_width = int(width * aspect_ratio)
new_height = int(height * aspect_ratio)
resized_image = image.resize((new_width, new_height), Image.Resampling(1))
output_buffer = io.BytesIO()
resized_image.save(output_buffer, "JPEG", quality=90)
compressed_bytes = output_buffer.getvalue()
output_buffer.close()
with open(thumbnail_path, "wb") as thumbnail_file:
thumbnail_file.write(compressed_bytes)
return compressed_bytes
except OSError as e:
cstr(f"The image from category {cstr.color.LIGHTYELLOW}{category}{cstr.color.END} at {cstr.color.BOLD}{path}{cstr.color.END} may be corrupted.").error.print()
print(f"Error Message: {e}")
return b''
# ROUTE FUNCTIONS
# GET DIRECTORY PATHS
async def get_directory(request):
category = request.query.get("category")
path = request.query.get("path")
category = urllib.parse.unquote(category) if category else None
path = urllib.parse.unquote(path) if path else None
if category is None or path is None:
cstr("A request for folder paths is being made without query paramters.").warning.print()
return web.Response(text="Missing query parameters 'category' or 'path'", status=400)
json_result = get_paths(category, path)
return web.json_response(json_result)
# GET IMAGE THUMBNAIL
async def get_image(request):
category = request.query.get("category")
path = request.query.get("path")
category = urllib.parse.unquote(category) if category else None
path = urllib.parse.unquote(path) if path else None
compressed_bytes = compress_image(category, path)
response = web.StreamResponse()
response.content_type = 'image/jpeg'
response.content_length = len(compressed_bytes)
await response.prepare(request)
await response.write(compressed_bytes)
await response.write_eof()
return response
# SEARCH IMAGES
async def search_images(request):
def search_image_paths(search_query):
results = {"images": []}
for path in IMAGE_PATHS:
if os.path.isdir(path):
for root, _, files in os.walk(path):
for file in files:
file_path = os.path.join(root, file)
if is_valid_image(file_path):
matched_objects = search_query_in_filename(file, search_query) or search_query_in_workflow(file_path, search_query)
if matched_objects:
result = {
"category": os.path.basename(path),
"path": os.path.relpath(file_path, path),
"matched": matched_objects[1][1] if len(matched_objects) > 1 and len(matched_objects[1]) > 1 else matched_objects,
}
results["images"].append(result)
return results
def is_valid_image(file_path):
_, extension = os.path.splitext(file_path)
return extension.lower() in ALLOWED_EXTENSIONS
def search_query_in_filename(filename, search_query):
matched_objects = []
for query in search_query.split():
if query.lower() in filename.lower():
matched_objects.append(([], None))
return matched_objects
def search_query_in_workflow(file_path, search_query):
matched_objects = []
try:
image = Image.open(file_path)
workflow = image.text.get("workflow")
if workflow:
json_obj = json.loads(workflow)
matched_objects = is_search_query_match(json_obj, search_query, [])
except Exception as e:
pass
return matched_objects
def is_search_query_match(json_obj, search_query, path):
matched_objects = []
if isinstance(json_obj, dict):
for key, value in json_obj.items():
new_path = path + [key]
if is_match(key, value, search_query):
matched_objects.append((new_path, json_obj))
matched_objects.extend(is_search_query_match(value, search_query, new_path))
elif isinstance(json_obj, list):
for index, item in enumerate(json_obj):
new_path = path + [index]
matched_objects.extend(is_search_query_match(item, search_query, new_path))
elif isinstance(json_obj, tuple):
for index, item in enumerate(json_obj):
new_path = path + [index]
matched_objects.extend(is_search_query_match(item, search_query, new_path))
return matched_objects
def is_match(key, value, search_query):
term = search_query.lower()
return term in str(key).lower() or term in str(value).lower()
query = request.query.get("query")
query = urllib.parse.unquote(query) if query else None
result = {"images": []}
if query:
result = search_image_paths(query)
return web.Response(text=json.dumps(result), content_type='application/json')
# DELETE IMAGE
last_image = ()
async def delete_image(request):
global last_image
category = request.query.get("category")
path = request.query.get("path")
category = urllib.parse.unquote(category) if category else None
path = urllib.parse.unquote(path) if path else None
if last_image:
if last_image == (category, path):
return web.Response(text=json.dumps({"success":True}), content_type='application/json')
last_image = (category, path)
full_path = get_full_path(category, path)
try:
os.remove(full_path)
cstr(f"Successfully deleted file: {full_path}").msg.print()
return web.Response(text=json.dumps({"success":True}), content_type='application/json')
except FileNotFoundError:
pass
except Exception as e:
cstr(f"An error occurred while deleting file: {full_path}").error.print()
print(e)
return web.Response(text=json.dumps({"success":False}), content_type='application/json')
# GET WORKFLOWS
last_image = None
async def get_workflow(request):
global last_image
category = request.query.get("category")
path = request.query.get("path")
category = urllib.parse.unquote(category) if category else None
path = urllib.parse.unquote(path) if path else None
full_path = get_full_path(category, path)
image = Image.open(full_path)
workflow = {}
if hasattr(image, 'text'):
if 'workflow' in image.text:
workflow = json.loads(image.text['workflow'])
return web.Response(text=json.dumps(workflow, indent=4), content_type='text/plain')
# GET FAV ICON SVG
async def get_fav_icon(request):
svg = '''<svg xmlns="http://www.w3.org/2000/svg" width="1024" height="1024" shape-rendering="geometricPrecision" image-rendering="optimizeQuality" fill-rule="evenodd" xmlns:v="https://vecta.io/nano"><path fill="#C1344E" d="M816.5 139.5c.762 1.762 2.096 2.762 4 3 125.583 111.584 181.75 251.584 168.5 420-21.063 159.595-99.897 281.095-236.5 364.5-102.486 57.637-211.82 76.64-328 57C286.266 955.677 179.1 882.51 103 764.5c-29.733-48.125-50.567-99.792-62.5-155-27.685-151.595 6.481-287.262 102.5-407C248.583 82.322 381.75 25.489 542.5 32c102.956 7.351 194.29 43.185 274 107.5zm71 331c7.284 56.848.117 111.848-21.5 165-19.757 51.712-49.09 97.046-88 136-64.708 66.104-143.208 105.271-235.5 117.5-50.532 4.638-99.532-2.028-147-20-33.858-12.091-65.191-28.591-94-49.5-.473-1.406-1.473-2.073-3-2 0-.667-.333-1-1-1a608.67 608.67 0 0 1-67.5-70c-42.768-57.276-68.102-121.609-76-193-2.956-46.221 4.044-90.887 21-134 15.195-41.082 36.528-78.415 64-112 32.099-38.788 69.599-70.955 112.5-96.5 35.984-19.662 73.984-33.995 114-43 56.31-10.79 111.31-6.123 165 14 50.397 17.513 95.064 44.18 134 80 27.031 25.32 50.864 53.154 71.5 83.5 23.534 39.115 40.7 80.782 51.5 125z" opacity=".996"/><path fill="#000001" d="M887.5 470.5c-10.8-44.218-27.966-85.885-51.5-125-20.636-30.346-44.469-58.18-71.5-83.5-38.936-35.82-83.603-62.487-134-80-53.69-20.123-108.69-24.79-165-14-40.016 9.005-78.016 23.338-114 43-42.901 25.545-80.401 57.712-112.5 96.5-27.472 33.585-48.805 70.918-64 112-16.956 43.113-23.956 87.779-21 134 7.898 71.391 33.232 135.724 76 193a608.67 608.67 0 0 0 67.5 70c-41.104-29.937-75.937-65.937-104.5-108-79.92-130.022-80.586-260.356-2-391 35.965-52.966 80.799-96.799 134.5-131.5 134.728-75.885 266.394-70.885 395 15 49.702 36.358 90.202 80.858 121.5 133.5 23.469 42.573 38.636 87.907 45.5 136z" opacity=".432"/><path d="M816.5 139.5c1.904.238 3.238 1.238 4 3-1.904-.238-3.238-1.238-4-3z" opacity=".004"/><path fill="#000001" d="M820.5 142.5c96.391 73.675 157.224 170.342 182.5 290 24.59 129.49 2.92 250.49-65 363-76.025 116.49-182.525 188.99-319.5 217.5-140.775 24.96-269.442-3.37-386-85-106.222-80.207-170.222-186.374-192-318.5 11.933 55.208 32.767 106.875 62.5 155C179.1 882.51 286.266 955.677 424.5 984c116.18 19.64 225.514.637 328-57C889.103 843.595 967.937 722.095 989 562.5c13.25-168.416-42.917-308.416-168.5-420z" opacity=".426"/><path fill="#C1344E" d="M682.5 281.5c.473 1.406 1.473 2.073 3 2 .667 0 1 .333 1 1 30.306 26.132 56.473 55.799 78.5 89 44.091 70.989 52.424 145.989 25 225-14.961 37.283-35.961 70.616-63 100-32.169 36.189-70.002 64.689-113.5 85.5-82.407 34.114-161.74 28.114-238-18-53.495-35.159-96.328-80.326-128.5-135.5-10.459-20.228-18.625-41.228-24.5-63-15.198-77.186.302-147.52 46.5-211 29.415-39.42 64.582-72.586 105.5-99.5 91.333-54 182.667-54 274 0 11.93 7.453 23.264 15.62 34 24.5z" opacity=".998"/><path d="M682.5 281.5c1.527-.073 2.527.594 3 2-1.527.073-2.527-.594-3-2z" opacity=".004"/><path fill="#000001" d="M686.5 284.5c43.046 31.208 78.213 69.542 105.5 115 39.782 74.119 45.448 150.786 17 230-14.726 35.146-34.726 66.813-60 95-24.558 27.247-52.058 50.747-82.5 70.5-76.32 45.405-156.32 53.072-240 23-35.146-14.726-66.813-34.726-95-60-30.748-27.721-56.581-59.221-77.5-94.5-16.814-29.941-27.314-61.941-31.5-96 5.875 21.772 14.041 42.772 24.5 63 32.172 55.174 75.005 100.341 128.5 135.5 76.26 46.114 155.593 52.114 238 18 43.498-20.811 81.331-49.311 113.5-85.5 27.039-29.384 48.039-62.717 63-100 27.424-79.011 19.091-154.011-25-225-22.027-33.201-48.194-62.868-78.5-89z" opacity=".419"/><path d="M298.5 817.5c1.527-.073 2.527.594 3 2-1.527.073-2.527-.594-3-2z" opacity=".004"/></svg>'''
return web.Response(text=svg, content_type="image/svg+xml")
async def index(request):
return web.Response(text=HTML, content_type='text/html')
if __name__ == "__main__":
# Startup
try:
window_title(TITLE)
except AttributeError as e:
pass
cstr("Starting ComfyUI Gallery ...").msg.print()
# CLI Arguments
parser = argparse.ArgumentParser(prog='comfyui_explorer.py')
parser.add_argument("--no-browser", action="store_true", help="Do not launch system browser when the server launches.")
parser.add_argument("--purge-cache", action="store_true", help="Delete the image gallery cache on startup.")
parser.add_argument('--image-paths', type=split_paths)
parser.add_argument('--comfyui-path', type=str, default=None, help="Path to ComfyUI root directory.")
parser.add_argument('--comfyui-color-palette', type=str, default=None, help="Path to a ComfyUI colorPalette.js file")
args = parser.parse_args()
if args.no_browser:
NO_BROWSER = True
if args.image_paths:
for _ in args.image_paths:
IMAGE_PATHS.append(_)
if args.purge_cache:
PURGE_CACHE = True
if args.comfyui_path:
CP_FILE = os.path.join(args.comfyui_path, CP_FILE)
new_image_paths = []
for img_path in IMAGE_PATHS:
if not os.path.isabs(img_path):
new_image_paths.append(os.path.join(args.comfyui_path, img_path))
else:
new_image_paths.append(img_path)
IMAGE_PATHS = new_image_paths
else:
CP_FILE = os.path.join(ROOT, CP_FILE)
new_image_paths = []
for img_path in IMAGE_PATHS:
if not os.path.isabs(img_path):
new_image_paths.append(os.path.join(ROOT, img_path))
else:
new_image_paths.append(img_path)
IMAGE_PATHS = new_image_paths
if args.comfyui_color_palette:
CP_FILE = args.comfyui_color_palette
# HANDLE TEMP PATH
if os.path.exists(THUMBNAIL_DIRECTORY) and PURGE_CACHE:
shutil.rmtree(THUMBNAIL_DIRECTORY)
os.makedirs(THUMBNAIL_DIRECTORY, exist_ok=True)
# Setup CSS Colors
COLORS = get_color_palettes(CP_FILE)
light_css_colors = ""
for key, value in COLORS['light']['colors']['node_slot'].items():
light_css_colors += f"\t\t\t--{key.lower().replace('_','-')}: {value};\n"
for key, value in COLORS['light']['colors']['comfy_base'].items():
light_css_colors += f"\t\t\t--{key.lower().replace('_','-')}: {value};\n"
light_css_colors += f"\t\t\t--header-bg: {COLORS['light']['colors']['litegraph_base']['NODE_DEFAULT_BOXCOLOR']};\n"
light_css_colors += f"\t\t\t--class-menu-bg: {COLORS['light']['colors']['litegraph_base']['WIDGET_BGCOLOR']};\n"
light_css_colors += f"\t\t\t--class-info-bg: {COLORS['light']['colors']['litegraph_base']['NODE_DEFAULT_BGCOLOR']};\n"
light_css_colors += f"\t\t\t--main-text-color: {COLORS['light']['colors']['litegraph_base']['NODE_SELECTED_TITLE_COLOR']};\n"
light_css_colors += f"\t\t\t--text-color: {COLORS['light']['colors']['litegraph_base']['NODE_TEXT_COLOR']};\n"
light_css_colors += f"\t\t\t--alt-text-color: {COLORS['light']['colors']['litegraph_base']['NODE_TITLE_COLOR']};\n"
light_css_colors += f"\t\t\t--alt-2-text-color: {COLORS['light']['colors']['litegraph_base']['WIDGET_SECONDARY_TEXT_COLOR']};\n"
light_css_colors += f"\t\t\t--shadow-color: {COLORS['light']['colors']['litegraph_base']['DEFAULT_SHADOW_COLOR']};\n"
light_css_colors += f"\t\t\t--link-color: {COLORS['light']['colors']['litegraph_base']['LINK_COLOR']};\n"
light_css_colors += f"\t\t\t--event-link-color: {COLORS['light']['colors']['litegraph_base']['EVENT_LINK_COLOR']};\n"
light_css_colors += f"\t\t\t--connecting-link-color: {COLORS['light']['colors']['litegraph_base']['CONNECTING_LINK_COLOR']};\n"
light_css_colors += f"\t\t\t--connecting-link-color: {COLORS['light']['colors']['litegraph_base']['CONNECTING_LINK_COLOR']};\n"
dark_css_colors = ""
for key, value in COLORS['dark']['colors']['node_slot'].items():
dark_css_colors += f"\t\t\t--{key.lower().replace('_','-')}: {value};\n"
for key, value in COLORS['dark']['colors']['comfy_base'].items():
dark_css_colors += f"\t\t\t--{key.lower().replace('_','-')}: {value};\n"
dark_css_colors += f"\t\t\t--header-bg: {COLORS['dark']['colors']['litegraph_base']['NODE_DEFAULT_BOXCOLOR']};\n"
dark_css_colors += f"\t\t\t--class-menu-bg: {COLORS['dark']['colors']['litegraph_base']['WIDGET_BGCOLOR']};\n"
dark_css_colors += f"\t\t\t--class-info-bg: {COLORS['dark']['colors']['litegraph_base']['NODE_DEFAULT_BGCOLOR']};\n"
dark_css_colors += f"\t\t\t--main-text-color: {COLORS['dark']['colors']['litegraph_base']['NODE_SELECTED_TITLE_COLOR']};\n"
dark_css_colors += f"\t\t\t--text-color: {COLORS['dark']['colors']['litegraph_base']['NODE_TEXT_COLOR']};\n"
dark_css_colors += f"\t\t\t--alt-text-color: {COLORS['dark']['colors']['litegraph_base']['NODE_TITLE_COLOR']};\n"
dark_css_colors += f"\t\t\t--alt-2-text-color: {COLORS['dark']['colors']['litegraph_base']['WIDGET_SECONDARY_TEXT_COLOR']};\n"
dark_css_colors += f"\t\t\t--shadow-color: {COLORS['dark']['colors']['litegraph_base']['DEFAULT_SHADOW_COLOR']};\n"
dark_css_colors += f"\t\t\t--link-color: {COLORS['dark']['colors']['litegraph_base']['LINK_COLOR']};\n"
dark_css_colors += f"\t\t\t--event-link-color: {COLORS['dark']['colors']['litegraph_base']['EVENT_LINK_COLOR']};\n"
dark_css_colors += f"\t\t\t--connecting-link-color: {COLORS['dark']['colors']['litegraph_base']['CONNECTING_LINK_COLOR']};\n"
# Define the ComfyUI Dictionary Webpage
HTML = '''
<!DOCTYPE html>
<html data-theme="light">
<head>
<title>ComfyGallery</title>
<meta charset="UTF-8">
<link rel="icon" type="image/svg+xml" href="http://''' + DOMAIN + ''':''' + str(PORT) + '''/favicon.svg">
<style id="theme-styles">
/* GLOBAL COLORS */
[data-theme="dark"] {
''' + dark_css_colors + '''
--trans-light: rgba(255,255,255,0.5);
--trans-dark: rgba(0,0,0,0.5);
--svg-invert: invert(100%);
--svg-active: invert(68%) sepia(79%) saturate(727%) hue-rotate(338deg) brightness(101%) contrast(101%);
--svg-cat-color: invert(27%) sepia(49%) saturate(3049%) hue-rotate(327deg) brightness(85%) contrast(85%);
--svg-menu-color: invert(58%) sepia(86%) saturate(342%) hue-rotate(108deg) brightness(95%) contrast(81%);
--content-text-color: rgba(0,0,0,0.75);
--content-text-shadow: 2px 2px 2px rgba(0,0,0,0.15);
--cat-color: #c1344e;
--menu-color: #39bf90;
--gen-title: #EFEFEF;
}
[data-theme="light"] {
''' + light_css_colors + '''
--trans-light: rgba(255,255,255,0.25);
--trans-dark: rgba(0,0,0,0.25);
--svg-invert: none;
--svg-active: invert(70%) sepia(92%) saturate(1082%) hue-rotate(3deg) brightness(107%) contrast(106%);
--svg-cat-color: invert(59%) sepia(72%) saturate(2627%) hue-rotate(183deg) brightness(102%) contrast(92%);
--svg-menu-color: invert(60%) sepia(35%) saturate(1025%) hue-rotate(175deg) brightness(84%) contrast(90%);
--content-text-color: rgba(0,0,0,0.75);
--content-text-shadow: 2px 2px 2px rgba(0,0,0,0.15);
--cat-color: #42a5f5;
--menu-color: #4693ce;
--gen-title: #eee;
}
/* MAIN BODY */
body {
font-family: sans-serif;
margin: 0;
background-color: var(--header-bg);
text-shadow: 1px 1px 0px var(--shadow-color);
}
a, a:link, a:visited { color: var(--clip); transition: color .2s ease; }
a:hover { color: var(--main-text-color); }
a:active: { color: var(--image); }
#class-list-container #class-list .header p a.logo-link {
display: inline-block;
text-decoration: none;
color: inherit !important;
transition: all .2s ease;
}
#class-list-container #class-list .header p a.logo-link:hover { transform: scale(1.05) !important; }
footer {
position: fixed;
height: 30px;
background-color: var(--tr-odd-bg-color);
box-shadow: inset 0px -10px 40px var(--shadow-color);
border-top: 2px solid var(--trans-light);
padding-top: 5px;
padding: 10px 20px;
text-align: right;
bottom: 0;
right: 0;
left: 0;
transition: background-color .125s ease;
}
footer .footer-content { font-size: 15px; color: var(--main-text-color); }
footer .footer-content .node-bullet {
color: var(--cat-color);
font-size: 16px;
margin-left: -1px;
margin-right: -1px;
vertical-align: inherit;
}
footer .footer-links {
display: inline-block;
margin-right: 20px;
line-height: 30px;
}
footer .footer-links .link {
padding-right: 10px;
border-right: 1px solid var(--trans-dark);
margin-left: 10px;
}
h1 {
text-align: center;
}
/* INPUT */
button {
padding: 5px;
background-color: var(--gen-title);
border-radius: 5px;
border: 1px solid var(--bg-color);
font-size: 15px;
font-weight: bold;
color: var(--content-text-color);
}
button:hover {
border-color: var(--clip);
}
button.active {
background-color: var(--conditioning);
border-color: var(--border-color);
}
.relative-container {
position: relative !important;
}
/* NODE CLASS LIST */
#logo-container {
position: relative;
background-color: var(--bg-color);
border-bottom: 3px solid var(--alt-text-color);
box-shadow: 0px 0px 25px 11px rgba(0,0,0,0.25);
z-index: 1000;
}
#logo-container .header {
position: relative;
padding: 0;
margin: 0;
height: 80px;
}
#logo-container .header p {
margin: 0;
position: absolute;
top: 40%;
left: 60px;
transform: translateY(-50%);
text-transform: uppercase;
letter-spacing: 2px;
font-weight: bold;
font-size: 28px;
color: var(--main-text-color);
text-shadow: 2px 2px 0 var(--shadow-color);
line-height: 15px;
text-dectoration: none;
}
#logo-container .header p a,
#logo-container .header p a:link,
#logo-container .header p a:active,
#logo-container .header p a:visited {
color: var(--main-text-color);
text-decoration: none !important;
transition: all .2s ease;
}
#logo-container .header p a:hover {
color: var(--clip);
}
#logo-container .header p a .comfyui {
font-size: small;
padding-bottom: 4px;
display: inline-block;
padding-left: 2px;
}
/* CLASS INFO */
#class-info {
width: 100%;
color: var(--main-text-color);
overflow: auto;
transition: background-color .125s ease;
padding-bottom: 0;
}
#class-info .content {
padding: 10px;
padding-bottom: 0;
margin: 10px 50px 0 50px
}
#class-info .content .title {
margin: 10px 0;
font-size: 28px;
color: var(--gen-title);
text-shadow: 2px 2px 0 var(--shadow-color);
border-bottom: 1px solid var(--trans-dark);
padding-bottom: 4px;
}
#class-info .content .title .subtitle { font-weight: 100; color: var(--content-text-color); }
#class-info .content h2,
#class-info .content h3 { margin: 10px 5px; color: var(--main-text-color); }
#class-info .content h4 { color: var(--main-text-color); }
#class-info .content p { max-width: 1280px; margin: 4px; }
#class-info .content p.description { margin: 15px 5px; font-size: 16px; }
#class-info .content .class-info-address { font-size: 18px; margn-left: 5px; }
li.active { background-color: var(--shadow-color); color: var(--text-color) !important; }
.node-bullet {
color: var(--cat-color);
font-size: 30px;
vertical-align: top;
display: inline-block;
margin-left: -2px;
margin-right: -2px;
padding-top: 2px;
}
/* General Content */
.gen-container {
background-color: var(--bg-color);
box-shadow: 0 0 0 5px var(--comfy-menu-bg);
margin-top: 20px;
margin-bottom: 25px;
margin-left: 0;
margin-right: 0;
border-radius: 8px;
}
.gen-menu {
background-color: var(--class-info-bg);
border-bottom: 1px solid rgba(0,0,0,0.25);
border-top: 1px solid rgba(255,255,255,0.25);
padding: 5px 10px;
box-shadow: inset 0 0 20px var(--shadow-color);
color: var(--alt-2-text-color);
}
.gen-container h3 {
padding: 10px !important;
margin: 0 !important;
border-bottom: 1px solid var(--shadow-color);
border-top: 3px solid rgba(255,255,255,0.2);
background-color: var(--cat-color);
border-top-right-radius: 8px;
border-top-left-radius: 8px;
color: var(--gen-title) !important;
text-shadow: 2px 2px 0 var(--shadow-color);
}
.gen-container .gen-content {
padding: 0 !important;
margin: 0 !important;
border-top: 1px solid var(--trans-light);
font-size: 16px;
}
.gen-container .gen-content .gen-content-subcontainer { padding: 5px 15px; }
.gen-container .gen-content .gen-content-subcontainer h4 { margin-top: 5px; }
.gen-scroll {
overflow: auto;
max-height: fit-content;
height: 500px;
resize: vertical;
border-top: 1px solid var(--trans-light);
}
.code {
display: inline-block;
width: 95%;
color: inherit;
padding: 10px;
border-radius: 3px;
margin-bottom: 5px;
opacity: 0.75;
background-color: rgba(255,255,255,0.2);
border-bottom: 1px solid rgba(0,0,0,0.15);
border-right: 1px solid rgba(0,0,0,0.15);
border-left: 1px solid var(--trans-light);
border-top: 1px solid var(--trans-light);
box-shadow: inset -20px -5px 60px var(--shadow-color);
}
.font-normal {
font-weight: normal !important;
text-decoration: none !important;
}
.font-normal a {
font-weight: normal;
text-decoration: none !important;
}
.code h5 { margin: 5px; var(--alt-2-text-color); }
.code ol {
margin: 0;
padding-left: 20px;
counter-reset: item;
list-style-type: none;
counter-reset: combo-id -1;
}
.code ol li { margin: 0 5px 0 5px; counter-increment: combo-id; }
.code ol li:before {
display: inline-block;
width: 45px;
margin-right: 10px;
text-align: right;
content: " [" counter(combo-id) "] ";
}
.text-block {
max-width: 70ch;
line-height: 1.5;
text-align: justify;
text-justify: inter-word;
font-size: 16px;
margin-bottom: 25px !important;
}
/* Image Gallery */
.image-gallery {
display: flex;
margin: 10px;
flex-wrap: wrap;
justify-content: flex-start;
align-items: flex-start;
gap: 10px;
justify-content: space-evenly;
}
.image-container {
flex: 0 0 calc(25% - 10px);
max-width: 400px;
height: auto;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: var(--trans-dark);
border: 1px solid var(--border-color);
border-radius: 8px;
overflow:hidden;
}
.image-container a img { max-width: 400px; }
#image-gallery { margin-bottom: 20px !important; padding-bottom: 0 !important; }
.gen-scroll.image-gallery-scroll,
.image-gallery-scroll { height: 300px; max-height: fit-content }
#image-gallery.content .gen-container .gen-content-subcontainer.gen-scroll { padding-top: 80px; height: 75vh; }
.gen-gallery-container {
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
gap: 10px;
}
#button-container {
position: absolute;
top: 70px;
left: 0;
right: 0;
padding: 8px;
background-color: rgba(65,65,65,0.5);
z-index: 9999;
line-height: 50px;
}
#button-container button {
padding: 5px;
background-color: var(--gen-title);
border-radius: 5px;
border: 1px solid var(--bg-color);
font-size: 15px;
font-weight: bold;
color: var(--content-text-color);
}
#button-container button:hover { border-color: var(--clip); }
#home-button {
background-color: var(--cat-color) !important;
border: 1px solid var(--clip-vision);
border-radius: 5px;
color: var(--gen-title);
}
#home-button:disabled, #button-container button:disabled {
background-color: var(--class-menu-bg) !important;
border: 1px solid var(--border-color) !important;
color: var(--border-color);
opacity: 0.7;
}
.directory {
display: flex;
padding: 10px;
flex: 0 0 calc(12%);
align-items: center;
background-color: var(--trans-dark);
border: 1px solid transparent;
border-radius: 8px;
font-weight: bold;
word-wrap: break-word;
cursor: pointer;
transition: all .2s ease;
}
.directory:before {
display: inline-block;
content: '📂';
margin-right: 5px;
margin-bottom: 5px;
}
.directory:hover { box-shadow: inset 0 0 20px var(--class-info-bg); color: var(--clip); }
.gallery-image-container {
flex: 0 0 calc(25% - 10px);
max-width: 200px;
height: fit-content !important;
margin: 5px;
padding: 5px;
background-color: var(--class-info-bg);
border-radius: 5px;
border: 1px solid var(--border-color);
box-shadow: inset 0px 0px 10px var(--bg-color);
transition: transform .2s ease;
}
.gallery-image-container:hover { transform: scale(1.05); box-shadow: 0 0 5px var(--clip); }
.gen-image-link { max-width: 200px; cursor: pointer; }
.gallery-image-container a img {
max-width: 200px;
cursor: pointer;
border: 1px solid var(--border-color);
border-radius: 4px;
}
.gen-gallery-image-title {
font-size: 12px;
letter-spacing: 1px;
text-align: center;
padding-top: 5px;
padding-bottom: 5px;
overflow:hidden;
word-wrap: break-word;
}
#category-dropdown { display: inline-block !important; }
#category-dropdown select {
background-color: var(--cat-color);
font-weight: bold;
font-size: 28px;
border: 1px solid var(--border-color);
border-radius: 4px;
padding: 5px;
color: var(--gen-title);
font-size: 28px;
text-shadow: 2px 2px 2px var(--shadow-color);
box-shadow: 0 0 0 2px var(--fg-color);
}
.gen-gal-sep {
display: block !important;
margin-bottom: 20px;
width: 100%;
border-top: 1px solid rgba(255,255,255,0.2);
border-bottom: 2px solid rgba(0,0,0,0.3);
box-shadow: 0 2px 2px rgba(0,0,0,0.2);
}
.image-gallery-search {
float: right;