feat(frontend): 优化设置页面并添加数据刷新功能
- 为 get_providers 函数添加异常处理,提高数据获取的稳定性 - 在设置页面添加刷新按钮,用户可手动触发数据刷新 - 优化页面布局,调整组件间距和对齐方式
This commit is contained in:
parent
d40f5b1f24
commit
286db405ca
@ -1,21 +1,23 @@
|
|||||||
import gradio as gr
|
import gradio as gr
|
||||||
from typing import List, Dict
|
from typing import List
|
||||||
from sqlmodel import Session, select
|
from sqlmodel import Session, select
|
||||||
from db import get_engine
|
from db import get_engine
|
||||||
from schema import APIProvider
|
from schema import APIProvider
|
||||||
import os
|
import os
|
||||||
|
|
||||||
# 获取数据库引擎
|
|
||||||
engine = get_engine(os.path.join(os.path.dirname(__file__), "..", "workdir"))
|
engine = get_engine(os.path.join(os.path.dirname(__file__), "..", "workdir"))
|
||||||
|
|
||||||
def setting_page():
|
def setting_page():
|
||||||
def get_providers() -> List[List[str]]:
|
def get_providers() -> List[List[str]]:
|
||||||
|
try: # 添加异常处理
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
providers = session.exec(select(APIProvider)).all()
|
providers = session.exec(select(APIProvider)).all()
|
||||||
return [
|
return [
|
||||||
[p.id, p.model_id, p.base_url, p.api_key or ""]
|
[p.id, p.model_id, p.base_url, p.api_key or ""]
|
||||||
for p in providers
|
for p in providers
|
||||||
]
|
]
|
||||||
|
except Exception as e:
|
||||||
|
raise gr.Error(f"获取数据失败: {str(e)}")
|
||||||
|
|
||||||
def add_provider(model_id, base_url, api_key):
|
def add_provider(model_id, base_url, api_key):
|
||||||
try:
|
try:
|
||||||
@ -30,7 +32,6 @@ def setting_page():
|
|||||||
session.refresh(new_provider)
|
session.refresh(new_provider)
|
||||||
return get_providers()
|
return get_providers()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# 抛出错误提示
|
|
||||||
raise gr.Error(f"添加失败: {str(e)}")
|
raise gr.Error(f"添加失败: {str(e)}")
|
||||||
|
|
||||||
with gr.Blocks() as demo:
|
with gr.Blocks() as demo:
|
||||||
@ -38,26 +39,32 @@ def setting_page():
|
|||||||
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
with gr.Column(scale=1):
|
with gr.Column(scale=1):
|
||||||
|
|
||||||
model_id_input = gr.Textbox(label="Model ID")
|
model_id_input = gr.Textbox(label="Model ID")
|
||||||
base_url_input = gr.Textbox(label="Base URL")
|
base_url_input = gr.Textbox(label="Base URL")
|
||||||
api_key_input = gr.Textbox(label="API Key")
|
api_key_input = gr.Textbox(label="API Key")
|
||||||
add_button = gr.Button("添加新API")
|
add_button = gr.Button("添加新API")
|
||||||
|
|
||||||
# API Provider列表
|
|
||||||
with gr.Column(scale=3):
|
with gr.Column(scale=3):
|
||||||
provider_table = gr.DataFrame(
|
provider_table = gr.DataFrame(
|
||||||
headers=["id" , "model id", "base URL", "API Key"],
|
headers=["id", "model id", "base URL", "API Key"],
|
||||||
datatype=["number","str", "str", "str"],
|
datatype=["number", "str", "str", "str"],
|
||||||
interactive=True,
|
interactive=True,
|
||||||
value=get_providers(),
|
value=get_providers(),
|
||||||
wrap=True,
|
wrap=True,
|
||||||
col_count=(4, "auto")
|
col_count=(4, "auto")
|
||||||
)
|
)
|
||||||
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
edit_button = gr.Button("编辑选中行")
|
edit_button = gr.Button("编辑选中行")
|
||||||
delete_button = gr.Button("删除选中行")
|
delete_button = gr.Button("删除选中行")
|
||||||
|
refresh_button = gr.Button("刷新数据", variant="secondary")
|
||||||
|
|
||||||
|
# 绑定刷新按钮事件
|
||||||
|
refresh_button.click(
|
||||||
|
fn=get_providers,
|
||||||
|
outputs=[provider_table],
|
||||||
|
queue=False # 立即刷新不需要排队
|
||||||
|
)
|
||||||
|
|
||||||
add_button.click(
|
add_button.click(
|
||||||
fn=add_provider,
|
fn=add_provider,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user