2023-05-31 19:40:09 +00:00
|
|
|
import os
|
|
|
|
import gradio as gr
|
|
|
|
|
2023-12-27 21:52:41 +00:00
|
|
|
from modules import localization, shared, scripts, util
|
|
|
|
from modules.paths import script_path, data_path
|
2023-05-31 19:40:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def webpath(fn):
|
2023-12-27 21:52:41 +00:00
|
|
|
return f'file={util.truncate_path(fn)}?{os.path.getmtime(fn)}'
|
2023-05-31 19:40:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def javascript_html():
|
|
|
|
# Ensure localization is in `window` before scripts
|
|
|
|
head = f'<script type="text/javascript">{localization.localization_js(shared.opts.localization)}</script>\n'
|
|
|
|
|
|
|
|
script_js = os.path.join(script_path, "script.js")
|
|
|
|
head += f'<script type="text/javascript" src="{webpath(script_js)}"></script>\n'
|
|
|
|
|
2024-04-05 12:40:49 +00:00
|
|
|
# We want the utils.js script to be imported first since it can be used by multiple other
|
|
|
|
# scripts. This resolves dependency issues caused by html <script> ordering.
|
|
|
|
js_scripts = scripts.list_scripts("javascript", ".js")
|
|
|
|
js_utils_script = [x for x in js_scripts if x.filename == "utils.js"][0]
|
|
|
|
head += f'<script type="text/javascript" src="{webpath(js_utils_script.path)}"></script>\n'
|
|
|
|
|
|
|
|
# Now add all remaining .js scripts excluding the utils.js file.
|
|
|
|
for script in [x for x in js_scripts if x.filename != "utils.js"]:
|
2023-05-31 19:40:09 +00:00
|
|
|
head += f'<script type="text/javascript" src="{webpath(script.path)}"></script>\n'
|
|
|
|
|
|
|
|
for script in scripts.list_scripts("javascript", ".mjs"):
|
|
|
|
head += f'<script type="module" src="{webpath(script.path)}"></script>\n'
|
|
|
|
|
|
|
|
if shared.cmd_opts.theme:
|
|
|
|
head += f'<script type="text/javascript">set_theme(\"{shared.cmd_opts.theme}\");</script>\n'
|
|
|
|
|
|
|
|
return head
|
|
|
|
|
|
|
|
|
|
|
|
def css_html():
|
|
|
|
head = ""
|
|
|
|
|
|
|
|
def stylesheet(fn):
|
|
|
|
return f'<link rel="stylesheet" property="stylesheet" href="{webpath(fn)}">'
|
|
|
|
|
|
|
|
for cssfile in scripts.list_files_with_name("style.css"):
|
|
|
|
head += stylesheet(cssfile)
|
|
|
|
|
2024-01-03 22:16:58 +00:00
|
|
|
user_css = os.path.join(data_path, "user.css")
|
|
|
|
if os.path.exists(user_css):
|
|
|
|
head += stylesheet(user_css)
|
2023-05-31 19:40:09 +00:00
|
|
|
|
|
|
|
return head
|
|
|
|
|
|
|
|
|
|
|
|
def reload_javascript():
|
|
|
|
js = javascript_html()
|
|
|
|
css = css_html()
|
|
|
|
|
|
|
|
def template_response(*args, **kwargs):
|
|
|
|
res = shared.GradioTemplateResponseOriginal(*args, **kwargs)
|
|
|
|
res.body = res.body.replace(b'</head>', f'{js}</head>'.encode("utf8"))
|
|
|
|
res.body = res.body.replace(b'</body>', f'{css}</body>'.encode("utf8"))
|
|
|
|
res.init_headers()
|
|
|
|
return res
|
|
|
|
|
|
|
|
gr.routes.templates.TemplateResponse = template_response
|
|
|
|
|
|
|
|
|
|
|
|
if not hasattr(shared, 'GradioTemplateResponseOriginal'):
|
|
|
|
shared.GradioTemplateResponseOriginal = gr.routes.templates.TemplateResponse
|