Skip to content

Commit 962c67a

Browse files
committed
fix list index out of range, #34
1 parent c25e8a7 commit 962c67a

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

scripts/image_browser.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import gradio as gr
2-
import json
32
import logging
43
import os
54
import random
5+
import platform
66
import re
77
import shutil
88
import stat
9+
import sys
910
import time
1011
import modules.extras
1112
import modules.ui
13+
from modules import paths
1214
from modules import script_callbacks
1315
from modules import shared, scripts, images
1416
from modules.shared import opts, cmd_opts
@@ -41,6 +43,7 @@
4143
current_depth = 0
4244
init = True
4345

46+
# Logging
4447
logger = logging.getLogger(__name__)
4548
logger_mode = logging.ERROR
4649
if hasattr(opts, "image_browser_logger_warning"):
@@ -53,6 +56,22 @@
5356
console_handler = logging.StreamHandler()
5457
console_handler.setLevel(logger_mode)
5558
logger.addHandler(console_handler)
59+
# Debug logging
60+
if logger.isEnabledFor(logging.DEBUG):
61+
logger.debug(f"{sys.executable} {sys.version}")
62+
logger.debug(f"{platform.system()} {platform.version()}")
63+
try:
64+
git = os.environ.get('GIT', "git")
65+
commit_hash = os.popen(f"{git} rev-parse HEAD").read()
66+
except Exception as e:
67+
commit_hash = e
68+
logger.debug(f"{commit_hash}")
69+
logger.debug(f"Gradio {gr.__version__}")
70+
logger.debug(f"{paths.script_path}")
71+
with open(cmd_opts.ui_config_file, "r") as f:
72+
logger.debug(f.read())
73+
with open(cmd_opts.ui_settings_file, "r") as f:
74+
logger.debug(f.read())
5675

5776
def delete_recycle(filename):
5877
if opts.image_browser_delete_recycle:
@@ -468,16 +487,22 @@ def get_image_page(img_path, page_index, filenames, keyword, sort_by, sort_order
468487
load_info += "</div>"
469488
return filenames, gr.update(value=page_index, label=f"Page Index ({page_index}/{max_page_index})"), image_list, "", "", "", visible_num, load_info
470489

471-
472-
473490
def get_current_file(tabname_box, num, page_index, filenames):
474491
file = filenames[int(num) + int((page_index - 1) * num_of_imgs_per_page)]
475492
return file
476493

477-
def show_image_info(tabname_box, num, page_index, filenames):
478-
file = filenames[int(num) + int((page_index - 1) * num_of_imgs_per_page)]
479-
tm = "<div style='color:#999' align='right'>" + time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(os.path.getmtime(file))) + "</div>"
480-
return file, tm, num, file, ""
494+
def show_image_info(tabname_box, num, page_index, filenames, turn_page_switch):
495+
logger.debug(f"tabname_box, num, page_index, len(filenames), num_of_imgs_per_page: {tabname_box}, {num}, {page_index}, {len(filenames)}, {num_of_imgs_per_page}")
496+
if len(filenames) == 0:
497+
# This should only happen if webui was stopped and started again and the user clicks on one of the still displayed images.
498+
# The state with the filenames will be empty then. In that case we return None to prevent further errors and force a page refresh.
499+
turn_page_switch = -turn_page_switch
500+
file = None
501+
tm = None
502+
else:
503+
file = filenames[int(num) + int((page_index - 1) * num_of_imgs_per_page)]
504+
tm = "<div style='color:#999' align='right'>" + time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(os.path.getmtime(file))) + "</div>"
505+
return file, tm, num, file, turn_page_switch
481506

482507
def show_next_image_info(tabname_box, num, page_index, filenames, auto_next):
483508
file = filenames[int(num) + int((page_index - 1) * num_of_imgs_per_page)]
@@ -714,7 +739,7 @@ def create_tab(tabname):
714739
)
715740

716741
# other functions
717-
set_index.click(show_image_info, _js="image_browser_get_current_img", inputs=[tabname_box, image_index, page_index, filenames], outputs=[img_file_name, img_file_time, image_index, hidden])
742+
set_index.click(show_image_info, _js="image_browser_get_current_img", inputs=[tabname_box, image_index, page_index, filenames, turn_page_switch], outputs=[img_file_name, img_file_time, image_index, hidden, turn_page_switch])
718743
set_index.click(fn=lambda:(gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)), inputs=None, outputs=[delete_panel, button_panel, ranking])
719744
img_file_name.change(fn=lambda : "", inputs=None, outputs=[collected_warning])
720745
img_file_name.change(get_ranking, inputs=img_file_name, outputs=ranking)

0 commit comments

Comments
 (0)