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