stable-diffusion-webui
62 строки · 2.0 Кб
1import os
2import gradio as gr
3
4from modules import localization, shared, scripts, util
5from modules.paths import script_path, data_path
6
7
8def webpath(fn):
9return f'file={util.truncate_path(fn)}?{os.path.getmtime(fn)}'
10
11
12def javascript_html():
13# Ensure localization is in `window` before scripts
14head = f'<script type="text/javascript">{localization.localization_js(shared.opts.localization)}</script>\n'
15
16script_js = os.path.join(script_path, "script.js")
17head += f'<script type="text/javascript" src="{webpath(script_js)}"></script>\n'
18
19for script in scripts.list_scripts("javascript", ".js"):
20head += f'<script type="text/javascript" src="{webpath(script.path)}"></script>\n'
21
22for script in scripts.list_scripts("javascript", ".mjs"):
23head += f'<script type="module" src="{webpath(script.path)}"></script>\n'
24
25if shared.cmd_opts.theme:
26head += f'<script type="text/javascript">set_theme(\"{shared.cmd_opts.theme}\");</script>\n'
27
28return head
29
30
31def css_html():
32head = ""
33
34def stylesheet(fn):
35return f'<link rel="stylesheet" property="stylesheet" href="{webpath(fn)}">'
36
37for cssfile in scripts.list_files_with_name("style.css"):
38head += stylesheet(cssfile)
39
40user_css = os.path.join(data_path, "user.css")
41if os.path.exists(user_css):
42head += stylesheet(user_css)
43
44return head
45
46
47def reload_javascript():
48js = javascript_html()
49css = css_html()
50
51def template_response(*args, **kwargs):
52res = shared.GradioTemplateResponseOriginal(*args, **kwargs)
53res.body = res.body.replace(b'</head>', f'{js}</head>'.encode("utf8"))
54res.body = res.body.replace(b'</body>', f'{css}</body>'.encode("utf8"))
55res.init_headers()
56return res
57
58gr.routes.templates.TemplateResponse = template_response
59
60
61if not hasattr(shared, 'GradioTemplateResponseOriginal'):
62shared.GradioTemplateResponseOriginal = gr.routes.templates.TemplateResponse
63