@@ -18,6 +18,9 @@ class Script:
18
18
args_to = None
19
19
alwayson = False
20
20
21
+ """A gr.Group component that has all script's UI inside it"""
22
+ group = None
23
+
21
24
infotext_fields = None
22
25
"""if set in ui(), this is a list of pairs of gradio component + text; the text will be used when
23
26
parsing infotext to set the value for the component; see ui.py's txt2img_paste_fields for an example
@@ -218,8 +221,6 @@ def create_script_ui(script, inputs, inputs_alwayson):
218
221
219
222
for control in controls :
220
223
control .custom_script_source = os .path .basename (script .filename )
221
- if not script .alwayson :
222
- control .visible = False
223
224
224
225
if script .infotext_fields is not None :
225
226
self .infotext_fields += script .infotext_fields
@@ -229,40 +230,41 @@ def create_script_ui(script, inputs, inputs_alwayson):
229
230
script .args_to = len (inputs )
230
231
231
232
for script in self .alwayson_scripts :
232
- with gr .Group ():
233
+ with gr .Group () as group :
233
234
create_script_ui (script , inputs , inputs_alwayson )
234
235
236
+ script .group = group
237
+
235
238
dropdown = gr .Dropdown (label = "Script" , elem_id = "script_list" , choices = ["None" ] + self .titles , value = "None" , type = "index" )
236
239
dropdown .save_to_config = True
237
240
inputs [0 ] = dropdown
238
241
239
242
for script in self .selectable_scripts :
240
- create_script_ui (script , inputs , inputs_alwayson )
243
+ with gr .Group (visible = False ) as group :
244
+ create_script_ui (script , inputs , inputs_alwayson )
245
+
246
+ script .group = group
241
247
242
248
def select_script (script_index ):
243
- if 0 < script_index <= len (self .selectable_scripts ):
244
- script = self .selectable_scripts [script_index - 1 ]
245
- args_from = script .args_from
246
- args_to = script .args_to
247
- else :
248
- args_from = 0
249
- args_to = 0
249
+ selected_script = self .selectable_scripts [script_index - 1 ] if script_index > 0 else None
250
250
251
- return [ui . gr_show ( True if i == 0 else args_from <= i < args_to or is_alwayson ) for i , is_alwayson in enumerate ( inputs_alwayson ) ]
251
+ return [gr . update ( visible = selected_script == s ) for s in self . selectable_scripts ]
252
252
253
253
def init_field (title ):
254
+ """called when an initial value is set from ui-config.json to show script's UI components"""
255
+
254
256
if title == 'None' :
255
257
return
258
+
256
259
script_index = self .titles .index (title )
257
- script = self .selectable_scripts [script_index ]
258
- for i in range (script .args_from , script .args_to ):
259
- inputs [i ].visible = True
260
+ self .selectable_scripts [script_index ].group .visible = True
260
261
261
262
dropdown .init_field = init_field
263
+
262
264
dropdown .change (
263
265
fn = select_script ,
264
266
inputs = [dropdown ],
265
- outputs = inputs
267
+ outputs = [ script . group for script in self . selectable_scripts ]
266
268
)
267
269
268
270
return inputs
0 commit comments