Skip to content

Commit 8ae012d

Browse files
committed
perf:修改部分服务端回调为浏览器端回调,提高性能
1 parent f65a387 commit 8ae012d

19 files changed

Lines changed: 408 additions & 258 deletions

File tree

dash-fastapi-frontend/callbacks/app_c.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,17 @@ def get_primary_color(_, custom_color):
6464
return dash.no_update
6565

6666

67-
@app.callback(
67+
app.clientside_callback(
68+
'''
69+
(system_color, custom_color) => {
70+
if (custom_color) {
71+
return custom_color;
72+
}
73+
return system_color;
74+
}
75+
''',
6876
Output('app-config-provider', 'primaryColor'),
6977
[Input('system-app-primary-color-container', 'data'),
7078
Input('custom-app-primary-color-container', 'data')],
7179
prevent_initial_call=True
7280
)
73-
def render_app_primary_color(system_color, custom_color):
74-
if custom_color:
75-
return custom_color
76-
77-
return system_color

dash-fastapi-frontend/callbacks/forget_c.py

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -121,75 +121,82 @@ def login_auth(nClicks, username, password, password_again, input_captcha, sessi
121121

122122
@app.callback(
123123
[Output('message-code-count-down', 'delay'),
124-
Output('get-message-code', 'disabled'),
124+
Output('get-message-code', 'disabled', allow_duplicate=True),
125125
Output('sms_code-session_id-container', 'data'),
126126
Output('global-message-container', 'children', allow_duplicate=True)],
127-
[Input('get-message-code', 'nClicks'),
128-
Input('message-code-count-down', 'countdown')],
127+
Input('get-message-code', 'nClicks'),
129128
[State('forget-username', 'value'),
130129
State('sms_code-session_id-container', 'data')],
131130
prevent_initial_call=True
132131
)
133-
def message_countdown(nClicks, countdown, username, session_id):
132+
def message_countdown(nClicks, username, session_id):
134133
if nClicks:
135134

136-
if dash.ctx.triggered_id == 'get-message-code':
137-
138-
try:
139-
if username:
140-
send_result = send_message_api(dict(user_name=username, session_id=session_id))
141-
if send_result.get('code') == 200:
142-
143-
return [
144-
120,
145-
True,
146-
send_result.get('data').get('session_id'),
147-
fuc.FefferyFancyMessage(send_result.get('message'), type='success')
148-
]
149-
else:
150-
151-
return [
152-
dash.no_update,
153-
False,
154-
dash.no_update,
155-
fuc.FefferyFancyMessage(send_result.get('message'), type='error')
156-
]
135+
try:
136+
if username:
137+
send_result = send_message_api(dict(user_name=username, session_id=session_id))
138+
if send_result.get('code') == 200:
157139

140+
return [
141+
120,
142+
True,
143+
send_result.get('data').get('session_id'),
144+
fuc.FefferyFancyMessage(send_result.get('message'), type='success')
145+
]
158146
else:
147+
159148
return [
160149
dash.no_update,
161150
False,
162151
dash.no_update,
163-
fuc.FefferyFancyMessage('请输入用户名', type='error')
152+
fuc.FefferyFancyMessage(send_result.get('message'), type='error')
164153
]
165154

166-
except Exception as e:
167-
155+
else:
168156
return [
169157
dash.no_update,
170158
False,
171159
dash.no_update,
172-
fuc.FefferyFancyMessage(str(e), type='error')
160+
fuc.FefferyFancyMessage('请输入用户名', type='error')
173161
]
174162

175-
if dash.ctx.triggered_id == 'message-code-count-down':
176-
if countdown:
177-
return [
178-
dash.no_update, True, dash.no_update, dash.no_update
179-
]
163+
except Exception as e:
180164

181-
return dash.no_update, False, dash.no_update, dash.no_update
165+
return [
166+
dash.no_update,
167+
False,
168+
dash.no_update,
169+
fuc.FefferyFancyMessage(str(e), type='error')
170+
]
182171

183172
return [dash.no_update] * 4
184173

185174

186-
@app.callback(
187-
Output('get-message-code', 'children'),
175+
app.clientside_callback(
176+
'''
177+
(countdown) => {
178+
if (countdown) {
179+
return true;
180+
}
181+
return false;
182+
}
183+
''',
184+
Output('get-message-code', 'disabled', allow_duplicate=True),
188185
Input('message-code-count-down', 'countdown'),
189186
prevent_initial_call=True
190187
)
191-
def update_button_content(countdown):
192-
if countdown:
193-
return f"获取中{countdown}s"
194188

195-
return "获取验证码"
189+
190+
app.clientside_callback(
191+
'''
192+
(countdown) => {
193+
if (countdown) {
194+
return `获取中${countdown}s`
195+
}
196+
return '获取验证码'
197+
}
198+
''',
199+
Output('get-message-code', 'children'),
200+
Input('message-code-count-down', 'countdown'),
201+
prevent_initial_call=True
202+
)

dash-fastapi-frontend/callbacks/layout_c/aside_c.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import dash
21
from dash.dependencies import Input, Output
32

43
from server import app
54

65

76
# url-pathname控制currentKey回调
8-
@app.callback(
7+
app.clientside_callback(
8+
'''
9+
(data) => {
10+
if (data) {
11+
return data['current_key'];
12+
}
13+
return window.dash_clientside.no_update;
14+
}
15+
''',
916
Output('index-side-menu', 'currentKey'),
1017
Input('current-key-container', 'data'),
1118
prevent_initial_call=True
1219
)
13-
def pathname_update_current_key(data):
14-
if data:
15-
return data['current_key']
16-
17-
return dash.no_update

dash-fastapi-frontend/callbacks/layout_c/head_c.py

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,38 @@
99

1010

1111
# 页首右侧个人中心选项卡回调
12-
@app.callback(
12+
app.clientside_callback(
13+
'''
14+
(nClicks, clickedKey) => {
15+
if (clickedKey == '退出登录') {
16+
return [
17+
window.dash_clientside.no_update,
18+
true,
19+
false
20+
];
21+
} else if (clickedKey == '个人资料') {
22+
return [
23+
'/user/profile',
24+
false,
25+
false
26+
];
27+
} else if ( clickedKey == '布局设置') {
28+
return [
29+
window.dash_clientside.no_update,
30+
false,
31+
true
32+
]
33+
}
34+
return window.dash_clientside.no_update;
35+
}
36+
''',
1337
[Output('dcc-url', 'pathname', allow_duplicate=True),
1438
Output('logout-modal', 'visible'),
1539
Output('layout-setting-drawer', 'visible')],
1640
Input('index-header-dropdown', 'nClicks'),
1741
State('index-header-dropdown', 'clickedKey'),
1842
prevent_initial_call=True
1943
)
20-
def index_dropdown_click(nClicks, clickedKey):
21-
if clickedKey == '退出登录':
22-
return [
23-
dash.no_update,
24-
True,
25-
False
26-
]
27-
28-
elif clickedKey == '个人资料':
29-
return [
30-
'/user/profile',
31-
False,
32-
False
33-
]
34-
35-
elif clickedKey == '布局设置':
36-
return [
37-
dash.no_update,
38-
False,
39-
True
40-
]
41-
42-
return [dash.no_update] * 3
4344

4445

4546
# 退出登录回调
@@ -65,27 +66,35 @@ def logout_confirm(okCounts):
6566

6667

6768
# 全局页面重载回调
68-
@app.callback(
69+
app.clientside_callback(
70+
'''
71+
(nClicks) => {
72+
return true;
73+
}
74+
''',
6975
Output('trigger-reload-output', 'reload'),
7076
Input('index-reload', 'nClicks'),
7177
prevent_initial_call=True
7278
)
73-
def reload_page(nClicks):
74-
return True
7579

7680

7781
# 布局设置回调
78-
@app.callback(
82+
app.clientside_callback(
83+
'''
84+
(visible, custom_color) => {
85+
if (visible) {
86+
if (custom_color) {
87+
return custom_color;
88+
}
89+
}
90+
return window.dash_clientside.no_update;
91+
}
92+
''',
7993
Output('hex-color-picker', 'color', allow_duplicate=True),
8094
Input('layout-setting-drawer', 'visible'),
8195
State('custom-app-primary-color-container', 'data'),
8296
prevent_initial_call=True
8397
)
84-
def init_hex_color_picker(visible, custom_color):
85-
if visible:
86-
if custom_color:
87-
return custom_color
88-
return dash.no_update
8998

9099

91100
@app.callback(

dash-fastapi-frontend/callbacks/monitor_c/cache_c/control_c.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
from server import app
44

55

6-
@app.callback(
6+
app.clientside_callback(
7+
'''
8+
(n_intervals, data) => {
9+
return [data, true];
10+
}
11+
''',
712
[Output('echarts-data-container', 'data'),
813
Output('init-echarts-interval', 'disabled')],
914
Input('init-echarts-interval', 'n_intervals'),
1015
State('init-echarts-data-container', 'data'),
1116
prevent_initial_call=True
1217
)
13-
def init_echarts(n_intervals, data):
14-
15-
return [data, True]
1618

1719

1820
app.clientside_callback(

dash-fastapi-frontend/callbacks/monitor_c/job_c/job_c.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,32 +108,42 @@ def get_job_table_data(search_click, refresh_click, pagination, operations, job_
108108
return [dash.no_update] * 5
109109

110110

111-
@app.callback(
111+
app.clientside_callback(
112+
'''
113+
(reset_click) => {
114+
if (reset_click) {
115+
return [null, null, null, {'type': 'reset'}]
116+
}
117+
return window.dash_clientside.no_update;
118+
}
119+
''',
112120
[Output('job-job_name-input', 'value'),
113121
Output('job-job_group-select', 'value'),
114122
Output('job-status-select', 'value'),
115123
Output('job-operations-store', 'data')],
116124
Input('job-reset', 'nClicks'),
117125
prevent_initial_call=True
118126
)
119-
def reset_job_query_params(reset_click):
120-
if reset_click:
121-
return [None, None, None, {'type': 'reset'}]
122-
123-
return [dash.no_update] * 4
124127

125128

126-
@app.callback(
129+
app.clientside_callback(
130+
'''
131+
(hidden_click, hidden_status) => {
132+
if (hidden_click) {
133+
return [
134+
!hidden_status,
135+
hidden_status ? '隐藏搜索' : '显示搜索'
136+
]
137+
}
138+
return window.dash_clientside.no_update;
139+
}
140+
''',
127141
[Output('job-search-form-container', 'hidden'),
128142
Output('job-hidden-tooltip', 'title')],
129143
Input('job-hidden', 'nClicks'),
130144
State('job-search-form-container', 'hidden'),
131145
prevent_initial_call=True
132146
)
133-
def hidden_job_search_form(hidden_click, hidden_status):
134-
if hidden_click:
135-
return [not hidden_status, '隐藏搜索' if hidden_status else '显示搜索']
136-
return [dash.no_update] * 2
137147

138148

139149
@app.callback(

0 commit comments

Comments
 (0)