ui_extensions.py 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
import json
import os.path
import shutil
import sys
import time
import traceback

import git

import gradio as gr
import html
12 13
import shutil
import errno
14 15 16 17

from modules import extensions, shared, paths


A
AUTOMATIC 已提交
18 19 20
available_extensions = {"extensions": []}


21
def check_access():
J
jcowens 已提交
22
    assert not shared.cmd_opts.disable_extension_access, "extension access disabled because of command line flags"
23 24


25
def apply_and_restart(disable_list, update_list):
26 27
    check_access()

28 29 30 31 32 33 34 35 36 37 38 39 40
    disabled = json.loads(disable_list)
    assert type(disabled) == list, f"wrong disable_list data for apply_and_restart: {disable_list}"

    update = json.loads(update_list)
    assert type(update) == list, f"wrong update_list data for apply_and_restart: {update_list}"

    update = set(update)

    for ext in extensions.extensions:
        if ext.name not in update:
            continue

        try:
41
            ext.fetch_and_reset_hard()
42
        except Exception:
43
            print(f"Error getting updates for {ext.name}:", file=sys.stderr)
44 45 46 47 48 49 50 51 52 53
            print(traceback.format_exc(), file=sys.stderr)

    shared.opts.disabled_extensions = disabled
    shared.opts.save(shared.config_filename)

    shared.state.interrupt()
    shared.state.need_restart = True


def check_updates():
54 55
    check_access()

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    for ext in extensions.extensions:
        if ext.remote is None:
            continue

        try:
            ext.check_updates()
        except Exception:
            print(f"Error checking updates for {ext.name}:", file=sys.stderr)
            print(traceback.format_exc(), file=sys.stderr)

    return extension_table()


def extension_table():
    code = f"""<!-- {time.time()} -->
    <table id="extensions">
        <thead>
            <tr>
                <th><abbr title="Use checkbox to enable the extension; it will be enabled or disabled when you click apply button">Extension</abbr></th>
                <th>URL</th>
                <th><abbr title="Use checkbox to mark the extension for update; it will be updated when you click apply button">Update</abbr></th>
            </tr>
        </thead>
        <tbody>
    """

    for ext in extensions.extensions:
A
AUTOMATIC 已提交
83 84 85 86 87 88
        remote = ""
        if ext.is_builtin:
            remote = "built-in"
        elif ext.remote:
            remote = f"""<a href="{html.escape(ext.remote or '')}" target="_blank">{html.escape("built-in" if ext.is_builtin else ext.remote or '')}</a>"""

89 90 91 92 93 94 95 96
        if ext.can_update:
            ext_status = f"""<label><input class="gr-check-radio gr-checkbox" name="update_{html.escape(ext.name)}" checked="checked" type="checkbox">{html.escape(ext.status)}</label>"""
        else:
            ext_status = ext.status

        code += f"""
            <tr>
                <td><label><input class="gr-check-radio gr-checkbox" name="enable_{html.escape(ext.name)}" type="checkbox" {'checked="checked"' if ext.enabled else ''}>{html.escape(ext.name)}</label></td>
A
AUTOMATIC 已提交
97
                <td>{remote}</td>
98 99 100 101 102 103 104 105 106 107 108 109
                <td{' class="extension_status"' if ext.remote is not None else ''}>{ext_status}</td>
            </tr>
    """

    code += """
        </tbody>
    </table>
    """

    return code


A
AUTOMATIC 已提交
110 111 112 113 114 115 116 117
def normalize_git_url(url):
    if url is None:
        return ""

    url = url.replace(".git", "")
    return url


118
def install_extension_from_url(dirname, url):
119 120
    check_access()

121 122 123 124
    assert url, 'No URL specified'

    if dirname is None or dirname == "":
        *parts, last_part = url.split('/')
A
AUTOMATIC 已提交
125
        last_part = normalize_git_url(last_part)
126 127 128 129 130 131

        dirname = last_part

    target_dir = os.path.join(extensions.extensions_dir, dirname)
    assert not os.path.exists(target_dir), f'Extension directory already exists: {target_dir}'

A
AUTOMATIC 已提交
132 133
    normalized_url = normalize_git_url(url)
    assert len([x for x in extensions.extensions if normalize_git_url(x.remote) == normalized_url]) == 0, 'Extension with this URL is already installed'
134

135
    tmpdir = os.path.join(paths.data_path, "tmp", dirname)
136 137 138 139 140 141 142

    try:
        shutil.rmtree(tmpdir, True)

        repo = git.Repo.clone_from(url, tmpdir)
        repo.remote().fetch()

143 144 145 146 147 148 149 150 151 152 153 154
        try:
            os.rename(tmpdir, target_dir)
        except OSError as err:
            # TODO what does this do on windows? I think it'll be a different error code but I don't have a system to check it
            # Shouldn't cause any new issues at least but we probably want to handle it there too.
            if err.errno == errno.EXDEV:
                # Cross device link, typical in docker or when tmp/ and extensions/ are on different file systems
                # Since we can't use a rename, do the slower but more versitile shutil.move()
                shutil.move(tmpdir, target_dir)
            else:
                # Something else, not enough free space, permissions, etc.  rethrow it so that it gets handled.
                raise(err)
155

156 157 158
        import launch
        launch.run_extension_installer(target_dir)

159 160 161 162 163 164
        extensions.list_extensions()
        return [extension_table(), html.escape(f"Installed into {target_dir}. Use Installed tab to restart.")]
    finally:
        shutil.rmtree(tmpdir, True)


165
def install_extension_from_index(url, hide_tags, sort_column):
A
AUTOMATIC 已提交
166 167
    ext_table, message = install_extension_from_url(None, url)

168
    code, _ = refresh_available_extensions_from_data(hide_tags, sort_column)
A
AUTOMATIC 已提交
169

170
    return code, ext_table, message
A
AUTOMATIC 已提交
171

172

173
def refresh_available_extensions(url, hide_tags, sort_column):
A
AUTOMATIC 已提交
174 175 176 177 178 179 180 181
    global available_extensions

    import urllib.request
    with urllib.request.urlopen(url) as response:
        text = response.read()

    available_extensions = json.loads(text)

182
    code, tags = refresh_available_extensions_from_data(hide_tags, sort_column)
183 184 185 186

    return url, code, gr.CheckboxGroup.update(choices=tags), ''


187 188
def refresh_available_extensions_for_tags(hide_tags, sort_column):
    code, _ = refresh_available_extensions_from_data(hide_tags, sort_column)
A
AUTOMATIC 已提交
189

190
    return code, ''
A
AUTOMATIC 已提交
191

192

193 194 195 196 197 198 199 200 201 202 203
sort_ordering = [
    # (reverse, order_by_function)
    (True, lambda x: x.get('added', 'z')),
    (False, lambda x: x.get('added', 'z')),
    (False, lambda x: x.get('name', 'z')),
    (True, lambda x: x.get('name', 'z')),
    (False, lambda x: 'z'),
]


def refresh_available_extensions_from_data(hide_tags, sort_column):
A
AUTOMATIC 已提交
204 205 206
    extlist = available_extensions["extensions"]
    installed_extension_urls = {normalize_git_url(extension.remote): extension.name for extension in extensions.extensions}

207 208 209 210
    tags = available_extensions.get("tags", {})
    tags_to_hide = set(hide_tags)
    hidden = 0

A
AUTOMATIC 已提交
211 212 213 214 215 216 217 218 219 220 221 222
    code = f"""<!-- {time.time()} -->
    <table id="available_extensions">
        <thead>
            <tr>
                <th>Extension</th>
                <th>Description</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
    """

223 224 225
    sort_reverse, sort_function = sort_ordering[sort_column if 0 <= sort_column < len(sort_ordering) else 0]

    for ext in sorted(extlist, key=sort_function, reverse=sort_reverse):
A
AUTOMATIC 已提交
226
        name = ext.get("name", "noname")
227
        added = ext.get('added', 'unknown')
A
AUTOMATIC 已提交
228 229
        url = ext.get("url", None)
        description = ext.get("description", "")
230
        extension_tags = ext.get("tags", [])
A
AUTOMATIC 已提交
231 232 233 234

        if url is None:
            continue

A
AUTOMATIC 已提交
235 236 237
        existing = installed_extension_urls.get(normalize_git_url(url), None)
        extension_tags = extension_tags + ["installed"] if existing else extension_tags

238 239 240 241
        if len([x for x in extension_tags if x in tags_to_hide]) > 0:
            hidden += 1
            continue

A
AUTOMATIC 已提交
242 243
        install_code = f"""<input onclick="install_extension_from_index(this, '{html.escape(url)}')" type="button" value="{"Install" if not existing else "Installed"}" {"disabled=disabled" if existing else ""} class="gr-button gr-button-lg gr-button-secondary">"""

244 245
        tags_text = ", ".join([f"<span class='extension-tag' title='{tags.get(x, '')}'>{x}</span>" for x in extension_tags])

A
AUTOMATIC 已提交
246 247
        code += f"""
            <tr>
248
                <td><a href="{html.escape(url)}" target="_blank">{html.escape(name)}</a><br />{tags_text}</td>
249
                <td>{html.escape(description)}<p class="info"><span class="date_added">Added: {html.escape(added)}</span></p></td>
A
AUTOMATIC 已提交
250 251
                <td>{install_code}</td>
            </tr>
A
AUTOMATIC 已提交
252 253 254 255 256
        
        """

        for tag in [x for x in extension_tags if x not in tags]:
            tags[tag] = tag
A
AUTOMATIC 已提交
257 258 259 260 261 262

    code += """
        </tbody>
    </table>
    """

263 264 265 266
    if hidden > 0:
        code += f"<p>Extension hidden: {hidden}</p>"

    return code, list(tags)
A
AUTOMATIC 已提交
267 268


269 270 271 272 273 274 275 276 277 278
def create_ui():
    import modules.ui

    with gr.Blocks(analytics_enabled=False) as ui:
        with gr.Tabs(elem_id="tabs_extensions") as tabs:
            with gr.TabItem("Installed"):

                with gr.Row():
                    apply = gr.Button(value="Apply and restart UI", variant="primary")
                    check = gr.Button(value="Check for updates")
A
AUTOMATIC 已提交
279 280
                    extensions_disabled_list = gr.Text(elem_id="extensions_disabled_list", visible=False).style(container=False)
                    extensions_update_list = gr.Text(elem_id="extensions_update_list", visible=False).style(container=False)
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

                extensions_table = gr.HTML(lambda: extension_table())

                apply.click(
                    fn=apply_and_restart,
                    _js="extensions_apply",
                    inputs=[extensions_disabled_list, extensions_update_list],
                    outputs=[],
                )

                check.click(
                    fn=check_updates,
                    _js="extensions_check",
                    inputs=[],
                    outputs=[extensions_table],
                )

A
AUTOMATIC 已提交
298 299 300 301 302 303 304
            with gr.TabItem("Available"):
                with gr.Row():
                    refresh_available_extensions_button = gr.Button(value="Load from:", variant="primary")
                    available_extensions_index = gr.Text(value="https://raw.githubusercontent.com/wiki/AUTOMATIC1111/stable-diffusion-webui/Extensions-index.md", label="Extension index URL").style(container=False)
                    extension_to_install = gr.Text(elem_id="extension_to_install", visible=False)
                    install_extension_button = gr.Button(elem_id="install_extension_button", visible=False)

305
                with gr.Row():
A
AUTOMATIC 已提交
306
                    hide_tags = gr.CheckboxGroup(value=["ads", "localization", "installed"], label="Hide extensions with tags", choices=["script", "ads", "localization", "installed"])
307
                    sort_column = gr.Radio(value="newest first", label="Order", choices=["newest first", "oldest first", "a-z", "z-a", "internal order", ], type="index")
308

A
AUTOMATIC 已提交
309 310 311 312
                install_result = gr.HTML()
                available_extensions_table = gr.HTML()

                refresh_available_extensions_button.click(
313
                    fn=modules.ui.wrap_gradio_call(refresh_available_extensions, extra_outputs=[gr.update(), gr.update(), gr.update()]),
314
                    inputs=[available_extensions_index, hide_tags, sort_column],
315
                    outputs=[available_extensions_index, available_extensions_table, hide_tags, install_result],
A
AUTOMATIC 已提交
316 317 318 319
                )

                install_extension_button.click(
                    fn=modules.ui.wrap_gradio_call(install_extension_from_index, extra_outputs=[gr.update(), gr.update()]),
320
                    inputs=[extension_to_install, hide_tags, sort_column],
A
AUTOMATIC 已提交
321 322 323
                    outputs=[available_extensions_table, extensions_table, install_result],
                )

324 325
                hide_tags.change(
                    fn=modules.ui.wrap_gradio_call(refresh_available_extensions_for_tags, extra_outputs=[gr.update()]),
326 327 328 329 330 331 332
                    inputs=[hide_tags, sort_column],
                    outputs=[available_extensions_table, install_result]
                )

                sort_column.change(
                    fn=modules.ui.wrap_gradio_call(refresh_available_extensions_for_tags, extra_outputs=[gr.update()]),
                    inputs=[hide_tags, sort_column],
333 334 335
                    outputs=[available_extensions_table, install_result]
                )

336 337 338
            with gr.TabItem("Install from URL"):
                install_url = gr.Text(label="URL for extension's git repository")
                install_dirname = gr.Text(label="Local directory name", placeholder="Leave empty for auto")
A
AUTOMATIC 已提交
339 340
                install_button = gr.Button(value="Install", variant="primary")
                install_result = gr.HTML(elem_id="extension_install_result")
341

A
AUTOMATIC 已提交
342
                install_button.click(
343 344
                    fn=modules.ui.wrap_gradio_call(install_extension_from_url, extra_outputs=[gr.update()]),
                    inputs=[install_dirname, install_url],
A
AUTOMATIC 已提交
345
                    outputs=[extensions_table, install_result],
346 347 348
                )

    return ui