From ad5afcaae0b47e9e68b49aacf04cc3ad59d41a8e Mon Sep 17 00:00:00 2001 From: space-nuko <24979496+space-nuko@users.noreply.github.com> Date: Wed, 29 Mar 2023 16:46:03 -0500 Subject: Save/restore working webui/extension configs --- modules/ui_extensions.py | 186 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 185 insertions(+), 1 deletion(-) (limited to 'modules/ui_extensions.py') diff --git a/modules/ui_extensions.py b/modules/ui_extensions.py index efd6cda2..ed677b3e 100644 --- a/modules/ui_extensions.py +++ b/modules/ui_extensions.py @@ -2,6 +2,7 @@ import json import os.path import sys import time +from datetime import datetime import traceback import git @@ -11,7 +12,8 @@ import html import shutil import errno -from modules import extensions, shared, paths +from modules import extensions, shared, paths, config_states +from modules.paths_internal import config_states_dir from modules.call_queue import wrap_gradio_gpu_call available_extensions = {"extensions": []} @@ -30,6 +32,9 @@ def apply_and_restart(disable_list, update_list, disable_all): update = json.loads(update_list) assert type(update) == list, f"wrong update_list data for apply_and_restart: {update_list}" + if update: + save_config_state("Backup (pre-update)") + update = set(update) for ext in extensions.extensions: @@ -50,6 +55,48 @@ def apply_and_restart(disable_list, update_list, disable_all): shared.state.need_restart = True +def save_config_state(name): + current_config_state = config_states.get_config() + if not name: + name = "Config" + current_config_state["name"] = name + filename = os.path.join(config_states_dir, datetime.now().strftime("%Y_%m_%d-%H_%M_%S") + ".json") + print(f"Saving backup of webui/extension state to {filename}.") + with open(filename, "w", encoding="utf-8") as f: + json.dump(current_config_state, f) + config_states.list_config_states() + new_value = next(iter(config_states.all_config_states.keys()), "Current") + new_choices = ["Current"] + list(config_states.all_config_states.keys()) + return gr.Dropdown.update(value=new_value, choices=new_choices), f"Saved current webui/extension state to '{filename}'" + + +def restore_config_state(confirmed, config_state_name, restore_type): + if config_state_name == "Current": + return "Select a config to restore from." + if not confirmed: + return "Cancelled." + + check_access() + + save_config_state("Backup (pre-restore)") + + config_state = config_states.all_config_states[config_state_name] + + print(f"Restoring webui state from backup: {restore_type}") + + if restore_type == "extensions" or restore_type == "both": + shared.opts.restore_config_state_file = config_state["filename"] + shared.opts.save(shared.config_filename) + + if restore_type == "webui" or restore_type == "both": + config_states.restore_webui_config(config_state) + + shared.state.interrupt() + shared.state.need_restart = True + + return "" + + def check_updates(id_task, disable_list): check_access() @@ -121,6 +168,117 @@ def extension_table(): return code +def update_config_states_table(state_name): + if state_name == "Current": + config_state = config_states.get_config() + else: + config_state = config_states.all_config_states[state_name] + + config_name = config_state.get("name", "Config") + created_date = time.asctime(time.gmtime(config_state["created_at"])) + + code = f"""""" + + webui_remote = config_state["webui"]["remote"] or "" + webui_branch = config_state["webui"]["branch"] + webui_commit_hash = config_state["webui"]["commit_hash"] + if webui_commit_hash: + webui_commit_hash = webui_commit_hash[:8] + else: + webui_commit_hash = "" + webui_commit_date = config_state["webui"]["commit_date"] + if webui_commit_date: + webui_commit_date = time.asctime(time.gmtime(webui_commit_date)) + else: + webui_commit_date = "" + + code += f"""

Config Backup: {config_name}

+ Created at: {created_date}""" + + code += f"""

WebUI State

+ + + + + + + + + + + + + + + + + +
URLBranchCommitDate
{webui_remote}{webui_branch}{webui_commit_hash}{webui_commit_date}
+ """ + + code += """

Extension State

+ + + + + + + + + + + + """ + + ext_map = {ext.name: ext for ext in extensions.extensions} + + for ext_name, ext_conf in config_state["extensions"].items(): + ext_remote = ext_conf["remote"] or "" + ext_branch = ext_conf["branch"] or "" + ext_enabled = ext_conf["enabled"] + ext_commit_hash = ext_conf["commit_hash"] or "" + ext_commit_date = ext_conf["commit_date"] + if ext_commit_date: + ext_commit_date = time.asctime(time.gmtime(ext_commit_date)) + else: + ext_commit_date = "" + + remote = f"""{html.escape(ext_remote or '')}""" + + style_enabled = "" + style_remote = "" + style_branch = "" + style_commit = "" + if ext_name in ext_map: + current_ext = ext_map[ext_name] + current_ext.read_info_from_repo() + if current_ext.enabled != ext_enabled: + style_enabled = ' style="color: var(--primary-400)"' + if current_ext.remote != ext_remote: + style_remote = ' style="color: var(--primary-400)"' + if current_ext.branch != ext_branch: + style_branch = ' style="color: var(--primary-400)"' + if current_ext.commit_hash != ext_commit_hash: + style_commit = ' style="color: var(--primary-400)"' + + code += f""" + + + + + + + + """ + + code += """ + +
ExtensionURLBranchCommitDate
{html.escape(ext_name)}{remote}{ext_branch}{ext_commit_hash[:8]}{ext_commit_date}
+ """ + + return code + + def normalize_git_url(url): if url is None: return "" @@ -292,6 +450,8 @@ def refresh_available_extensions_from_data(hide_tags, sort_column, filter_text=" def create_ui(): import modules.ui + config_states.list_config_states() + with gr.Blocks(analytics_enabled=False) as ui: with gr.Tabs(elem_id="tabs_extensions") as tabs: with gr.TabItem("Installed"): @@ -386,4 +546,28 @@ def create_ui(): outputs=[extensions_table, install_result], ) + with gr.TabItem("Backup/Restore"): + with gr.Row(elem_id="extensions_backup_top_row"): + config_states_list = gr.Dropdown(label="Saved Configs", elem_id="extension_backup_saved_configs", value="Current", choices=["Current"] + list(config_states.all_config_states.keys())) + modules.ui.create_refresh_button(config_states_list, config_states.list_config_states, lambda: {"choices": ["Current"] + list(config_states.all_config_states.keys())}, "refresh_config_states") + config_restore_type = gr.Radio(label="State to restore", choices=["extensions", "webui", "both"], value="extensions", elem_id="extension_backup_restore_type") + config_restore_button = gr.Button(value="Restore Selected Config", variant="primary", elem_id="extension_backup_restore") + with gr.Row(elem_id="extensions_backup_top_row2"): + config_save_name = gr.Textbox("", placeholder="Config Name", show_label=False) + config_save_button = gr.Button(value="Save Current Config") + + config_states_info = gr.HTML("") + config_states_table = gr.HTML(lambda: update_config_states_table("Current")) + + config_save_button.click(fn=save_config_state, inputs=[config_save_name], outputs=[config_states_list, config_states_info]) + + dummy_component = gr.Label(visible=False) + config_restore_button.click(fn=restore_config_state, _js="config_state_confirm_restore", inputs=[dummy_component, config_states_list, config_restore_type], outputs=[config_states_info]) + + config_states_list.change( + fn=update_config_states_table, + inputs=[config_states_list], + outputs=[config_states_table], + ) + return ui -- cgit v1.2.3 From f3320b802c12f29e5a3201fcc0abfe72be294293 Mon Sep 17 00:00:00 2001 From: space-nuko <24979496+space-nuko@users.noreply.github.com> Date: Wed, 29 Mar 2023 18:32:54 -0500 Subject: Various UI fixes in config state tab --- modules/ui_extensions.py | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) (limited to 'modules/ui_extensions.py') diff --git a/modules/ui_extensions.py b/modules/ui_extensions.py index ed677b3e..b94b3a3a 100644 --- a/modules/ui_extensions.py +++ b/modules/ui_extensions.py @@ -17,6 +17,7 @@ from modules.paths_internal import config_states_dir from modules.call_queue import wrap_gradio_gpu_call available_extensions = {"extensions": []} +STYLE_PRIMARY = ' style="color: var(--primary-400)"' def check_access(): @@ -67,7 +68,7 @@ def save_config_state(name): config_states.list_config_states() new_value = next(iter(config_states.all_config_states.keys()), "Current") new_choices = ["Current"] + list(config_states.all_config_states.keys()) - return gr.Dropdown.update(value=new_value, choices=new_choices), f"Saved current webui/extension state to '{filename}'" + return gr.Dropdown.update(value=new_value, choices=new_choices), f"Saved current webui/extension state to \"{filename}\"" def restore_config_state(confirmed, config_state_name, restore_type): @@ -78,14 +79,12 @@ def restore_config_state(confirmed, config_state_name, restore_type): check_access() - save_config_state("Backup (pre-restore)") - config_state = config_states.all_config_states[config_state_name] - print(f"Restoring webui state from backup: {restore_type}") + print(f"*** Restoring webui state from backup: {restore_type} ***") if restore_type == "extensions" or restore_type == "both": - shared.opts.restore_config_state_file = config_state["filename"] + shared.opts.restore_config_state_file = config_state["filepath"] shared.opts.save(shared.config_filename) if restore_type == "webui" or restore_type == "both": @@ -149,7 +148,7 @@ def extension_table(): style = "" if shared.opts.disable_all_extensions == "extra" and not ext.is_builtin or shared.opts.disable_all_extensions == "all": - style = ' style="color: var(--primary-400)"' + style = STYLE_PRIMARY code += f""" @@ -181,17 +180,25 @@ def update_config_states_table(state_name): webui_remote = config_state["webui"]["remote"] or "" webui_branch = config_state["webui"]["branch"] - webui_commit_hash = config_state["webui"]["commit_hash"] - if webui_commit_hash: - webui_commit_hash = webui_commit_hash[:8] - else: - webui_commit_hash = "" + webui_commit_hash = config_state["webui"]["commit_hash"] or "" webui_commit_date = config_state["webui"]["commit_date"] if webui_commit_date: webui_commit_date = time.asctime(time.gmtime(webui_commit_date)) else: webui_commit_date = "" + current_webui = config_states.get_webui_config() + + style_remote = "" + style_branch = "" + style_commit = "" + if current_webui["remote"] != webui_remote: + style_remote = STYLE_PRIMARY + if current_webui["branch"] != webui_branch: + style_branch = STYLE_PRIMARY + if current_webui["commit_hash"] != webui_commit_hash: + style_commit = STYLE_PRIMARY + code += f"""

Config Backup: {config_name}

Created at: {created_date}""" @@ -207,10 +214,10 @@ def update_config_states_table(state_name): - {webui_remote} - {webui_branch} - {webui_commit_hash} - {webui_commit_date} + {webui_remote} + {webui_branch} + {webui_commit_hash[:8]} + {webui_commit_date} @@ -253,13 +260,13 @@ def update_config_states_table(state_name): current_ext = ext_map[ext_name] current_ext.read_info_from_repo() if current_ext.enabled != ext_enabled: - style_enabled = ' style="color: var(--primary-400)"' + style_enabled = STYLE_PRIMARY if current_ext.remote != ext_remote: - style_remote = ' style="color: var(--primary-400)"' + style_remote = STYLE_PRIMARY if current_ext.branch != ext_branch: - style_branch = ' style="color: var(--primary-400)"' + style_branch = STYLE_PRIMARY if current_ext.commit_hash != ext_commit_hash: - style_commit = ' style="color: var(--primary-400)"' + style_commit = STYLE_PRIMARY code += f""" -- cgit v1.2.3 From 9b1fa8298127b05c71c4de04bd6f64b72540ef5a Mon Sep 17 00:00:00 2001 From: space-nuko <24979496+space-nuko@users.noreply.github.com> Date: Wed, 29 Mar 2023 18:55:57 -0500 Subject: Add filename to UI and config name to filename --- modules/ui_extensions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'modules/ui_extensions.py') diff --git a/modules/ui_extensions.py b/modules/ui_extensions.py index b94b3a3a..b1ef38e2 100644 --- a/modules/ui_extensions.py +++ b/modules/ui_extensions.py @@ -61,7 +61,7 @@ def save_config_state(name): if not name: name = "Config" current_config_state["name"] = name - filename = os.path.join(config_states_dir, datetime.now().strftime("%Y_%m_%d-%H_%M_%S") + ".json") + filename = os.path.join(config_states_dir, datetime.now().strftime("%Y_%m_%d-%H_%M_%S") + "_" + name + ".json") print(f"Saving backup of webui/extension state to {filename}.") with open(filename, "w", encoding="utf-8") as f: json.dump(current_config_state, f) @@ -175,6 +175,7 @@ def update_config_states_table(state_name): config_name = config_state.get("name", "Config") created_date = time.asctime(time.gmtime(config_state["created_at"])) + filepath = config_state.get("filepath", "") code = f"""""" @@ -200,6 +201,7 @@ def update_config_states_table(state_name): style_commit = STYLE_PRIMARY code += f"""

Config Backup: {config_name}

+ Filepath: {filepath} Created at: {created_date}""" code += f"""

WebUI State

-- cgit v1.2.3 From 64bbd3bf030aac9068df6bbe79c852e97cf6dbde Mon Sep 17 00:00:00 2001 From: space-nuko <24979496+space-nuko@users.noreply.github.com> Date: Wed, 29 Mar 2023 19:00:51 -0500 Subject: Make into divs --- modules/ui_extensions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'modules/ui_extensions.py') diff --git a/modules/ui_extensions.py b/modules/ui_extensions.py index b1ef38e2..3735965c 100644 --- a/modules/ui_extensions.py +++ b/modules/ui_extensions.py @@ -201,8 +201,8 @@ def update_config_states_table(state_name): style_commit = STYLE_PRIMARY code += f"""

Config Backup: {config_name}

- Filepath: {filepath} - Created at: {created_date}""" +
Filepath: {filepath}
+
Created at: {created_date}
""" code += f"""

WebUI State

-- cgit v1.2.3 From 1c0544abdbffb910837d285857515b932a073f89 Mon Sep 17 00:00:00 2001 From: space-nuko <24979496+space-nuko@users.noreply.github.com> Date: Wed, 29 Mar 2023 19:21:57 -0500 Subject: Add links for commits in table, if remote is from GitHub --- modules/ui_extensions.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'modules/ui_extensions.py') diff --git a/modules/ui_extensions.py b/modules/ui_extensions.py index 3735965c..0843a7c0 100644 --- a/modules/ui_extensions.py +++ b/modules/ui_extensions.py @@ -122,6 +122,16 @@ def check_updates(id_task, disable_list): return extension_table(), "" +def make_commit_link(commit_hash, remote, text=None): + if text is None: + text = commit_hash[:8] + if remote.startswith("https://github.com/"): + href = os.path.join(remote, "commit", commit_hash) + return f'{text}' + else: + return text + + def extension_table(): code = f"""
@@ -150,11 +160,15 @@ def extension_table(): if shared.opts.disable_all_extensions == "extra" and not ext.is_builtin or shared.opts.disable_all_extensions == "all": style = STYLE_PRIMARY + version_link = ext.version + if ext.commit_hash and ext.remote: + version_link = make_commit_link(ext.commit_hash, ext.remote, ext.version) + code += f""" - + {ext_status} """ @@ -200,6 +214,9 @@ def update_config_states_table(state_name): if current_webui["commit_hash"] != webui_commit_hash: style_commit = STYLE_PRIMARY + commit_link = make_commit_link(webui_commit_hash, webui_remote) + date_link = make_commit_link(webui_commit_hash, webui_remote, webui_commit_date) + code += f"""

Config Backup: {config_name}

Filepath: {filepath}
Created at: {created_date}
""" @@ -218,8 +235,8 @@ def update_config_states_table(state_name): - - + +
{html.escape(ext.name)} {remote}{ext.version}{version_link}
{webui_remote} {webui_branch}{webui_commit_hash[:8]}{webui_commit_date}{commit_link}{date_link}
@@ -270,13 +287,16 @@ def update_config_states_table(state_name): if current_ext.commit_hash != ext_commit_hash: style_commit = STYLE_PRIMARY + commit_link = make_commit_link(ext_commit_hash, ext_remote) + date_link = make_commit_link(ext_commit_hash, ext_remote, ext_commit_date) + code += f""" {html.escape(ext_name)} {remote} {ext_branch} - {ext_commit_hash[:8]} - {ext_commit_date} + {commit_link} + {date_link} """ -- cgit v1.2.3 From 3ccf6f5ae8e57233200f9cac52da8d5d88ecee93 Mon Sep 17 00:00:00 2001 From: space-nuko <24979496+space-nuko@users.noreply.github.com> Date: Wed, 29 Mar 2023 19:26:52 -0500 Subject: Add webui link --- modules/ui_extensions.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'modules/ui_extensions.py') diff --git a/modules/ui_extensions.py b/modules/ui_extensions.py index 0843a7c0..af9e92cc 100644 --- a/modules/ui_extensions.py +++ b/modules/ui_extensions.py @@ -202,6 +202,10 @@ def update_config_states_table(state_name): else: webui_commit_date = "" + remote = f"""{html.escape(webui_remote or '')}""" + commit_link = make_commit_link(webui_commit_hash, webui_remote) + date_link = make_commit_link(webui_commit_hash, webui_remote, webui_commit_date) + current_webui = config_states.get_webui_config() style_remote = "" @@ -214,9 +218,6 @@ def update_config_states_table(state_name): if current_webui["commit_hash"] != webui_commit_hash: style_commit = STYLE_PRIMARY - commit_link = make_commit_link(webui_commit_hash, webui_remote) - date_link = make_commit_link(webui_commit_hash, webui_remote, webui_commit_date) - code += f"""

Config Backup: {config_name}

Filepath: {filepath}
Created at: {created_date}
""" @@ -233,7 +234,7 @@ def update_config_states_table(state_name): - {webui_remote} + {remote} {webui_branch} {commit_link} {date_link} @@ -270,6 +271,8 @@ def update_config_states_table(state_name): ext_commit_date = "" remote = f"""{html.escape(ext_remote or '')}""" + commit_link = make_commit_link(ext_commit_hash, ext_remote) + date_link = make_commit_link(ext_commit_hash, ext_remote, ext_commit_date) style_enabled = "" style_remote = "" @@ -287,9 +290,6 @@ def update_config_states_table(state_name): if current_ext.commit_hash != ext_commit_hash: style_commit = STYLE_PRIMARY - commit_link = make_commit_link(ext_commit_hash, ext_remote) - date_link = make_commit_link(ext_commit_hash, ext_remote, ext_commit_date) - code += f""" {html.escape(ext_name)} -- cgit v1.2.3 From 1aba8d82cbb816a755d012c5c729d8bafeb1b8ed Mon Sep 17 00:00:00 2001 From: yike5460 Date: Sun, 9 Apr 2023 22:22:43 +0800 Subject: feat: add branch support for extension installation --- modules/ui_extensions.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'modules/ui_extensions.py') diff --git a/modules/ui_extensions.py b/modules/ui_extensions.py index efd6cda2..d9487f83 100644 --- a/modules/ui_extensions.py +++ b/modules/ui_extensions.py @@ -129,7 +129,7 @@ def normalize_git_url(url): return url -def install_extension_from_url(dirname, url): +def install_extension_from_url(dirname, branch_name, url): check_access() assert url, 'No URL specified' @@ -150,7 +150,7 @@ def install_extension_from_url(dirname, url): try: shutil.rmtree(tmpdir, True) - with git.Repo.clone_from(url, tmpdir) as repo: + with git.Repo.clone_from(url, tmpdir, branch=branch_name if branch_name else '') as repo: repo.remote().fetch() for submodule in repo.submodules: submodule.update() @@ -376,13 +376,14 @@ def create_ui(): with gr.TabItem("Install from URL"): install_url = gr.Text(label="URL for extension's git repository") + install_branch = gr.Text(label="Branch name for extension's git repository", placeholder="Leave empty for default branch") install_dirname = gr.Text(label="Local directory name", placeholder="Leave empty for auto") install_button = gr.Button(value="Install", variant="primary") install_result = gr.HTML(elem_id="extension_install_result") install_button.click( fn=modules.ui.wrap_gradio_call(install_extension_from_url, extra_outputs=[gr.update()]), - inputs=[install_dirname, install_url], + inputs=[install_dirname, install_branch, install_url], outputs=[extensions_table, install_result], ) -- cgit v1.2.3 From 7c62bb2788d9cec10bab9d0154bd24f3658f7a83 Mon Sep 17 00:00:00 2001 From: yike5460 Date: Mon, 10 Apr 2023 09:38:26 +0800 Subject: fix: support for default branch --- modules/ui_extensions.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'modules/ui_extensions.py') diff --git a/modules/ui_extensions.py b/modules/ui_extensions.py index d9487f83..b402bc8b 100644 --- a/modules/ui_extensions.py +++ b/modules/ui_extensions.py @@ -150,10 +150,17 @@ def install_extension_from_url(dirname, branch_name, url): try: shutil.rmtree(tmpdir, True) - with git.Repo.clone_from(url, tmpdir, branch=branch_name if branch_name else '') as repo: - repo.remote().fetch() - for submodule in repo.submodules: - submodule.update() + if branch_name == '': + # if no branch is specified, use the default branch + with git.Repo.clone_from(url, tmpdir) as repo: + repo.remote().fetch() + for submodule in repo.submodules: + submodule.update() + else: + with git.Repo.clone_from(url, tmpdir, branch=branch_name) as repo: + repo.remote().fetch() + for submodule in repo.submodules: + submodule.update() try: os.rename(tmpdir, target_dir) except OSError as err: @@ -376,7 +383,7 @@ def create_ui(): with gr.TabItem("Install from URL"): install_url = gr.Text(label="URL for extension's git repository") - install_branch = gr.Text(label="Branch name for extension's git repository", placeholder="Leave empty for default branch") + install_branch = gr.Text(label="Specific branch name", placeholder="Leave empty for default main branch") install_dirname = gr.Text(label="Local directory name", placeholder="Leave empty for auto") install_button = gr.Button(value="Install", variant="primary") install_result = gr.HTML(elem_id="extension_install_result") -- cgit v1.2.3 From cb940a583d909f5f144ffc6e4ad83a319620df69 Mon Sep 17 00:00:00 2001 From: AUTOMATIC <16777216c@gmail.com> Date: Sat, 29 Apr 2023 13:45:05 +0300 Subject: fix extension installation broken by #9518 --- modules/ui_extensions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'modules/ui_extensions.py') diff --git a/modules/ui_extensions.py b/modules/ui_extensions.py index b402bc8b..e90bedc8 100644 --- a/modules/ui_extensions.py +++ b/modules/ui_extensions.py @@ -129,7 +129,7 @@ def normalize_git_url(url): return url -def install_extension_from_url(dirname, branch_name, url): +def install_extension_from_url(dirname, url, branch_name=None): check_access() assert url, 'No URL specified' @@ -150,7 +150,7 @@ def install_extension_from_url(dirname, branch_name, url): try: shutil.rmtree(tmpdir, True) - if branch_name == '': + if not branch_name: # if no branch is specified, use the default branch with git.Repo.clone_from(url, tmpdir) as repo: repo.remote().fetch() @@ -390,7 +390,7 @@ def create_ui(): install_button.click( fn=modules.ui.wrap_gradio_call(install_extension_from_url, extra_outputs=[gr.update()]), - inputs=[install_dirname, install_branch, install_url], + inputs=[install_dirname, install_url, install_branch], outputs=[extensions_table, install_result], ) -- cgit v1.2.3