aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorJames Tolton <jjtolton@gmail.com>2023-01-21 12:59:05 -0500
committerJames Tolton <jjtolton@gmail.com>2023-01-21 12:59:05 -0500
commitf726df8a2fd2620ba245de5702e2afe620f79b91 (patch)
treef1a2acf0729d5f0513993d78eb1d9c1e001cb7cb /modules
parenta2749ec655af93d96ea7ebed85e8c1e7c5072b02 (diff)
Compile and serve js from /statica instead of inline in html
Diffstat (limited to 'modules')
-rw-r--r--modules/ui.py35
1 files changed, 30 insertions, 5 deletions
diff --git a/modules/ui.py b/modules/ui.py
index fbc3efa0..d19eaf25 100644
--- a/modules/ui.py
+++ b/modules/ui.py
@@ -10,6 +10,7 @@ import sys
import tempfile
import time
import traceback
+from collections import OrderedDict
from functools import partial, reduce
import warnings
@@ -1918,27 +1919,51 @@ def create_ui():
def reload_javascript():
+ javascript_files = OrderedDict()
with open(os.path.join(script_path, "script.js"), "r", encoding="utf8") as jsfile:
- javascript = f'<script>{jsfile.read()}</script>'
+ contents = jsfile.read()
+ javascript_files["script.js"] = [contents]
+ # javascript = f'<script>{contents}</script>'
scripts_list = modules.scripts.list_scripts("javascript", ".js")
for basedir, filename, path in scripts_list:
with open(path, "r", encoding="utf8") as jsfile:
- javascript += f"\n<!-- {filename} --><script>{jsfile.read()}</script>"
+ contents = jsfile.read()
+ javascript_files[filename] = [contents]
+ # javascript += f"\n<!-- {filename} --><script>{contents}</script>"
if cmd_opts.theme is not None:
- javascript += f"\n<script>set_theme('{cmd_opts.theme}');</script>\n"
+ javascript_files["theme.js"] = [f"set_theme('{cmd_opts.theme}');"]
+ # javascript += f"\n<script>set_theme('{cmd_opts.theme}');</script>\n"
- javascript += f"\n<script>{localization.localization_js(shared.opts.localization)}</script>"
+ # javascript += f"\n<script>{localization.localization_js(shared.opts.localization)}</script>"
+ javascript_files["localization.js"] = [f"{localization.localization_js(shared.opts.localization)}"]
+
+ compiled_name = "webui-compiled.js"
+ head = f"""
+ <script src=/statica/{compiled_name}?{int(time.time())} type="text/javascript"></script>
+ """
def template_response(*args, **kwargs):
res = shared.GradioTemplateResponseOriginal(*args, **kwargs)
res.body = res.body.replace(
- b'</head>', f'{javascript}</head>'.encode("utf8"))
+ b'</head>', f'{head}</head>'.encode("utf8"))
res.init_headers()
return res
+ for k in javascript_files:
+ javascript_files[k] = "\n".join(javascript_files[k])
+
+ # make static_path if not exists
+ statica_path = os.path.join(script_path, 'statica')
+ if not os.path.exists(statica_path):
+ os.mkdir(statica_path)
+
+ javascript_out = "\n\n\n".join([f"// <!-- {k} -->\n\n{v}" for k, v in javascript_files.items()])
+ with open(os.path.join(script_path, "statica", compiled_name), "w", encoding="utf8") as jsfile:
+ jsfile.write(javascript_out)
+
gradio.routes.templates.TemplateResponse = template_response