Skip to content

Commit 910a097

Browse files
committed
add initial version of the extensions tab
fix broken Restart Gradio button
1 parent 9b384df commit 910a097

File tree

9 files changed

+333
-30
lines changed

9 files changed

+333
-30
lines changed

javascript/extensions.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
function extensions_apply(_, _){
3+
disable = []
4+
update = []
5+
gradioApp().querySelectorAll('#extensions input[type="checkbox"]').forEach(function(x){
6+
if(x.name.startsWith("enable_") && ! x.checked)
7+
disable.push(x.name.substr(7))
8+
9+
if(x.name.startsWith("update_") && x.checked)
10+
update.push(x.name.substr(7))
11+
})
12+
13+
restart_reload()
14+
15+
return [JSON.stringify(disable), JSON.stringify(update)]
16+
}
17+
18+
function extensions_check(){
19+
gradioApp().querySelectorAll('#extensions .extension_status').forEach(function(x){
20+
x.innerHTML = "Loading..."
21+
})
22+
23+
return []
24+
}

modules/extensions.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import os
2+
import sys
3+
import traceback
4+
5+
import git
6+
7+
from modules import paths, shared
8+
9+
10+
extensions = []
11+
extensions_dir = os.path.join(paths.script_path, "extensions")
12+
13+
14+
def active():
15+
return [x for x in extensions if x.enabled]
16+
17+
18+
class Extension:
19+
def __init__(self, name, path, enabled=True):
20+
self.name = name
21+
self.path = path
22+
self.enabled = enabled
23+
self.status = ''
24+
self.can_update = False
25+
26+
repo = None
27+
try:
28+
if os.path.exists(os.path.join(path, ".git")):
29+
repo = git.Repo(path)
30+
except Exception:
31+
print(f"Error reading github repository info from {path}:", file=sys.stderr)
32+
print(traceback.format_exc(), file=sys.stderr)
33+
34+
if repo is None or repo.bare:
35+
self.remote = None
36+
else:
37+
self.remote = next(repo.remote().urls, None)
38+
self.status = 'unknown'
39+
40+
def list_files(self, subdir, extension):
41+
from modules import scripts
42+
43+
dirpath = os.path.join(self.path, subdir)
44+
if not os.path.isdir(dirpath):
45+
return []
46+
47+
res = []
48+
for filename in sorted(os.listdir(dirpath)):
49+
res.append(scripts.ScriptFile(dirpath, filename, os.path.join(dirpath, filename)))
50+
51+
res = [x for x in res if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]
52+
53+
return res
54+
55+
def check_updates(self):
56+
repo = git.Repo(self.path)
57+
for fetch in repo.remote().fetch("--dry-run"):
58+
if fetch.flags != fetch.HEAD_UPTODATE:
59+
self.can_update = True
60+
self.status = "behind"
61+
return
62+
63+
self.can_update = False
64+
self.status = "latest"
65+
66+
def pull(self):
67+
repo = git.Repo(self.path)
68+
repo.remotes.origin.pull()
69+
70+
71+
def list_extensions():
72+
extensions.clear()
73+
74+
if not os.path.isdir(extensions_dir):
75+
return
76+
77+
for dirname in sorted(os.listdir(extensions_dir)):
78+
path = os.path.join(extensions_dir, dirname)
79+
if not os.path.isdir(path):
80+
continue
81+
82+
extension = Extension(name=dirname, path=path, enabled=dirname not in shared.opts.disabled_extensions)
83+
extensions.append(extension)

modules/generation_parameters_copypaste.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
bind_list = []
1818

1919

20+
def reset():
21+
paste_fields.clear()
22+
bind_list.clear()
23+
24+
2025
def quote(text):
2126
if ',' not in str(text):
2227
return text

modules/scripts.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import gradio as gr
88

99
from modules.processing import StableDiffusionProcessing
10-
from modules import shared, paths, script_callbacks
10+
from modules import shared, paths, script_callbacks, extensions
1111

1212
AlwaysVisible = object()
1313

@@ -107,17 +107,8 @@ def list_scripts(scriptdirname, extension):
107107
for filename in sorted(os.listdir(basedir)):
108108
scripts_list.append(ScriptFile(paths.script_path, filename, os.path.join(basedir, filename)))
109109

110-
extdir = os.path.join(paths.script_path, "extensions")
111-
if os.path.exists(extdir):
112-
for dirname in sorted(os.listdir(extdir)):
113-
dirpath = os.path.join(extdir, dirname)
114-
scriptdirpath = os.path.join(dirpath, scriptdirname)
115-
116-
if not os.path.isdir(scriptdirpath):
117-
continue
118-
119-
for filename in sorted(os.listdir(scriptdirpath)):
120-
scripts_list.append(ScriptFile(dirpath, filename, os.path.join(scriptdirpath, filename)))
110+
for ext in extensions.active():
111+
scripts_list += ext.list_files(scriptdirname, extension)
121112

122113
scripts_list = [x for x in scripts_list if os.path.splitext(x.path)[1].lower() == extension and os.path.isfile(x.path)]
123114

@@ -127,11 +118,7 @@ def list_scripts(scriptdirname, extension):
127118
def list_files_with_name(filename):
128119
res = []
129120

130-
dirs = [paths.script_path]
131-
132-
extdir = os.path.join(paths.script_path, "extensions")
133-
if os.path.exists(extdir):
134-
dirs += [os.path.join(extdir, d) for d in sorted(os.listdir(extdir))]
121+
dirs = [paths.script_path] + [ext.path for ext in extensions.active()]
135122

136123
for dirpath in dirs:
137124
if not os.path.isdir(dirpath):

modules/shared.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class State:
132132
current_image = None
133133
current_image_sampling_step = 0
134134
textinfo = None
135+
need_restart = False
135136

136137
def skip(self):
137138
self.skipped = True
@@ -354,6 +355,12 @@ def options_section(section_identifier, options_dict):
354355
'eta_noise_seed_delta': OptionInfo(0, "Eta noise seed delta", gr.Number, {"precision": 0}),
355356
}))
356357

358+
options_templates.update(options_section((None, "Hidden options"), {
359+
"disabled_extensions": OptionInfo([], "Disable those extensions"),
360+
}))
361+
362+
options_templates.update()
363+
357364

358365
class Options:
359366
data = None
@@ -365,8 +372,9 @@ def __init__(self):
365372

366373
def __setattr__(self, key, value):
367374
if self.data is not None:
368-
if key in self.data:
375+
if key in self.data or key in self.data_labels:
369376
self.data[key] = value
377+
return
370378

371379
return super(Options, self).__setattr__(key, value)
372380

modules/ui.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from PIL import Image, PngImagePlugin
2020

2121

22-
from modules import sd_hijack, sd_models, localization, script_callbacks
22+
from modules import sd_hijack, sd_models, localization, script_callbacks, ui_extensions
2323
from modules.paths import script_path
2424

2525
from modules.shared import opts, cmd_opts, restricted_opts
@@ -671,6 +671,7 @@ def create_ui(wrap_gradio_gpu_call):
671671
import modules.img2img
672672
import modules.txt2img
673673

674+
parameters_copypaste.reset()
674675

675676
with gr.Blocks(analytics_enabled=False) as txt2img_interface:
676677
txt2img_prompt, roll, txt2img_prompt_style, txt2img_negative_prompt, txt2img_prompt_style2, submit, _, _, txt2img_prompt_style_apply, txt2img_save_style, txt2img_paste, token_counter, token_button = create_toprow(is_img2img=False)
@@ -1511,8 +1512,9 @@ def run_settings_single(value, key):
15111512
column = None
15121513
with gr.Row(elem_id="settings").style(equal_height=False):
15131514
for i, (k, item) in enumerate(opts.data_labels.items()):
1515+
section_must_be_skipped = item.section[0] is None
15141516

1515-
if previous_section != item.section:
1517+
if previous_section != item.section and not section_must_be_skipped:
15161518
if cols_displayed < settings_cols and (items_displayed >= items_per_col or previous_section is None):
15171519
if column is not None:
15181520
column.__exit__()
@@ -1531,6 +1533,8 @@ def run_settings_single(value, key):
15311533
if k in quicksettings_names and not shared.cmd_opts.freeze_settings:
15321534
quicksettings_list.append((i, k, item))
15331535
components.append(dummy_component)
1536+
elif section_must_be_skipped:
1537+
components.append(dummy_component)
15341538
else:
15351539
component = create_setting_component(k)
15361540
component_dict[k] = component
@@ -1572,9 +1576,10 @@ def reload_scripts():
15721576

15731577
def request_restart():
15741578
shared.state.interrupt()
1575-
settings_interface.gradio_ref.do_restart = True
1579+
shared.state.need_restart = True
15761580

15771581
restart_gradio.click(
1582+
15781583
fn=request_restart,
15791584
inputs=[],
15801585
outputs=[],
@@ -1612,14 +1617,15 @@ def request_restart():
16121617
interfaces += script_callbacks.ui_tabs_callback()
16131618
interfaces += [(settings_interface, "Settings", "settings")]
16141619

1620+
extensions_interface = ui_extensions.create_ui()
1621+
interfaces += [(extensions_interface, "Extensions", "extensions")]
1622+
16151623
with gr.Blocks(css=css, analytics_enabled=False, title="Stable Diffusion") as demo:
16161624
with gr.Row(elem_id="quicksettings"):
16171625
for i, k, item in quicksettings_list:
16181626
component = create_setting_component(k, is_quicksettings=True)
16191627
component_dict[k] = component
16201628

1621-
settings_interface.gradio_ref = demo
1622-
16231629
parameters_copypaste.integrate_settings_paste_fields(component_dict)
16241630
parameters_copypaste.run_bind()
16251631

0 commit comments

Comments
 (0)