2023-01-21 13:36:07 +08:00
|
|
|
import os.path
|
2023-01-29 03:52:27 +08:00
|
|
|
import urllib.parse
|
|
|
|
from pathlib import Path
|
2023-01-21 13:36:07 +08:00
|
|
|
|
|
|
|
from modules import shared
|
2023-05-18 02:03:41 +08:00
|
|
|
from modules.images import read_info_from_image, save_image_with_geninfo
|
2023-06-02 12:08:45 +08:00
|
|
|
from modules.ui import up_down_symbol
|
2023-01-21 13:36:07 +08:00
|
|
|
import gradio as gr
|
|
|
|
import json
|
2023-01-24 04:11:34 +08:00
|
|
|
import html
|
2023-01-21 13:36:07 +08:00
|
|
|
|
|
|
|
from modules.generation_parameters_copypaste import image_from_url_text
|
|
|
|
|
|
|
|
extra_pages = []
|
2023-01-29 03:52:27 +08:00
|
|
|
allowed_dirs = set()
|
2023-01-21 13:36:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
def register_page(page):
|
2023-01-21 21:15:53 +08:00
|
|
|
"""registers extra networks page for the UI; recommend doing it in on_before_ui() callback for extensions"""
|
2023-01-21 13:36:07 +08:00
|
|
|
|
|
|
|
extra_pages.append(page)
|
2023-01-29 03:52:27 +08:00
|
|
|
allowed_dirs.clear()
|
|
|
|
allowed_dirs.update(set(sum([x.allowed_directories_for_previews() for x in extra_pages], [])))
|
|
|
|
|
|
|
|
|
2023-03-25 15:11:04 +08:00
|
|
|
def fetch_file(filename: str = ""):
|
|
|
|
from starlette.responses import FileResponse
|
|
|
|
|
2023-05-10 16:05:02 +08:00
|
|
|
if not any(Path(x).absolute() in Path(filename).absolute().parents for x in allowed_dirs):
|
2023-03-25 15:11:04 +08:00
|
|
|
raise ValueError(f"File cannot be fetched: {filename}. Must be in one of directories registered by extra pages.")
|
|
|
|
|
|
|
|
ext = os.path.splitext(filename)[1].lower()
|
2023-07-05 17:19:26 +08:00
|
|
|
if ext not in (".png", ".jpg", ".jpeg", ".webp", ".gif"):
|
|
|
|
raise ValueError(f"File cannot be fetched: {filename}. Only png, jpg, webp, and gif.")
|
2023-03-25 15:11:04 +08:00
|
|
|
|
|
|
|
# would profit from returning 304
|
|
|
|
return FileResponse(filename, headers={"Accept-Ranges": "bytes"})
|
|
|
|
|
|
|
|
|
|
|
|
def get_metadata(page: str = "", item: str = ""):
|
|
|
|
from starlette.responses import JSONResponse
|
2023-01-29 03:52:27 +08:00
|
|
|
|
2023-03-25 15:11:04 +08:00
|
|
|
page = next(iter([x for x in extra_pages if x.name == page]), None)
|
|
|
|
if page is None:
|
|
|
|
return JSONResponse({})
|
2023-01-29 03:52:27 +08:00
|
|
|
|
2023-03-25 15:11:04 +08:00
|
|
|
metadata = page.metadata.get(item)
|
|
|
|
if metadata is None:
|
|
|
|
return JSONResponse({})
|
2023-01-29 03:52:27 +08:00
|
|
|
|
2023-03-25 15:11:04 +08:00
|
|
|
return JSONResponse({"metadata": metadata})
|
2023-01-29 03:52:27 +08:00
|
|
|
|
2023-03-25 15:11:04 +08:00
|
|
|
|
|
|
|
def add_pages_to_demo(app):
|
2023-01-29 03:52:27 +08:00
|
|
|
app.add_api_route("/sd_extra_networks/thumb", fetch_file, methods=["GET"])
|
2023-03-25 15:11:04 +08:00
|
|
|
app.add_api_route("/sd_extra_networks/metadata", get_metadata, methods=["GET"])
|
2023-01-21 13:36:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ExtraNetworksPage:
|
|
|
|
def __init__(self, title):
|
|
|
|
self.title = title
|
2023-01-21 22:20:24 +08:00
|
|
|
self.name = title.lower()
|
2023-01-21 13:36:07 +08:00
|
|
|
self.card_page = shared.html("extra-networks-card.html")
|
|
|
|
self.allow_negative_prompt = False
|
2023-03-25 15:11:04 +08:00
|
|
|
self.metadata = {}
|
2023-01-21 13:36:07 +08:00
|
|
|
|
|
|
|
def refresh(self):
|
|
|
|
pass
|
|
|
|
|
2023-01-29 03:52:27 +08:00
|
|
|
def link_preview(self, filename):
|
2023-05-10 03:17:58 +08:00
|
|
|
quoted_filename = urllib.parse.quote(filename.replace('\\', '/'))
|
|
|
|
mtime = os.path.getmtime(filename)
|
|
|
|
return f"./sd_extra_networks/thumb?filename={quoted_filename}&mtime={mtime}"
|
2023-01-29 03:52:27 +08:00
|
|
|
|
2023-01-29 15:20:19 +08:00
|
|
|
def search_terms_from_path(self, filename, possible_directories=None):
|
|
|
|
abspath = os.path.abspath(filename)
|
|
|
|
|
|
|
|
for parentdir in (possible_directories if possible_directories is not None else self.allowed_directories_for_previews()):
|
|
|
|
parentdir = os.path.abspath(parentdir)
|
|
|
|
if abspath.startswith(parentdir):
|
2023-01-29 16:34:58 +08:00
|
|
|
return abspath[len(parentdir):].replace('\\', '/')
|
2023-01-29 15:20:19 +08:00
|
|
|
|
|
|
|
return ""
|
|
|
|
|
2023-01-23 09:01:24 +08:00
|
|
|
def create_html(self, tabname):
|
|
|
|
view = shared.opts.extra_networks_default_view
|
2023-01-21 13:36:07 +08:00
|
|
|
items_html = ''
|
|
|
|
|
2023-03-25 15:11:04 +08:00
|
|
|
self.metadata = {}
|
|
|
|
|
2023-01-29 16:34:58 +08:00
|
|
|
subdirs = {}
|
|
|
|
for parentdir in [os.path.abspath(x) for x in self.allowed_directories_for_previews()]:
|
2023-05-11 23:55:43 +08:00
|
|
|
for root, dirs, _ in os.walk(parentdir, followlinks=True):
|
2023-05-08 16:33:45 +08:00
|
|
|
for dirname in dirs:
|
|
|
|
x = os.path.join(root, dirname)
|
2023-01-29 16:34:58 +08:00
|
|
|
|
2023-05-08 16:33:45 +08:00
|
|
|
if not os.path.isdir(x):
|
|
|
|
continue
|
2023-01-29 16:34:58 +08:00
|
|
|
|
2023-05-08 16:33:45 +08:00
|
|
|
subdir = os.path.abspath(x)[len(parentdir):].replace("\\", "/")
|
|
|
|
while subdir.startswith("/"):
|
|
|
|
subdir = subdir[1:]
|
2023-02-19 17:09:25 +08:00
|
|
|
|
2023-05-08 16:33:45 +08:00
|
|
|
is_empty = len(os.listdir(x)) == 0
|
|
|
|
if not is_empty and not subdir.endswith("/"):
|
|
|
|
subdir = subdir + "/"
|
|
|
|
|
2023-05-18 02:45:26 +08:00
|
|
|
if ("/." in subdir or subdir.startswith(".")) and not shared.opts.extra_networks_show_hidden_directories:
|
|
|
|
continue
|
|
|
|
|
2023-05-08 16:33:45 +08:00
|
|
|
subdirs[subdir] = 1
|
2023-01-29 16:34:58 +08:00
|
|
|
|
|
|
|
if subdirs:
|
|
|
|
subdirs = {"": 1, **subdirs}
|
|
|
|
|
|
|
|
subdirs_html = "".join([f"""
|
2023-03-21 14:24:19 +08:00
|
|
|
<button class='lg secondary gradio-button custom-button{" search-all" if subdir=="" else ""}' onclick='extraNetworksSearchButton("{tabname}_extra_tabs", event)'>
|
2023-01-29 16:34:58 +08:00
|
|
|
{html.escape(subdir if subdir!="" else "all")}
|
|
|
|
</button>
|
|
|
|
""" for subdir in subdirs])
|
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
for item in self.list_items():
|
2023-03-25 15:11:04 +08:00
|
|
|
metadata = item.get("metadata")
|
|
|
|
if metadata:
|
|
|
|
self.metadata[item["name"]] = metadata
|
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
items_html += self.create_html_for_item(item, tabname)
|
|
|
|
|
|
|
|
if items_html == '':
|
|
|
|
dirs = "".join([f"<li>{x}</li>" for x in self.allowed_directories_for_previews()])
|
|
|
|
items_html = shared.html("extra-networks-no-cards.html").format(dirs=dirs)
|
|
|
|
|
2023-02-06 05:42:45 +08:00
|
|
|
self_name_id = self.name.replace(" ", "_")
|
|
|
|
|
2023-01-21 22:20:24 +08:00
|
|
|
res = f"""
|
2023-02-06 05:42:45 +08:00
|
|
|
<div id='{tabname}_{self_name_id}_subdirs' class='extra-network-subdirs extra-network-subdirs-{view}'>
|
2023-01-29 16:34:58 +08:00
|
|
|
{subdirs_html}
|
|
|
|
</div>
|
2023-02-06 05:42:45 +08:00
|
|
|
<div id='{tabname}_{self_name_id}_cards' class='extra-network-{view}'>
|
2023-01-21 22:20:24 +08:00
|
|
|
{items_html}
|
|
|
|
</div>
|
|
|
|
"""
|
2023-01-21 13:36:07 +08:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
def list_items(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def allowed_directories_for_previews(self):
|
|
|
|
return []
|
|
|
|
|
|
|
|
def create_html_for_item(self, item, tabname):
|
2023-05-18 02:45:26 +08:00
|
|
|
"""
|
|
|
|
Create HTML for card item in tab tabname; can return empty string if the item is not meant to be shown.
|
|
|
|
"""
|
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
preview = item.get("preview", None)
|
|
|
|
|
2023-01-29 03:52:27 +08:00
|
|
|
onclick = item.get("onclick", None)
|
|
|
|
if onclick is None:
|
|
|
|
onclick = '"' + html.escape(f"""return cardClicked({json.dumps(tabname)}, {item["prompt"]}, {"true" if self.allow_negative_prompt else "false"})""") + '"'
|
|
|
|
|
2023-03-25 17:12:55 +08:00
|
|
|
height = f"height: {shared.opts.extra_networks_card_height}px;" if shared.opts.extra_networks_card_height else ''
|
|
|
|
width = f"width: {shared.opts.extra_networks_card_width}px;" if shared.opts.extra_networks_card_width else ''
|
2023-03-27 11:47:05 +08:00
|
|
|
background_image = f'<img src="{html.escape(preview)}" class="preview" loading="lazy">' if preview else ''
|
2023-03-14 14:10:26 +08:00
|
|
|
metadata_button = ""
|
|
|
|
metadata = item.get("metadata")
|
|
|
|
if metadata:
|
2023-03-25 17:08:24 +08:00
|
|
|
metadata_button = f"<div class='metadata-button' title='Show metadata' onclick='extraNetworksRequestMetadata(event, {json.dumps(self.name)}, {json.dumps(item['name'])})'></div>"
|
2023-03-14 14:10:26 +08:00
|
|
|
|
2023-05-08 16:33:45 +08:00
|
|
|
local_path = ""
|
|
|
|
filename = item.get("filename", "")
|
|
|
|
for reldir in self.allowed_directories_for_previews():
|
|
|
|
absdir = os.path.abspath(reldir)
|
|
|
|
|
|
|
|
if filename.startswith(absdir):
|
|
|
|
local_path = filename[len(absdir):]
|
|
|
|
|
2023-05-18 02:45:26 +08:00
|
|
|
# if this is true, the item must not be shown in the default view, and must instead only be
|
2023-05-08 16:33:45 +08:00
|
|
|
# shown when searching for it
|
2023-05-18 02:45:26 +08:00
|
|
|
if shared.opts.extra_networks_hidden_models == "Always":
|
|
|
|
search_only = False
|
|
|
|
else:
|
|
|
|
search_only = "/." in local_path or "\\." in local_path
|
|
|
|
|
|
|
|
if search_only and shared.opts.extra_networks_hidden_models == "Never":
|
|
|
|
return ""
|
2023-05-08 16:33:45 +08:00
|
|
|
|
2023-06-02 12:08:45 +08:00
|
|
|
sort_keys = " ".join([html.escape(f'data-sort-{k}={v}') for k, v in item.get("sort_keys", {}).items()]).strip()
|
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
args = {
|
2023-03-27 11:47:05 +08:00
|
|
|
"background_image": background_image,
|
2023-05-19 04:23:13 +08:00
|
|
|
"style": f"'display: none; {height}{width}'",
|
2023-01-29 03:52:27 +08:00
|
|
|
"prompt": item.get("prompt", None),
|
2023-01-21 13:36:07 +08:00
|
|
|
"tabname": json.dumps(tabname),
|
|
|
|
"local_preview": json.dumps(item["local_preview"]),
|
|
|
|
"name": item["name"],
|
2023-03-07 03:14:06 +08:00
|
|
|
"description": (item.get("description") or ""),
|
2023-01-29 03:52:27 +08:00
|
|
|
"card_clicked": onclick,
|
2023-01-24 04:11:34 +08:00
|
|
|
"save_card_preview": '"' + html.escape(f"""return saveCardPreview(event, {json.dumps(tabname)}, {json.dumps(item["local_preview"])})""") + '"',
|
2023-01-29 15:20:19 +08:00
|
|
|
"search_term": item.get("search_term", ""),
|
2023-03-14 14:10:26 +08:00
|
|
|
"metadata_button": metadata_button,
|
2023-05-18 00:51:54 +08:00
|
|
|
"search_only": " search_only" if search_only else "",
|
2023-06-02 12:08:45 +08:00
|
|
|
"sort_keys": sort_keys,
|
2023-01-21 13:36:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return self.card_page.format(**args)
|
|
|
|
|
2023-06-02 12:08:45 +08:00
|
|
|
def get_sort_keys(self, path):
|
|
|
|
"""
|
|
|
|
List of default keys used for sorting in the UI.
|
|
|
|
"""
|
2023-06-03 04:12:24 +08:00
|
|
|
pth = Path(path)
|
|
|
|
stat = pth.stat()
|
2023-06-02 12:08:45 +08:00
|
|
|
return {
|
2023-06-03 04:12:24 +08:00
|
|
|
"date_created": int(stat.st_ctime or 0),
|
|
|
|
"date_modified": int(stat.st_mtime or 0),
|
|
|
|
"name": pth.name.lower(),
|
2023-06-02 12:08:45 +08:00
|
|
|
}
|
|
|
|
|
2023-03-11 20:27:42 +08:00
|
|
|
def find_preview(self, path):
|
2023-03-07 03:00:34 +08:00
|
|
|
"""
|
|
|
|
Find a preview PNG for a given path (without extension) and call link_preview on it.
|
|
|
|
"""
|
2023-03-11 20:33:24 +08:00
|
|
|
|
2023-05-14 15:22:40 +08:00
|
|
|
preview_extensions = ["png", "jpg", "jpeg", "webp"]
|
2023-03-11 20:46:20 +08:00
|
|
|
if shared.opts.samples_format not in preview_extensions:
|
|
|
|
preview_extensions.append(shared.opts.samples_format)
|
|
|
|
|
2023-03-11 20:33:24 +08:00
|
|
|
potential_files = sum([[path + "." + ext, path + ".preview." + ext] for ext in preview_extensions], [])
|
|
|
|
|
|
|
|
for file in potential_files:
|
2023-03-07 03:00:34 +08:00
|
|
|
if os.path.isfile(file):
|
|
|
|
return self.link_preview(file)
|
2023-03-11 20:27:42 +08:00
|
|
|
|
2023-03-07 03:00:34 +08:00
|
|
|
return None
|
|
|
|
|
2023-03-11 20:27:42 +08:00
|
|
|
def find_description(self, path):
|
2023-03-07 03:14:06 +08:00
|
|
|
"""
|
|
|
|
Find and read a description file for a given path (without extension).
|
|
|
|
"""
|
|
|
|
for file in [f"{path}.txt", f"{path}.description.txt"]:
|
|
|
|
try:
|
|
|
|
with open(file, "r", encoding="utf-8", errors="replace") as f:
|
|
|
|
return f.read()
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
return None
|
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
|
2023-05-19 20:27:23 +08:00
|
|
|
def initialize():
|
2023-01-21 13:36:07 +08:00
|
|
|
extra_pages.clear()
|
|
|
|
|
|
|
|
|
2023-05-19 20:35:16 +08:00
|
|
|
def register_default_pages():
|
|
|
|
from modules.ui_extra_networks_textual_inversion import ExtraNetworksPageTextualInversion
|
|
|
|
from modules.ui_extra_networks_hypernets import ExtraNetworksPageHypernetworks
|
|
|
|
from modules.ui_extra_networks_checkpoints import ExtraNetworksPageCheckpoints
|
|
|
|
register_page(ExtraNetworksPageTextualInversion())
|
|
|
|
register_page(ExtraNetworksPageHypernetworks())
|
|
|
|
register_page(ExtraNetworksPageCheckpoints())
|
|
|
|
|
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
class ExtraNetworksUi:
|
|
|
|
def __init__(self):
|
|
|
|
self.pages = None
|
2023-05-08 16:33:45 +08:00
|
|
|
"""gradio HTML components related to extra networks' pages"""
|
|
|
|
|
|
|
|
self.page_contents = None
|
|
|
|
"""HTML content of the above; empty initially, filled when extra pages have to be shown"""
|
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
self.stored_extra_pages = None
|
|
|
|
|
|
|
|
self.button_save_preview = None
|
|
|
|
self.preview_target_filename = None
|
|
|
|
|
|
|
|
self.tabname = None
|
|
|
|
|
|
|
|
|
2023-01-22 16:00:05 +08:00
|
|
|
def pages_in_preferred_order(pages):
|
|
|
|
tab_order = [x.lower().strip() for x in shared.opts.ui_extra_networks_tab_reorder.split(",")]
|
|
|
|
|
|
|
|
def tab_name_score(name):
|
|
|
|
name = name.lower()
|
|
|
|
for i, possible_match in enumerate(tab_order):
|
|
|
|
if possible_match in name:
|
|
|
|
return i
|
|
|
|
|
|
|
|
return len(pages)
|
|
|
|
|
|
|
|
tab_scores = {page.name: (tab_name_score(page.name), original_index) for original_index, page in enumerate(pages)}
|
|
|
|
|
|
|
|
return sorted(pages, key=lambda x: tab_scores[x.name])
|
|
|
|
|
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
def create_ui(container, button, tabname):
|
|
|
|
ui = ExtraNetworksUi()
|
|
|
|
ui.pages = []
|
2023-05-08 16:33:45 +08:00
|
|
|
ui.pages_contents = []
|
2023-01-22 16:04:02 +08:00
|
|
|
ui.stored_extra_pages = pages_in_preferred_order(extra_pages.copy())
|
2023-01-21 13:36:07 +08:00
|
|
|
ui.tabname = tabname
|
|
|
|
|
2023-05-10 12:52:45 +08:00
|
|
|
with gr.Tabs(elem_id=tabname+"_extra_tabs"):
|
2023-01-22 16:04:02 +08:00
|
|
|
for page in ui.stored_extra_pages:
|
2023-05-08 16:33:45 +08:00
|
|
|
page_id = page.title.lower().replace(" ", "_")
|
2023-03-25 15:11:04 +08:00
|
|
|
|
2023-05-08 16:33:45 +08:00
|
|
|
with gr.Tab(page.title, id=page_id):
|
|
|
|
elem_id = f"{tabname}_{page_id}_cards_html"
|
2023-05-16 01:33:44 +08:00
|
|
|
page_elem = gr.HTML('Loading...', elem_id=elem_id)
|
2023-01-21 13:36:07 +08:00
|
|
|
ui.pages.append(page_elem)
|
|
|
|
|
2023-05-08 16:33:45 +08:00
|
|
|
page_elem.change(fn=lambda: None, _js='function(){applyExtraNetworkFilter(' + json.dumps(tabname) + '); return []}', inputs=[], outputs=[])
|
|
|
|
|
|
|
|
gr.Textbox('', show_label=False, elem_id=tabname+"_extra_search", placeholder="Search...", visible=False)
|
2023-06-02 12:08:45 +08:00
|
|
|
gr.Dropdown(choices=['Default Sort', 'Date Created', 'Date Modified', 'Name'], value='Default Sort', elem_id=tabname+"_extra_sort", multiselect=False, visible=False, show_label=False, interactive=True)
|
|
|
|
gr.Button(up_down_symbol, elem_id=tabname+"_extra_sortorder")
|
2023-01-21 22:20:24 +08:00
|
|
|
button_refresh = gr.Button('Refresh', elem_id=tabname+"_extra_refresh")
|
|
|
|
|
2023-01-21 13:36:07 +08:00
|
|
|
ui.button_save_preview = gr.Button('Save preview', elem_id=tabname+"_save_preview", visible=False)
|
|
|
|
ui.preview_target_filename = gr.Textbox('Preview save filename', elem_id=tabname+"_preview_filename", visible=False)
|
|
|
|
|
2023-01-26 00:55:31 +08:00
|
|
|
def toggle_visibility(is_visible):
|
|
|
|
is_visible = not is_visible
|
2023-05-08 16:33:45 +08:00
|
|
|
|
2023-05-16 01:33:44 +08:00
|
|
|
return is_visible, gr.update(visible=is_visible), gr.update(variant=("secondary-down" if is_visible else "secondary"))
|
|
|
|
|
|
|
|
def fill_tabs(is_empty):
|
|
|
|
"""Creates HTML for extra networks' tabs when the extra networks button is clicked for the first time."""
|
|
|
|
|
|
|
|
if not ui.pages_contents:
|
2023-05-08 16:33:45 +08:00
|
|
|
refresh()
|
|
|
|
|
2023-05-16 01:33:44 +08:00
|
|
|
if is_empty:
|
|
|
|
return True, *ui.pages_contents
|
|
|
|
|
|
|
|
return True, *[gr.update() for _ in ui.pages_contents]
|
2023-01-26 00:55:31 +08:00
|
|
|
|
|
|
|
state_visible = gr.State(value=False)
|
2023-05-16 01:33:44 +08:00
|
|
|
button.click(fn=toggle_visibility, inputs=[state_visible], outputs=[state_visible, container, button], show_progress=False)
|
|
|
|
|
|
|
|
state_empty = gr.State(value=True)
|
|
|
|
button.click(fn=fill_tabs, inputs=[state_empty], outputs=[state_empty, *ui.pages], show_progress=False)
|
2023-01-21 13:36:07 +08:00
|
|
|
|
2023-01-23 09:01:24 +08:00
|
|
|
def refresh():
|
2023-01-21 13:36:07 +08:00
|
|
|
for pg in ui.stored_extra_pages:
|
|
|
|
pg.refresh()
|
|
|
|
|
2023-05-08 16:33:45 +08:00
|
|
|
ui.pages_contents = [pg.create_html(ui.tabname) for pg in ui.stored_extra_pages]
|
|
|
|
|
|
|
|
return ui.pages_contents
|
2023-01-21 13:36:07 +08:00
|
|
|
|
2023-01-23 09:01:24 +08:00
|
|
|
button_refresh.click(fn=refresh, inputs=[], outputs=ui.pages)
|
2023-01-21 13:36:07 +08:00
|
|
|
|
|
|
|
return ui
|
|
|
|
|
|
|
|
|
|
|
|
def path_is_parent(parent_path, child_path):
|
|
|
|
parent_path = os.path.abspath(parent_path)
|
|
|
|
child_path = os.path.abspath(child_path)
|
|
|
|
|
2023-01-29 03:52:27 +08:00
|
|
|
return child_path.startswith(parent_path)
|
2023-01-21 13:36:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
def setup_ui(ui, gallery):
|
2023-01-23 09:01:24 +08:00
|
|
|
def save_preview(index, images, filename):
|
2023-01-21 13:36:07 +08:00
|
|
|
if len(images) == 0:
|
|
|
|
print("There is no image in gallery to save as a preview.")
|
2023-04-29 22:45:22 +08:00
|
|
|
return [page.create_html(ui.tabname) for page in ui.stored_extra_pages]
|
2023-01-21 13:36:07 +08:00
|
|
|
|
|
|
|
index = int(index)
|
|
|
|
index = 0 if index < 0 else index
|
|
|
|
index = len(images) - 1 if index >= len(images) else index
|
|
|
|
|
|
|
|
img_info = images[index if index >= 0 else 0]
|
|
|
|
image = image_from_url_text(img_info)
|
2023-03-26 03:59:50 +08:00
|
|
|
geninfo, items = read_info_from_image(image)
|
2023-01-21 13:36:07 +08:00
|
|
|
|
|
|
|
is_allowed = False
|
|
|
|
for extra_page in ui.stored_extra_pages:
|
2023-05-10 16:05:02 +08:00
|
|
|
if any(path_is_parent(x, filename) for x in extra_page.allowed_directories_for_previews()):
|
2023-01-21 13:36:07 +08:00
|
|
|
is_allowed = True
|
|
|
|
break
|
|
|
|
|
|
|
|
assert is_allowed, f'writing to {filename} is not allowed'
|
|
|
|
|
2023-05-18 02:03:41 +08:00
|
|
|
save_image_with_geninfo(image, geninfo, filename)
|
2023-01-21 13:36:07 +08:00
|
|
|
|
2023-04-29 22:45:22 +08:00
|
|
|
return [page.create_html(ui.tabname) for page in ui.stored_extra_pages]
|
2023-01-21 13:36:07 +08:00
|
|
|
|
|
|
|
ui.button_save_preview.click(
|
|
|
|
fn=save_preview,
|
2023-01-29 03:52:27 +08:00
|
|
|
_js="function(x, y, z){return [selected_gallery_index(), y, z]}",
|
2023-01-23 09:01:24 +08:00
|
|
|
inputs=[ui.preview_target_filename, gallery, ui.preview_target_filename],
|
2023-01-21 13:36:07 +08:00
|
|
|
outputs=[*ui.pages]
|
|
|
|
)
|
2023-01-29 03:52:27 +08:00
|
|
|
|