stable-diffusion-webui

Форк
0
/
ui_gradio_extensions.py 
62 строки · 2.0 Кб
1
import os
2
import gradio as gr
3

4
from modules import localization, shared, scripts, util
5
from modules.paths import script_path, data_path
6

7

8
def webpath(fn):
9
    return f'file={util.truncate_path(fn)}?{os.path.getmtime(fn)}'
10

11

12
def javascript_html():
13
    # Ensure localization is in `window` before scripts
14
    head = f'<script type="text/javascript">{localization.localization_js(shared.opts.localization)}</script>\n'
15

16
    script_js = os.path.join(script_path, "script.js")
17
    head += f'<script type="text/javascript" src="{webpath(script_js)}"></script>\n'
18

19
    for script in scripts.list_scripts("javascript", ".js"):
20
        head += f'<script type="text/javascript" src="{webpath(script.path)}"></script>\n'
21

22
    for script in scripts.list_scripts("javascript", ".mjs"):
23
        head += f'<script type="module" src="{webpath(script.path)}"></script>\n'
24

25
    if shared.cmd_opts.theme:
26
        head += f'<script type="text/javascript">set_theme(\"{shared.cmd_opts.theme}\");</script>\n'
27

28
    return head
29

30

31
def css_html():
32
    head = ""
33

34
    def stylesheet(fn):
35
        return f'<link rel="stylesheet" property="stylesheet" href="{webpath(fn)}">'
36

37
    for cssfile in scripts.list_files_with_name("style.css"):
38
        head += stylesheet(cssfile)
39

40
    user_css = os.path.join(data_path, "user.css")
41
    if os.path.exists(user_css):
42
        head += stylesheet(user_css)
43

44
    return head
45

46

47
def reload_javascript():
48
    js = javascript_html()
49
    css = css_html()
50

51
    def template_response(*args, **kwargs):
52
        res = shared.GradioTemplateResponseOriginal(*args, **kwargs)
53
        res.body = res.body.replace(b'</head>', f'{js}</head>'.encode("utf8"))
54
        res.body = res.body.replace(b'</body>', f'{css}</body>'.encode("utf8"))
55
        res.init_headers()
56
        return res
57

58
    gr.routes.templates.TemplateResponse = template_response
59

60

61
if not hasattr(shared, 'GradioTemplateResponseOriginal'):
62
    shared.GradioTemplateResponseOriginal = gr.routes.templates.TemplateResponse
63

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.