From d818c38dfed87624ed4e0dfef4c1a07bfc30a0e9 Mon Sep 17 00:00:00 2001 From: binary-husky Date: Sun, 21 Jan 2024 19:41:18 +0800 Subject: [PATCH] better theme --- themes/common.py | 21 +++++++++++++++++++++ themes/contrast.py | 19 +++---------------- themes/default.py | 18 ++---------------- themes/gradios.py | 20 +++----------------- themes/green.py | 18 ++---------------- themes/mermaid_loader.js | 2 +- 6 files changed, 32 insertions(+), 66 deletions(-) create mode 100644 themes/common.py diff --git a/themes/common.py b/themes/common.py new file mode 100644 index 0000000..08f8561 --- /dev/null +++ b/themes/common.py @@ -0,0 +1,21 @@ +from toolbox import get_conf +CODE_HIGHLIGHT, ADD_WAIFU, LAYOUT = get_conf("CODE_HIGHLIGHT", "ADD_WAIFU", "LAYOUT") + +def get_common_html_javascript_code(): + js = "\n" + for jsf in [ + "file=themes/common.js", + "file=themes/mermaid.min.js", + "file=themes/mermaid_loader.js", + ]: + js += f"""\n""" + + # 添加Live2D + if ADD_WAIFU: + for jsf in [ + "file=docs/waifu_plugin/jquery.min.js", + "file=docs/waifu_plugin/jquery-ui.min.js", + "file=docs/waifu_plugin/autoload.js", + ]: + js += f"""\n""" + return js \ No newline at end of file diff --git a/themes/contrast.py b/themes/contrast.py index 9a4b56f..1e98837 100644 --- a/themes/contrast.py +++ b/themes/contrast.py @@ -67,22 +67,9 @@ def adjust_theme(): button_cancel_text_color_dark="white", ) - js = "" - for jsf in [ - os.path.join(theme_dir, "common.js"), - os.path.join(theme_dir, "mermaid.min.js"), - os.path.join(theme_dir, "mermaid_loader.js"), - ]: - with open(jsf, "r", encoding="utf8") as f: - js += f"" - - # 添加一个萌萌的看板娘 - if ADD_WAIFU: - js += """ - - - - """ + from themes.common import get_common_html_javascript_code + js = get_common_html_javascript_code() + if not hasattr(gr, "RawTemplateResponse"): gr.RawTemplateResponse = gr.routes.templates.TemplateResponse gradio_original_template_fn = gr.RawTemplateResponse diff --git a/themes/default.py b/themes/default.py index b8e9431..a65b011 100644 --- a/themes/default.py +++ b/themes/default.py @@ -67,22 +67,8 @@ def adjust_theme(): button_cancel_text_color_dark="white", ) - js = "" - for jsf in [ - os.path.join(theme_dir, "common.js"), - os.path.join(theme_dir, "mermaid.min.js"), - os.path.join(theme_dir, "mermaid_loader.js"), - ]: - with open(jsf, "r", encoding="utf8") as f: - js += f"" - - # 添加一个萌萌的看板娘 - if ADD_WAIFU: - js += """ - - - - """ + from themes.common import get_common_html_javascript_code + js = get_common_html_javascript_code() if not hasattr(gr, "RawTemplateResponse"): gr.RawTemplateResponse = gr.routes.templates.TemplateResponse gradio_original_template_fn = gr.RawTemplateResponse diff --git a/themes/gradios.py b/themes/gradios.py index 68f15df..14d88a2 100644 --- a/themes/gradios.py +++ b/themes/gradios.py @@ -31,23 +31,9 @@ def adjust_theme(): THEME = THEME.lstrip("huggingface-") set_theme = set_theme.from_hub(THEME.lower()) - js = "" - for jsf in [ - os.path.join(theme_dir, "common.js"), - os.path.join(theme_dir, "mermaid.min.js"), - os.path.join(theme_dir, "mermaid_loader.js"), - ]: - with open(jsf, "r", encoding="utf8") as f: - js += f"" - - - # 添加一个萌萌的看板娘 - if ADD_WAIFU: - js += """ - - - - """ + from themes.common import get_common_html_javascript_code + js = get_common_html_javascript_code() + if not hasattr(gr, "RawTemplateResponse"): gr.RawTemplateResponse = gr.routes.templates.TemplateResponse gradio_original_template_fn = gr.RawTemplateResponse diff --git a/themes/green.py b/themes/green.py index 8428741..b16249a 100644 --- a/themes/green.py +++ b/themes/green.py @@ -76,22 +76,8 @@ def adjust_theme(): chatbot_code_background_color_dark="*neutral_950", ) - js = "" - for jsf in [ - os.path.join(theme_dir, "common.js"), - os.path.join(theme_dir, "mermaid.min.js"), - os.path.join(theme_dir, "mermaid_loader.js"), - ]: - with open(jsf, "r", encoding="utf8") as f: - js += f"" - - # 添加一个萌萌的看板娘 - if ADD_WAIFU: - js += """ - - - - """ + from themes.common import get_common_html_javascript_code + js = get_common_html_javascript_code() with open(os.path.join(theme_dir, "green.js"), "r", encoding="utf8") as f: js += f"" diff --git a/themes/mermaid_loader.js b/themes/mermaid_loader.js index 443d636..36304f2 100644 --- a/themes/mermaid_loader.js +++ b/themes/mermaid_loader.js @@ -106,7 +106,7 @@ const uml = async className => { defaultConfig.theme = "dark" } - const Module = await import('./file=themes/mermaid_editor.js'); + const Module = await import('/file=themes/mermaid_editor.js'); function do_render(block, code, codeContent, cnt) { var rendered_content = mermaid.render(`_diagram_${cnt}`, code);