Skip to content

Commit

Permalink
Merge pull request #765 from Ikaros-521/owner
Browse files Browse the repository at this point in the history
配置模板功能优化
  • Loading branch information
Ikaros-521 authored Apr 16, 2024
2 parents bb8c33b + 1d706ed commit 7b7a300
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,8 @@ def config_template_save(file_path: str):
with open(config_path, 'r', encoding="utf-8") as config_file:
config_data = json.load(config_file)

config_data = webui_config_to_dict(config_data)

# 将JSON数据保存到文件中
with open(file_path, "w", encoding="utf-8") as file:
json.dump(config_data, file, indent=2, ensure_ascii=False)
Expand Down Expand Up @@ -1289,21 +1291,13 @@ def check_config():

return True

# 保存配置
def save_config():
global config, config_path

# 配置检查
if not check_config():
return
# 读取webui配置到dict变量
def webui_config_to_dict(config_data):
"""读取webui配置到dict变量
try:
with open(config_path, 'r', encoding="utf-8") as config_file:
config_data = json.load(config_file)
except Exception as e:
logging.error(f"无法读取配置文件!\n{e}")
ui.notify(position="top", type="negative", message=f"无法读取配置文件!{e}")
return False
Args:
config_data (dict): 从本地配置文件读取的dict数据
"""

def common_textarea_handle(content):
"""通用的textEdit 多行文本内容处理
Expand Down Expand Up @@ -2478,11 +2472,34 @@ def common_textarea_handle(content):
config_data["login"]["username"] = input_login_username.value
config_data["login"]["password"] = input_login_password.value

return config_data
except Exception as e:
logging.error(f"无法写入配置文件\n{e}")
ui.notify(position="top", type="negative", message=f"无法写入配置文件\n{e}")
logging.error(f"无法读取webui配置到变量\n{e}")
ui.notify(position="top", type="negative", message=f"无法读取webui配置到变量\n{e}")
logging.error(traceback.format_exc())

return None

# 保存配置
def save_config():
global config, config_path

# 配置检查
if not check_config():
return False

try:
with open(config_path, 'r', encoding="utf-8") as config_file:
config_data = json.load(config_file)
except Exception as e:
logging.error(f"无法读取配置文件!\n{e}")
ui.notify(position="top", type="negative", message=f"无法读取配置文件!{e}")
return False

# 读取webui配置到dict变量
config_data = webui_config_to_dict(config_data)
if config_data is None:
return False

# 写入配置到配置文件
try:
Expand Down Expand Up @@ -5359,7 +5376,7 @@ def update_echart_gift():
with ui.row():
input_config_template_path = ui.input(label='配置模板路径', value="", placeholder='输入你需要加载或保存的配置文件路径,例如:直播带货.json')

button_config_template_save = ui.button('保存配置到文件', on_click=lambda: config_template_save(input_config_template_path.value), color=button_internal_color).style(button_internal_css)
button_config_template_save = ui.button('保存webui配置到文件', on_click=lambda: config_template_save(input_config_template_path.value), color=button_internal_color).style(button_internal_css)
button_config_template_load = ui.button('读取模板到本地(慎点)', on_click=lambda: config_template_load(input_config_template_path.value), color=button_internal_color).style(button_internal_css)


Expand Down

0 comments on commit 7b7a300

Please sign in to comment.