-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathtest_arbitrary_callbacks.py
More file actions
275 lines (220 loc) · 7.13 KB
/
test_arbitrary_callbacks.py
File metadata and controls
275 lines (220 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import time
from multiprocessing import Value
from dash import (
Dash,
Input,
Output,
html,
set_props,
register_page,
clientside_callback,
)
def test_arb001_global_set_props(dash_duo):
app = Dash()
app.layout = html.Div(
[
html.Div(id="output"),
html.Div(id="secondary-output"),
html.Button("click", id="clicker"),
]
)
@app.callback(
Output("output", "children"),
Input("clicker", "n_clicks"),
prevent_initial_call=True,
)
def on_click(n_clicks):
set_props("secondary-output", {"children": "secondary"})
return f"Clicked {n_clicks} times"
dash_duo.start_server(app)
dash_duo.wait_for_element("#clicker").click()
dash_duo.wait_for_text_to_equal("#output", "Clicked 1 times")
dash_duo.wait_for_text_to_equal("#secondary-output", "secondary")
def test_arb002_no_output_callbacks(dash_duo):
app = Dash()
counter = Value("i", 0)
app.layout = html.Div(
[
html.Div(id="secondary-output"),
html.Button("no-output", id="no-output"),
html.Button("no-output2", id="no-output2"),
html.Button("no-output3", id="no-output3"),
]
)
@app.callback(
Input("no-output", "n_clicks"),
prevent_initial_call=True,
)
def no_output1(_):
set_props("secondary-output", {"children": "no-output"})
@app.callback(
Input("no-output2", "n_clicks"),
prevent_initial_call=True,
)
def no_output2(_):
set_props("secondary-output", {"children": "no-output2"})
@app.callback(
Input("no-output3", "n_clicks"),
prevent_initial_call=True,
)
def no_output3(_):
with counter.get_lock():
counter.value += 1
dash_duo.start_server(app)
dash_duo.wait_for_element("#no-output").click()
dash_duo.wait_for_text_to_equal("#secondary-output", "no-output")
dash_duo.wait_for_element("#no-output2").click()
dash_duo.wait_for_text_to_equal("#secondary-output", "no-output2")
dash_duo.wait_for_element("#no-output3").click()
time.sleep(1)
with counter.get_lock():
assert counter.value == 1
def test_arb003_arbitrary_pages(dash_duo, clear_pages_state):
app = Dash(use_pages=True, pages_folder="")
register_page(
"page",
"/",
layout=html.Div(
[
html.Div(id="secondary-output"),
html.Button("no-output", id="no-output"),
html.Button("no-output2", id="no-output2"),
]
),
)
@app.callback(
Input("no-output", "n_clicks"),
prevent_initial_call=True,
)
def no_output(_):
set_props("secondary-output", {"children": "no-output"})
@app.callback(
Input("no-output2", "n_clicks"),
prevent_initial_call=True,
)
def no_output(_):
set_props("secondary-output", {"children": "no-output2"})
dash_duo.start_server(app)
dash_duo.wait_for_element("#no-output").click()
dash_duo.wait_for_text_to_equal("#secondary-output", "no-output")
dash_duo.wait_for_element("#no-output2").click()
dash_duo.wait_for_text_to_equal("#secondary-output", "no-output2")
def test_arb004_wildcard_set_props(dash_duo):
app = Dash()
app.layout = html.Div(
[
html.Button("click", id="click"),
html.Div(html.Div(id={"id": "output", "index": 0}), id="output"),
]
)
@app.callback(
Input("click", "n_clicks"),
prevent_initial_call=True,
)
def on_click(n_clicks):
set_props(
{"id": "output", "index": 0}, {"children": f"Clicked {n_clicks} times"}
)
dash_duo.start_server(app)
dash_duo.wait_for_element("#click").click()
dash_duo.wait_for_text_to_equal("#output", "Clicked 1 times")
def test_arb005_no_output_error(dash_duo):
app = Dash()
app.layout = html.Div([html.Button("start", id="start")])
@app.callback(Input("start", "n_clicks"), prevent_initial_call=True)
def on_click(clicked):
return f"clicked {clicked}"
dash_duo.start_server(
app,
debug=True,
use_reloader=False,
use_debugger=True,
dev_tools_hot_reload=False,
)
dash_duo.wait_for_element("#start").click()
dash_duo.wait_for_text_to_equal(
".dash-fe-error__title",
"Callback error with no output from input start.n_clicks",
)
def test_arb006_multi_set_props(dash_duo):
app = Dash()
app.layout = [
html.Button("start", id="start"),
html.Div("initial", id="output"),
]
@app.callback(
Input("start", "n_clicks"),
)
def on_click(_):
set_props("output", {"children": "changed"})
set_props("output", {"style": {"background": "rgb(255,0,0)"}})
dash_duo.start_server(app)
dash_duo.wait_for_element("#start").click()
dash_duo.wait_for_text_to_equal("#output", "changed")
dash_duo.wait_for_style_to_equal(
"#output", "background-color", "rgba(255, 0, 0, 1)"
)
def test_arb007_clientside_no_output(dash_duo):
app = Dash()
app.layout = [
html.Button("start", id="start1"),
html.Button("start2", id="start2"),
html.Div(id="output"),
]
clientside_callback(
"""
function(_) {
dash_clientside.set_props('output', {children: 'start1'})
}
""",
Input("start1", "n_clicks"),
prevent_initial_call=True,
)
clientside_callback(
"""
function(_) {
dash_clientside.set_props('output', {children: 'start2'})
}
""",
Input("start2", "n_clicks"),
prevent_initial_call=True,
)
dash_duo.start_server(app)
dash_duo.find_element("#start1").click()
dash_duo.wait_for_text_to_equal("#output", "start1")
dash_duo.find_element("#start2").click()
dash_duo.wait_for_text_to_equal("#output", "start2")
def test_arb008_set_props_chain_cb(dash_duo):
app = Dash(suppress_callback_exceptions=True)
app.layout = html.Div(
[
html.Button("origin button", id="origin-button"),
html.Div(id="generated-button-container"),
html.Div("initial text", id="generated-button-output"),
],
style={"padding": 50},
)
@app.callback(
Input("origin-button", "n_clicks"),
)
def generate_button(n_clicks):
set_props(
"generated-button-container",
{
"children": html.Button(
"generated button", id="generated-button", n_clicks=0
)
},
)
@app.callback(
Output("generated-button-output", "children"),
Input("generated-button", "n_clicks", allow_optional=True),
prevent_initial_call=True,
)
def update_output(n_clicks):
return f"n_clicks: {n_clicks}"
dash_duo.start_server(app)
dash_duo.wait_for_element("#origin-button").click()
for i in range(1, 5):
dash_duo.wait_for_element("#generated-button").click()
dash_duo.wait_for_text_to_equal("#generated-button-output", f"n_clicks: {i}")