Skip to content

Commit 93f8982

Browse files
committed
refactor:引入Dash特性flexible callback signatures重构前端复杂的回调,增加所有回调注释,提高回调逻辑可读性 #I825WG
feat:新增表单动态回写及校验,将表单字段id与数据库字段名一一对应并设置校验字段即可实现
1 parent 3dcb5bd commit 93f8982

37 files changed

+4123
-2627
lines changed

dash-fastapi-frontend/app.py

Lines changed: 120 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,27 @@
7676

7777

7878
@app.callback(
79-
[Output('app-mount', 'children'),
80-
Output('redirect-container', 'children', allow_duplicate=True),
81-
Output('global-message-container', 'children', allow_duplicate=True),
82-
Output('api-check-token', 'data', allow_duplicate=True),
83-
Output('current-key-container', 'data'),
84-
Output('menu-info-store-container', 'data'),
85-
Output('menu-list-store-container', 'data'),
86-
Output('search-panel', 'data')],
87-
Input('url-container', 'pathname'),
88-
[State('url-container', 'trigger'),
89-
State('token-container', 'data')],
79+
output=dict(
80+
app_mount=Output('app-mount', 'children'),
81+
redirect_container=Output('redirect-container', 'children', allow_duplicate=True),
82+
global_message_container=Output('global-message-container', 'children', allow_duplicate=True),
83+
api_check_token_trigger=Output('api-check-token', 'data', allow_duplicate=True),
84+
menu_current_key=Output('current-key-container', 'data'),
85+
menu_info=Output('menu-info-store-container', 'data'),
86+
menu_list=Output('menu-list-store-container', 'data'),
87+
search_panel_data=Output('search-panel', 'data')
88+
),
89+
inputs=dict(pathname=Input('url-container', 'pathname')),
90+
state=dict(
91+
url_trigger=State('url-container', 'trigger'),
92+
session_token=State('token-container', 'data')
93+
),
9094
prevent_initial_call=True
9195
)
92-
def router(pathname, trigger, session_token):
96+
def router(pathname, url_trigger, session_token):
97+
"""
98+
全局路由回调
99+
"""
93100
# 检查当前会话是否已经登录
94101
token_result = session.get('Authorization')
95102
# 若已登录
@@ -115,142 +122,136 @@ def router(pathname, trigger, session_token):
115122
current_key = '首页'
116123
if pathname == '/user/profile':
117124
current_key = '个人资料'
118-
if trigger == 'load':
125+
if url_trigger == 'load':
119126

120127
# 根据pathname控制渲染行为
121128
if pathname == '/login' or pathname == '/forget':
122129
# 重定向到主页面
123-
return [
124-
dash.no_update,
125-
dcc.Location(
126-
pathname='/',
127-
id='router-redirect'
128-
),
129-
None,
130-
{'timestamp': time.time()},
131-
{'current_key': current_key},
132-
{'menu_info': menu_info},
133-
{'menu_list': menu_list},
134-
search_panel_data
135-
]
130+
return dict(
131+
app_mount=dash.no_update,
132+
redirect_container=dcc.Location(pathname='/', id='router-redirect'),
133+
global_message_container=None,
134+
api_check_token_trigger={'timestamp': time.time()},
135+
menu_current_key={'current_key': current_key},
136+
menu_info={'menu_info': menu_info},
137+
menu_list={'menu_list': menu_list},
138+
search_panel_data=search_panel_data
139+
)
136140

137141
# 否则正常渲染主页面
138-
return [
139-
views.layout.render_content(user_menu_info),
140-
None,
141-
fuc.FefferyFancyNotification('进入主页面', type='success', autoClose=2000),
142-
{'timestamp': time.time()},
143-
{'current_key': current_key},
144-
{'menu_info': menu_info},
145-
{'menu_list': menu_list},
146-
search_panel_data
147-
]
142+
return dict(
143+
app_mount=views.layout.render_content(user_menu_info),
144+
redirect_container=None,
145+
global_message_container=None,
146+
api_check_token_trigger={'timestamp': time.time()},
147+
menu_current_key={'current_key': current_key},
148+
menu_info={'menu_info': menu_info},
149+
menu_list={'menu_list': menu_list},
150+
search_panel_data=search_panel_data
151+
)
148152

149153
else:
150-
return [
151-
dash.no_update,
152-
None,
153-
None,
154-
{'timestamp': time.time()},
155-
{'current_key': current_key},
156-
{'menu_info': menu_info},
157-
{'menu_list': menu_list},
158-
search_panel_data
159-
]
154+
return dict(
155+
app_mount=dash.no_update,
156+
redirect_container=None,
157+
global_message_container=None,
158+
api_check_token_trigger={'timestamp': time.time()},
159+
menu_current_key={'current_key': current_key},
160+
menu_info={'menu_info': menu_info},
161+
menu_list={'menu_list': menu_list},
162+
search_panel_data=search_panel_data
163+
)
160164

161165
else:
162166
# 渲染404状态页
163-
return [
164-
views.page_404.render_content(),
165-
None,
166-
None,
167-
{'timestamp': time.time()},
168-
dash.no_update,
169-
dash.no_update,
170-
dash.no_update,
171-
dash.no_update
172-
]
167+
return dict(
168+
app_mount=views.page_404.render_content(),
169+
redirect_container=None,
170+
global_message_container=None,
171+
api_check_token_trigger={'timestamp': time.time()},
172+
menu_current_key=dash.no_update,
173+
menu_info=dash.no_update,
174+
menu_list=dash.no_update,
175+
search_panel_data=dash.no_update
176+
)
173177

174178
else:
175-
return [
176-
dash.no_update,
177-
dash.no_update,
178-
dash.no_update,
179-
{'timestamp': time.time()},
180-
dash.no_update,
181-
dash.no_update,
182-
dash.no_update,
183-
dash.no_update
184-
]
179+
return dict(
180+
app_mount=dash.no_update,
181+
redirect_container=dash.no_update,
182+
global_message_container=dash.no_update,
183+
api_check_token_trigger={'timestamp': time.time()},
184+
menu_current_key=dash.no_update,
185+
menu_info=dash.no_update,
186+
menu_list=dash.no_update,
187+
search_panel_data=dash.no_update
188+
)
185189

186190
except Exception as e:
187191
print(e)
188192

189-
return [
190-
dash.no_update,
191-
None,
192-
fuc.FefferyFancyNotification('接口异常', type='error', autoClose=2000),
193-
{'timestamp': time.time()},
194-
dash.no_update,
195-
dash.no_update,
196-
dash.no_update,
197-
dash.no_update
198-
]
193+
return dict(
194+
app_mount=dash.no_update,
195+
redirect_container=None,
196+
global_message_container=fuc.FefferyFancyNotification('接口异常', type='error', autoClose=2000),
197+
api_check_token_trigger={'timestamp': time.time()},
198+
menu_current_key=dash.no_update,
199+
menu_info=dash.no_update,
200+
menu_list=dash.no_update,
201+
search_panel_data=dash.no_update
202+
)
199203
else:
200204
# 若未登录
201205
# 根据pathname控制渲染行为
202206
# 检验pathname合法性
203207
if pathname not in RouterConfig.BASIC_VALID_PATHNAME:
204208
# 渲染404状态页
205-
return [
206-
views.page_404.render_content(),
207-
None,
208-
None,
209-
{'timestamp': time.time()},
210-
dash.no_update,
211-
dash.no_update,
212-
dash.no_update,
213-
dash.no_update
214-
]
209+
return dict(
210+
app_mount=views.page_404.render_content(),
211+
redirect_container=None,
212+
global_message_container=None,
213+
api_check_token_trigger={'timestamp': time.time()},
214+
menu_current_key=dash.no_update,
215+
menu_info=dash.no_update,
216+
menu_list=dash.no_update,
217+
search_panel_data=dash.no_update
218+
)
215219

216220
if pathname == '/login':
217-
return [
218-
views.login.render_content(),
219-
None,
220-
None,
221-
{'timestamp': time.time()},
222-
dash.no_update,
223-
dash.no_update,
224-
dash.no_update,
225-
dash.no_update
226-
]
221+
return dict(
222+
app_mount=views.login.render_content(),
223+
redirect_container=None,
224+
global_message_container=None,
225+
api_check_token_trigger={'timestamp': time.time()},
226+
menu_current_key=dash.no_update,
227+
menu_info=dash.no_update,
228+
menu_list=dash.no_update,
229+
search_panel_data=dash.no_update
230+
)
227231

228232
if pathname == '/forget':
229-
return [
230-
views.forget.render_forget_content(),
231-
None,
232-
None,
233-
{'timestamp': time.time()},
234-
dash.no_update,
235-
dash.no_update,
236-
dash.no_update,
237-
dash.no_update
238-
]
233+
return dict(
234+
app_mount=views.forget.render_forget_content(),
235+
redirect_container=None,
236+
global_message_container=None,
237+
api_check_token_trigger={'timestamp': time.time()},
238+
menu_current_key=dash.no_update,
239+
menu_info=dash.no_update,
240+
menu_list=dash.no_update,
241+
search_panel_data=dash.no_update
242+
)
239243

240244
# 否则重定向到登录页
241-
return [
242-
dash.no_update,
243-
dcc.Location(
244-
pathname='/login',
245-
id='router-redirect'
246-
),
247-
None,
248-
{'timestamp': time.time()},
249-
dash.no_update,
250-
dash.no_update,
251-
dash.no_update,
252-
dash.no_update
253-
]
245+
return dict(
246+
app_mount=dash.no_update,
247+
redirect_container=dcc.Location(pathname='/login', id='router-redirect'),
248+
global_message_container=None,
249+
api_check_token_trigger={'timestamp': time.time()},
250+
menu_current_key=dash.no_update,
251+
menu_info=dash.no_update,
252+
menu_list=dash.no_update,
253+
search_panel_data=dash.no_update
254+
)
254255

255256

256257
if __name__ == '__main__':

0 commit comments

Comments
 (0)