Skip to content

Commit

Permalink
Added confirmation prompt when deleting wildcards
Browse files Browse the repository at this point in the history
Also deletion moves files to Trash using the send2trash library
  • Loading branch information
adieyal committed Dec 24, 2022
1 parent bb0c425 commit 601871b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 49 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@
1.4.0 Added option to weight prompts in combinationselector - thanks to @shoffing
1.4.1 Fixed sub-prompts in combination - see https://github.com/adieyal/sd-dynamic-prompts/issues/117
1.4.2 The change for 1.4.1 broke the ability to use wildcards in combination braces, e.g. {2$$__colours__}, 1.4.2. fixes this.
1.4.3 Added animal wildcards to the jumbo collection
1.4.3 Added animal wildcards to the jumbo collection
1.4.4 Added confirm prompt when deleting wildcards. Wildcards are also sent to Trash instead of being deleted outright
2 changes: 2 additions & 0 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
if not launch.is_installed("spacy"):
launch.run_pip("install spacy==3.0.8", desc='spacy==3.0.8')
launch.run_pip("install https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.4.1/en_core_web_sm-3.4.1.tar.gz", desc='Installing en_core_web_sm==3.4.1')
if not launch.is_installed("Send2Trash"):
launch.run_pip("install Send2Trash==1.8.0", desc='Send2Trash==1.8.0')
1 change: 0 additions & 1 deletion javascript/dynamic_prompting.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class SDDPUI {
}

deleteTree() {
return true;
let response = confirm("Are you sure you want to delete all your wildcards?")
return response
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/dynamic_prompting.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
else:
WILDCARD_DIR = Path(wildcard_dir)

VERSION = "1.4.3"
VERSION = "1.4.4"


wildcard_manager = WildcardManager(WILDCARD_DIR)
Expand Down
119 changes: 73 additions & 46 deletions ui/wildcards_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from pathlib import Path
from glob import glob

from send2trash import send2trash

from prompts.wildcardmanager import WildcardManager

logger = logging.getLogger(__name__)
Expand All @@ -18,6 +20,7 @@

BASE_DIR = scripts.basedir()


def initialize(manager: WildcardManager):
global tree_json
global wildcard_manager
Expand All @@ -26,11 +29,13 @@ def initialize(manager: WildcardManager):
tree_json = load_hierarchy()
script_callbacks.on_ui_tabs(on_ui_tabs)


def load_hierarchy():
hierarchy = wildcard_manager.get_wildcard_hierarchy()
tree_json = format_json(hierarchy)
return tree_json


def format_json(js):
if js is None:
return []
Expand Down Expand Up @@ -67,39 +72,66 @@ def on_ui_tabs():
with gr.Column():
gr.HTML(header_html)
html = gr.HTML("", elem_id="html_id")
collection_dropdown = gr.Dropdown(choices=available_collections, type="value", label="Select a collection", elem_id="collection_dropdown")
collection_dropdown = gr.Dropdown(
choices=available_collections,
type="value",
label="Select a collection",
elem_id="collection_dropdown",
)
with gr.Row():
collection_copy_button = gr.Button("Copy collection", full_width=True, elem_id="collection_copy_button")
overwrite_checkbox = gr.Checkbox(label="Overwrite existing", elem_id="overwrite_checkbox", value=False)
collection_copy_button = gr.Button(
"Copy collection",
full_width=True,
elem_id="collection_copy_button",
)
overwrite_checkbox = gr.Checkbox(
label="Overwrite existing",
elem_id="overwrite_checkbox",
value=False,
)
with gr.Row():
load_tree = gr.Button("Refresh wildcards", elem_id="load_tree_button")
delete_tree = gr.Button("Delete all wildcards", elem_id="delete_tree_button")
load_tree = gr.Button(
"Refresh wildcards", elem_id="load_tree_button"
)
delete_tree = gr.Button(
"Delete all wildcards", elem_id="delete_tree_button"
)
with gr.Column():
file_name = gr.Textbox(
"", elem_id="file_name_id", interactive=False, label="Wildcards file"
"",
elem_id="file_name_id",
interactive=False,
label="Wildcards file",
)
file_edit_box = gr.Textbox(
"", elem_id="file_edit_box_id", lines=10, interactive=True, label="File editor"
"",
elem_id="file_edit_box_id",
lines=10,
interactive=True,
label="File editor",
)
save_button = gr.Button("Save wildcards", full_width=True)

hidden_hierarchy = gr.Textbox(
json.dumps(tree_json), elem_id="tree_textbox", visible=False
)
hidden_textbox = gr.Textbox(
"", elem_id="scratch_textbox", visible=False
)
hidden_textbox = gr.Textbox("", elem_id="scratch_textbox", visible=False)

hidden_action_button = gr.Button(
"Action", elem_id="action_button", visible=False
)

load_tree.click(
load_tree_callback, inputs=[], outputs=[hidden_textbox],
load_tree_callback,
inputs=[],
outputs=[hidden_textbox],
)

delete_tree.click(
delete_tree_callback, _js="deleteTree", inputs=[], outputs=[hidden_textbox],
delete_tree_callback,
_js="deleteTree",
inputs=[hidden_textbox],
outputs=[hidden_textbox],
)

hidden_action_button.click(
Expand All @@ -110,22 +142,31 @@ def on_ui_tabs():
)

save_button.click(
save_file_callback, _js="saveFile", inputs=[file_edit_box], outputs=[hidden_textbox]
save_file_callback,
_js="saveFile",
inputs=[file_edit_box],
outputs=[hidden_textbox],
)

collection_copy_button.click(
copy_collection_callback, inputs=[overwrite_checkbox, collection_dropdown], outputs=[hidden_textbox]
copy_collection_callback,
inputs=[overwrite_checkbox, collection_dropdown],
outputs=[hidden_textbox],
)

return ((wildcards_tab, "Wildcards Manager", "wildcards_tab"),)


def create_payload(action, result, payload):
return json.dumps({
"action": action,
"result": result,
"payload": payload,
"id": random.randint(0, 1000000),
})
return json.dumps(
{
"action": action,
"result": result,
"payload": payload,
"id": random.randint(0, 1000000),
}
)


def copy_collection_callback(overwrite_checkbox, collection):
collection_paths = wildcard_manager.get_collection_dirs()
Expand All @@ -137,41 +178,28 @@ def copy_collection_callback(overwrite_checkbox, collection):
if not target_path.exists() or overwrite_checkbox:
target_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(file, target_path)

return load_tree_callback()

return create_payload(
"copy collection",
"failed",
json.dumps([])
)
return create_payload("copy collection", "failed", json.dumps([]))


def load_tree_callback():
hierarchy = load_hierarchy()

return create_payload(
"load tree",
"success",
json.dumps(hierarchy)
)
return create_payload("load tree", "success", json.dumps(hierarchy))


def delete_tree_callback(confirm_delete=True):
if confirm_delete:
shutil.rmtree(wildcard_manager._path)
def delete_tree_callback(confirm_delete):
if confirm_delete == "True":
send2trash(wildcard_manager._path)
wildcard_manager._path.mkdir(parents=True, exist_ok=True)
hierarchy = load_hierarchy()

return create_payload("load tree", "success", json.dumps(hierarchy))

return create_payload("delete tree", "failed", json.dumps([]))

return create_payload(
"load tree",
"success",
json.dumps(hierarchy)
)
return create_payload(
"delete tree",
"failed",
json.dumps([])
)

def receive_tree_event(s):
js = json.loads(s)
Expand All @@ -190,12 +218,11 @@ def save_file_callback(js):
wildcard = js["wildcard"]["name"]
path = wildcard_manager.wildcard_to_path(wildcard)

contents=js["contents"]
contents = js["contents"]

with path.open("w") as f:
contents = contents.splitlines()
for c in contents:
f.write(c.strip() + os.linesep)
except Exception as e:
logger.exception(e)

0 comments on commit 601871b

Please sign in to comment.