Skip to content

Commit 4dd898b

Browse files
committed
do not mess with components' visibility for scripts; instead create group components and show/hide those; this will break scripts that create invisible components and rely on UI but the earlier i make this change the better
1 parent 59a21a6 commit 4dd898b

File tree

7 files changed

+33
-31
lines changed

7 files changed

+33
-31
lines changed

modules/scripts.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class Script:
1818
args_to = None
1919
alwayson = False
2020

21+
"""A gr.Group component that has all script's UI inside it"""
22+
group = None
23+
2124
infotext_fields = None
2225
"""if set in ui(), this is a list of pairs of gradio component + text; the text will be used when
2326
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):
218221

219222
for control in controls:
220223
control.custom_script_source = os.path.basename(script.filename)
221-
if not script.alwayson:
222-
control.visible = False
223224

224225
if script.infotext_fields is not None:
225226
self.infotext_fields += script.infotext_fields
@@ -229,40 +230,41 @@ def create_script_ui(script, inputs, inputs_alwayson):
229230
script.args_to = len(inputs)
230231

231232
for script in self.alwayson_scripts:
232-
with gr.Group():
233+
with gr.Group() as group:
233234
create_script_ui(script, inputs, inputs_alwayson)
234235

236+
script.group = group
237+
235238
dropdown = gr.Dropdown(label="Script", elem_id="script_list", choices=["None"] + self.titles, value="None", type="index")
236239
dropdown.save_to_config = True
237240
inputs[0] = dropdown
238241

239242
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
241247

242248
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
250250

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]
252252

253253
def init_field(title):
254+
"""called when an initial value is set from ui-config.json to show script's UI components"""
255+
254256
if title == 'None':
255257
return
258+
256259
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
260261

261262
dropdown.init_field = init_field
263+
262264
dropdown.change(
263265
fn=select_script,
264266
inputs=[dropdown],
265-
outputs=inputs
267+
outputs=[script.group for script in self.selectable_scripts]
266268
)
267269

268270
return inputs

scripts/custom_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def show(self, is_img2img):
1414
return cmd_opts.allow_code
1515

1616
def ui(self, is_img2img):
17-
code = gr.Textbox(label="Python code", visible=False, lines=1)
17+
code = gr.Textbox(label="Python code", lines=1)
1818

1919
return [code]
2020

scripts/outpainting_mk_2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def ui(self, is_img2img):
132132
info = gr.HTML("<p style=\"margin-bottom:0.75em\">Recommended settings: Sampling Steps: 80-100, Sampler: Euler a, Denoising strength: 0.8</p>")
133133

134134
pixels = gr.Slider(label="Pixels to expand", minimum=8, maximum=256, step=8, value=128)
135-
mask_blur = gr.Slider(label='Mask blur', minimum=0, maximum=64, step=1, value=8, visible=False)
135+
mask_blur = gr.Slider(label='Mask blur', minimum=0, maximum=64, step=1, value=8)
136136
direction = gr.CheckboxGroup(label="Outpainting direction", choices=['left', 'right', 'up', 'down'], value=['left', 'right', 'up', 'down'])
137137
noise_q = gr.Slider(label="Fall-off exponent (lower=higher detail)", minimum=0.0, maximum=4.0, step=0.01, value=1.0)
138138
color_variation = gr.Slider(label="Color variation", minimum=0.0, maximum=1.0, step=0.01, value=0.05)

scripts/poor_mans_outpainting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def ui(self, is_img2img):
2222
return None
2323

2424
pixels = gr.Slider(label="Pixels to expand", minimum=8, maximum=256, step=8, value=128)
25-
mask_blur = gr.Slider(label='Mask blur', minimum=0, maximum=64, step=1, value=4, visible=False)
26-
inpainting_fill = gr.Radio(label='Masked content', choices=['fill', 'original', 'latent noise', 'latent nothing'], value='fill', type="index", visible=False)
25+
mask_blur = gr.Slider(label='Mask blur', minimum=0, maximum=64, step=1, value=4)
26+
inpainting_fill = gr.Radio(label='Masked content', choices=['fill', 'original', 'latent noise', 'latent nothing'], value='fill', type="index")
2727
direction = gr.CheckboxGroup(label="Outpainting direction", choices=['left', 'right', 'up', 'down'], value=['left', 'right', 'up', 'down'])
2828

2929
return [pixels, mask_blur, inpainting_fill, direction]

scripts/prompts_from_file.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ def cmdargs(line):
8383

8484

8585
def load_prompt_file(file):
86-
if (file is None):
86+
if file is None:
8787
lines = []
8888
else:
8989
lines = [x.strip() for x in file.decode('utf8', errors='ignore').split("\n")]
9090

9191
return None, "\n".join(lines), gr.update(lines=7)
9292

93+
9394
class Script(scripts.Script):
9495
def title(self):
9596
return "Prompts from file or textbox"
@@ -107,9 +108,9 @@ def ui(self, is_img2img):
107108
# We don't shrink back to 1, because that causes the control to ignore [enter], and it may
108109
# be unclear to the user that shift-enter is needed.
109110
prompt_txt.change(lambda tb: gr.update(lines=7) if ("\n" in tb) else gr.update(lines=2), inputs=[prompt_txt], outputs=[prompt_txt])
110-
return [checkbox_iterate, checkbox_iterate_batch, file, prompt_txt]
111+
return [checkbox_iterate, checkbox_iterate_batch, prompt_txt]
111112

112-
def run(self, p, checkbox_iterate, checkbox_iterate_batch, file, prompt_txt: str):
113+
def run(self, p, checkbox_iterate, checkbox_iterate_batch, prompt_txt: str):
113114
lines = [x.strip() for x in prompt_txt.splitlines()]
114115
lines = [x for x in lines if len(x) > 0]
115116

@@ -157,5 +158,4 @@ def run(self, p, checkbox_iterate, checkbox_iterate_batch, file, prompt_txt: str
157158
if checkbox_iterate:
158159
p.seed = p.seed + (p.batch_size * p.n_iter)
159160

160-
161-
return Processed(p, images, p.seed, "")
161+
return Processed(p, images, p.seed, "")

scripts/sd_upscale.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def show(self, is_img2img):
1818

1919
def ui(self, is_img2img):
2020
info = gr.HTML("<p style=\"margin-bottom:0.75em\">Will upscale the image to twice the dimensions; use width and height sliders to set tile size</p>")
21-
overlap = gr.Slider(minimum=0, maximum=256, step=16, label='Tile overlap', value=64, visible=False)
22-
upscaler_index = gr.Radio(label='Upscaler', choices=[x.name for x in shared.sd_upscalers], value=shared.sd_upscalers[0].name, type="index", visible=False)
21+
overlap = gr.Slider(minimum=0, maximum=256, step=16, label='Tile overlap', value=64)
22+
upscaler_index = gr.Radio(label='Upscaler', choices=[x.name for x in shared.sd_upscalers], value=shared.sd_upscalers[0].name, type="index")
2323

2424
return [info, overlap, upscaler_index]
2525

scripts/xy_grid.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,12 @@ def ui(self, is_img2img):
263263
current_axis_options = [x for x in axis_options if type(x) == AxisOption or type(x) == AxisOptionImg2Img and is_img2img]
264264

265265
with gr.Row():
266-
x_type = gr.Dropdown(label="X type", choices=[x.label for x in current_axis_options], value=current_axis_options[1].label, visible=False, type="index", elem_id="x_type")
267-
x_values = gr.Textbox(label="X values", visible=False, lines=1)
266+
x_type = gr.Dropdown(label="X type", choices=[x.label for x in current_axis_options], value=current_axis_options[1].label, type="index", elem_id="x_type")
267+
x_values = gr.Textbox(label="X values", lines=1)
268268

269269
with gr.Row():
270-
y_type = gr.Dropdown(label="Y type", choices=[x.label for x in current_axis_options], value=current_axis_options[0].label, visible=False, type="index", elem_id="y_type")
271-
y_values = gr.Textbox(label="Y values", visible=False, lines=1)
270+
y_type = gr.Dropdown(label="Y type", choices=[x.label for x in current_axis_options], value=current_axis_options[0].label, type="index", elem_id="y_type")
271+
y_values = gr.Textbox(label="Y values", lines=1)
272272

273273
draw_legend = gr.Checkbox(label='Draw legend', value=True)
274274
include_lone_images = gr.Checkbox(label='Include Separate Images', value=False)

0 commit comments

Comments
 (0)