From ddd2fd84da3321e4d542e525f1737cc172c5242b Mon Sep 17 00:00:00 2001 From: binary-husky Date: Tue, 2 Apr 2024 19:42:55 +0800 Subject: [PATCH] fix checkbox bugs --- main.py | 4 +- themes/common.js | 100 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 83 insertions(+), 21 deletions(-) diff --git a/main.py b/main.py index 1cbc400..6789ef1 100644 --- a/main.py +++ b/main.py @@ -24,7 +24,7 @@ def main(): CHATBOT_HEIGHT, LAYOUT, AVAIL_LLM_MODELS, AUTO_CLEAR_TXT = get_conf('CHATBOT_HEIGHT', 'LAYOUT', 'AVAIL_LLM_MODELS', 'AUTO_CLEAR_TXT') ENABLE_AUDIO, AUTO_CLEAR_TXT, PATH_LOGGING, AVAIL_THEMES, THEME, ADD_WAIFU = get_conf('ENABLE_AUDIO', 'AUTO_CLEAR_TXT', 'PATH_LOGGING', 'AVAIL_THEMES', 'THEME', 'ADD_WAIFU') NUM_CUSTOM_BASIC_BTN, SSL_KEYFILE, SSL_CERTFILE = get_conf('NUM_CUSTOM_BASIC_BTN', 'SSL_KEYFILE', 'SSL_CERTFILE') - DARK_MODE, INIT_SYS_PROMPT = get_conf('DARK_MODE', 'INIT_SYS_PROMPT') + DARK_MODE, INIT_SYS_PROMPT, ADD_WAIFU = get_conf('DARK_MODE', 'INIT_SYS_PROMPT', 'ADD_WAIFU') # 如果WEB_PORT是-1, 则随机选取WEB端口 PORT = find_free_port() if WEB_PORT <= 0 else WEB_PORT @@ -379,7 +379,7 @@ def main(): demo.load(init_cookie, inputs=[cookies], outputs=[cookies]) demo.load(persistent_cookie_reload, inputs = [py_pickle_cookie, cookies], outputs = [py_pickle_cookie, cookies, *customize_btns.values(), *predefined_btns.values()], _js=js_code_for_persistent_cookie_init) - demo.load(None, inputs=[], outputs=None, _js=f"""()=>init_frontend_with_cookies("{DARK_MODE}","{INIT_SYS_PROMPT}")""") # 配置暗色主题或亮色主题 + demo.load(None, inputs=[], outputs=None, _js=f"""()=>init_frontend_with_cookies("{DARK_MODE}","{INIT_SYS_PROMPT}","{ADD_WAIFU}")""") # 配置暗色主题或亮色主题 demo.load(None, inputs=[gr.Textbox(LAYOUT, visible=False)], outputs=None, _js='(LAYOUT)=>{GptAcademicJavaScriptInit(LAYOUT);}') # gradio的inbrowser触发不太稳定,回滚代码到原始的浏览器打开函数 diff --git a/themes/common.js b/themes/common.js index 118e3e8..e3de453 100644 --- a/themes/common.js +++ b/themes/common.js @@ -5,7 +5,10 @@ function push_data_to_gradio_component(DAT, ELEM_ID, TYPE){ // type, // type==="str" / type==="float" if (TYPE=="str"){ - // convert dat to string: do nothign + // convert dat to string: do nothing + } + else if (TYPE=="no_conversion"){ + // no nothing } else if (TYPE=="float"){ // convert dat to float @@ -20,6 +23,56 @@ function push_data_to_gradio_component(DAT, ELEM_ID, TYPE){ window.dispatchEvent(myEvent); } + +async function get_gradio_component(ELEM_ID){ + function waitFor(ELEM_ID) { + return new Promise((resolve) => { + const myEvent = new CustomEvent('gpt_academic_get_gradio_component_value', { + detail: { + elem_id: ELEM_ID, + resolve, + } + }); + window.dispatchEvent(myEvent); + }); + } + result = await waitFor(ELEM_ID); + return result; +} + + +async function get_data_from_gradio_component(ELEM_ID){ + let comp = await get_gradio_component(ELEM_ID); + return comp.props.value; +} + +function update_array(arr, item, mode) { + // let p = ["基础功能区", "输入清除键", "函数插件区"]; + + // // Remove "输入清除键" + // p = updateArray(p, "输入清除键", "remove"); + // console.log(p); // Should log: ["基础功能区", "函数插件区"] + + // // Add "输入清除键" + // p = updateArray(p, "输入清除键", "add"); + // console.log(p); // Should log: ["基础功能区", "函数插件区", "输入清除键"] + + const index = arr.indexOf(item); + if (mode === "remove") { + if (index !== -1) { + // Item found, remove it + arr.splice(index, 1); + } + } else if (mode === "add") { + if (index === -1) { + // Item not found, add it + arr.push(item); + } + } + return arr; +} + + function gradioApp() { // https://github.com/GaiZhenbiao/ChuanhuChatGPT/tree/main/web_assets/javascript const elems = document.getElementsByTagName('gradio-app'); @@ -844,7 +897,7 @@ function gpt_academic_gradio_saveload( } } -function init_frontend_with_cookies(dark, prompt) { +async function init_frontend_with_cookies(dark, prompt, live2d) { let searchString = "输入清除键"; let bool_value = "False"; @@ -874,23 +927,29 @@ function init_frontend_with_cookies(dark, prompt) { bool_value = getCookie("js_clearbtn_show_cookie") bool_value = bool_value == "True"; searchString = "输入清除键"; + if (bool_value) { - let clearButton = document.getElementById("elem_clear"); - let clearButton2 = document.getElementById("elem_clear2"); - clearButton.style.display = "block"; - clearButton2.style.display = "block"; - set_checkbox(searchString, true); + // make btns appear + let clearButton = document.getElementById("elem_clear"); clearButton.style.display = "block"; + let clearButton2 = document.getElementById("elem_clear2"); clearButton2.style.display = "block"; + // deal with checkboxes + let arr_with_clear_btn = update_array( + await get_data_from_gradio_component('cbs'), "输入清除键", "add" + ) + push_data_to_gradio_component(arr_with_clear_btn, "cbs", "no_conversion"); } else { - let clearButton = document.getElementById("elem_clear"); - let clearButton2 = document.getElementById("elem_clear2"); - clearButton.style.display = "none"; - clearButton2.style.display = "none"; - set_checkbox(searchString, false); + // make btns disappear + let clearButton = document.getElementById("elem_clear"); clearButton.style.display = "none"; + let clearButton2 = document.getElementById("elem_clear2"); clearButton2.style.display = "none"; + // deal with checkboxes + let arr_without_clear_btn = update_array( + await get_data_from_gradio_component('cbs'), "输入清除键", "remove" + ) + push_data_to_gradio_component(arr_without_clear_btn, "cbs", "no_conversion"); } } ////////////////////// live2d /////////////////////////// - if (getCookie("js_live2d_show_cookie")) { // have cookie searchString = "添加Live2D形象"; @@ -898,20 +957,23 @@ function init_frontend_with_cookies(dark, prompt) { bool_value = bool_value == "True"; if (bool_value) { loadLive2D(); - set_checkbox(searchString, true); + let arr_with_live2d = update_array( + await get_data_from_gradio_component('cbsc'), "添加Live2D形象", "add" + ) + push_data_to_gradio_component(arr_with_live2d, "cbsc", "no_conversion"); } else { try { $('.waifu').hide(); - set_checkbox(searchString, false); + let arr_without_live2d = update_array( + await get_data_from_gradio_component('cbsc'), "添加Live2D形象", "remove" + ) + push_data_to_gradio_component(arr_without_live2d, "cbsc", "no_conversion"); } catch (error) { } } } else { // do not have cookie - // get conf - display_panel_arr = get_checkbox_selected_items("cbsc"); - searchString = "添加Live2D形象"; - if (display_panel_arr.includes(searchString)) { + if (live2d) { loadLive2D(); } else { }