|
1 | 1 | from dash import *
|
2 | 2 |
|
| 3 | + |
3 | 4 | def test_cbop001_optional_input(dash_duo):
|
4 | 5 | app = Dash(suppress_callback_exceptions=True)
|
5 | 6 |
|
6 | 7 | app.layout = html.Div(
|
7 | 8 | [
|
8 |
| - html.Button(id='button1', children='Button 1'), |
9 |
| - html.Div(id='button-container'), |
10 |
| - html.Div(id='test-out'), |
11 |
| - html.Div(id='test-out2') |
| 9 | + html.Button(id="button1", children="Button 1"), |
| 10 | + html.Div(id="button-container"), |
| 11 | + html.Div(id="test-out"), |
| 12 | + html.Div(id="test-out2"), |
12 | 13 | ]
|
13 | 14 | )
|
14 | 15 |
|
15 | 16 | @app.callback(
|
16 |
| - Output('button-container', 'children'), |
17 |
| - Input('button1', 'n_clicks'), |
18 |
| - State('button-container', 'children'), |
19 |
| - prevent_initial_call=True |
| 17 | + Output("button-container", "children"), |
| 18 | + Input("button1", "n_clicks"), |
| 19 | + State("button-container", "children"), |
| 20 | + prevent_initial_call=True, |
20 | 21 | )
|
21 | 22 | def _(_, c):
|
22 | 23 | if not c:
|
23 |
| - return html.Button(id='button2', children='Button 2') |
| 24 | + return html.Button(id="button2", children="Button 2") |
24 | 25 | return no_update
|
25 | 26 |
|
26 | 27 | @app.callback(
|
27 |
| - Output('test-out', 'children'), |
28 |
| - Input('button1', 'n_clicks'), |
29 |
| - Input('button2', 'n_clicks', allow_optional=True), |
30 |
| - prevent_inital_call=True |
| 28 | + Output("test-out", "children"), |
| 29 | + Input("button1", "n_clicks"), |
| 30 | + Input("button2", "n_clicks", allow_optional=True), |
| 31 | + prevent_inital_call=True, |
31 | 32 | )
|
32 | 33 | def display(n, n2):
|
33 | 34 | return f"{n} - {n2}"
|
34 | 35 |
|
35 | 36 | dash_duo.start_server(app)
|
36 |
| - dash_duo.wait_for_text_to_equal('#button1', 'Button 1') |
| 37 | + dash_duo.wait_for_text_to_equal("#button1", "Button 1") |
37 | 38 | assert dash_duo.get_logs() == []
|
38 |
| - dash_duo.wait_for_text_to_equal('#test-out', 'None - None') |
| 39 | + dash_duo.wait_for_text_to_equal("#test-out", "None - None") |
39 | 40 | dash_duo.find_element("#button1").click()
|
40 |
| - dash_duo.wait_for_text_to_equal('#test-out', '1 - None') |
| 41 | + dash_duo.wait_for_text_to_equal("#test-out", "1 - None") |
41 | 42 |
|
42 | 43 | dash_duo.find_element("#button2").click()
|
43 |
| - dash_duo.wait_for_text_to_equal('#test-out', '1 - 1') |
| 44 | + dash_duo.wait_for_text_to_equal("#test-out", "1 - 1") |
44 | 45 | assert dash_duo.get_logs() == []
|
45 | 46 |
|
46 | 47 |
|
47 |
| - |
48 | 48 | def test_cbop002_optional_state(dash_duo):
|
49 | 49 | app = Dash(suppress_callback_exceptions=True)
|
50 | 50 |
|
51 | 51 | app.layout = html.Div(
|
52 | 52 | [
|
53 |
| - html.Button(id='button1', children='Button 1'), |
54 |
| - html.Div(id='button-container'), |
55 |
| - html.Div(id='test-out'), |
56 |
| - html.Div(id='test-out2') |
| 53 | + html.Button(id="button1", children="Button 1"), |
| 54 | + html.Div(id="button-container"), |
| 55 | + html.Div(id="test-out"), |
| 56 | + html.Div(id="test-out2"), |
57 | 57 | ]
|
58 | 58 | )
|
59 | 59 |
|
60 | 60 | @app.callback(
|
61 |
| - Output('button-container', 'children'), |
62 |
| - Input('button1', 'n_clicks'), |
63 |
| - State('button-container', 'children'), |
64 |
| - prevent_initial_call=True |
| 61 | + Output("button-container", "children"), |
| 62 | + Input("button1", "n_clicks"), |
| 63 | + State("button-container", "children"), |
| 64 | + prevent_initial_call=True, |
65 | 65 | )
|
66 | 66 | def _(_, c):
|
67 | 67 | if not c:
|
68 |
| - return html.Button(id='button2', children='Button 2') |
| 68 | + return html.Button(id="button2", children="Button 2") |
69 | 69 | return no_update
|
70 | 70 |
|
71 | 71 | @app.callback(
|
72 |
| - Output('test-out', 'children'), |
73 |
| - Input('button1', 'n_clicks'), |
74 |
| - State('button2', 'n_clicks', allow_optional=True), |
75 |
| - prevent_inital_call=True |
| 72 | + Output("test-out", "children"), |
| 73 | + Input("button1", "n_clicks"), |
| 74 | + State("button2", "n_clicks", allow_optional=True), |
| 75 | + prevent_inital_call=True, |
76 | 76 | )
|
77 | 77 | def display(n, n2):
|
78 | 78 | return f"{n} - {n2}"
|
79 | 79 |
|
80 | 80 | @app.callback(
|
81 |
| - Output('test-out2', 'children'), |
82 |
| - Input('button2', 'n_clicks', allow_optional=True) |
| 81 | + Output("test-out2", "children"), |
| 82 | + Input("button2", "n_clicks", allow_optional=True), |
83 | 83 | )
|
84 | 84 | def test(n):
|
85 | 85 | if n:
|
86 | 86 | return n
|
87 | 87 | return no_update
|
88 | 88 |
|
89 | 89 | dash_duo.start_server(app)
|
90 |
| - dash_duo.wait_for_text_to_equal('#button1', 'Button 1') |
| 90 | + dash_duo.wait_for_text_to_equal("#button1", "Button 1") |
91 | 91 | assert dash_duo.get_logs() == []
|
92 |
| - dash_duo.wait_for_text_to_equal('#test-out', 'None - None') |
| 92 | + dash_duo.wait_for_text_to_equal("#test-out", "None - None") |
93 | 93 | dash_duo.find_element("#button1").click()
|
94 |
| - dash_duo.wait_for_text_to_equal('#test-out', '1 - None') |
| 94 | + dash_duo.wait_for_text_to_equal("#test-out", "1 - None") |
95 | 95 |
|
96 | 96 | dash_duo.find_element("#button2").click()
|
97 |
| - dash_duo.wait_for_text_to_equal('#test-out2', '1') |
98 |
| - dash_duo.wait_for_text_to_equal('#test-out', '1 - None') |
| 97 | + dash_duo.wait_for_text_to_equal("#test-out2", "1") |
| 98 | + dash_duo.wait_for_text_to_equal("#test-out", "1 - None") |
99 | 99 | dash_duo.find_element("#button1").click()
|
100 |
| - dash_duo.wait_for_text_to_equal('#test-out', '2 - 1') |
| 100 | + dash_duo.wait_for_text_to_equal("#test-out", "2 - 1") |
101 | 101 | assert dash_duo.get_logs() == []
|
0 commit comments