From 5206b938efe617e7747472e12360db9096fa6eb0 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:03:21 +0900 Subject: [PATCH] InputAccordion duplicate elem_id handling (#16381) --- modules/ui_components.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/modules/ui_components.py b/modules/ui_components.py index 9cf67722a..3e3ea54ba 100644 --- a/modules/ui_components.py +++ b/modules/ui_components.py @@ -91,6 +91,7 @@ class InputAccordion(gr.Checkbox): Actually just a hidden checkbox, but creates an accordion that follows and is followed by the state of the checkbox. """ + accordion_id_set = set() global_index = 0 def __init__(self, value, **kwargs): @@ -99,6 +100,18 @@ class InputAccordion(gr.Checkbox): self.accordion_id = f"input-accordion-{InputAccordion.global_index}" InputAccordion.global_index += 1 + if not InputAccordion.accordion_id_set: + from modules import script_callbacks + script_callbacks.on_script_unloaded(InputAccordion.reset) + + if self.accordion_id in InputAccordion.accordion_id_set: + count = 1 + while (unique_id := f'{self.accordion_id}-{count}') in InputAccordion.accordion_id_set: + count += 1 + self.accordion_id = unique_id + + InputAccordion.accordion_id_set.add(self.accordion_id) + kwargs_checkbox = { **kwargs, "elem_id": f"{self.accordion_id}-checkbox", @@ -143,3 +156,7 @@ class InputAccordion(gr.Checkbox): def get_block_name(self): return "checkbox" + @classmethod + def reset(cls): + cls.global_index = 0 + cls.accordion_id_set.clear()