|
| 1 | +from multiprocessing import Value |
| 2 | + |
| 3 | +from copy import copy |
| 4 | +from selenium.webdriver.common.keys import Keys |
| 5 | + |
| 6 | +import dash_core_components as dcc |
| 7 | +import dash_html_components as html |
| 8 | +from dash import Dash, no_update |
| 9 | +from dash.dependencies import Input, Output, State |
| 10 | +from dash.exceptions import PreventUpdate |
| 11 | + |
| 12 | +from dash.testing.wait import until |
| 13 | + |
| 14 | + |
| 15 | +def test_cbpu001_aborted_callback(dash_duo): |
| 16 | + """Raising PreventUpdate OR returning no_update prevents update and |
| 17 | + triggering dependencies.""" |
| 18 | + |
| 19 | + initial_input = "initial input" |
| 20 | + initial_output = "initial output" |
| 21 | + |
| 22 | + app = Dash(__name__) |
| 23 | + app.layout = html.Div( |
| 24 | + [ |
| 25 | + dcc.Input(id="input", value=initial_input), |
| 26 | + html.Div(initial_output, id="output1"), |
| 27 | + html.Div(initial_output, id="output2"), |
| 28 | + ] |
| 29 | + ) |
| 30 | + |
| 31 | + callback1_count = Value("i", 0) |
| 32 | + callback2_count = Value("i", 0) |
| 33 | + |
| 34 | + @app.callback(Output("output1", "children"), [Input("input", "value")]) |
| 35 | + def callback1(value): |
| 36 | + callback1_count.value += 1 |
| 37 | + if callback1_count.value > 2: |
| 38 | + return no_update |
| 39 | + raise PreventUpdate("testing callback does not update") |
| 40 | + return value |
| 41 | + |
| 42 | + @app.callback(Output("output2", "children"), [Input("output1", "children")]) |
| 43 | + def callback2(value): |
| 44 | + callback2_count.value += 1 |
| 45 | + return value |
| 46 | + |
| 47 | + dash_duo.start_server(app) |
| 48 | + |
| 49 | + input_ = dash_duo.find_element("#input") |
| 50 | + input_.send_keys("xyz") |
| 51 | + dash_duo.wait_for_text_to_equal("#input", "initial inputxyz") |
| 52 | + |
| 53 | + until( |
| 54 | + lambda: callback1_count.value == 4, |
| 55 | + timeout=3, |
| 56 | + msg="callback1 runs 4x (initial page load and 3x through send_keys)", |
| 57 | + ) |
| 58 | + |
| 59 | + assert ( |
| 60 | + callback2_count.value == 0 |
| 61 | + ), "callback2 is never triggered, even on initial load" |
| 62 | + |
| 63 | + # double check that output1 and output2 children were not updated |
| 64 | + assert dash_duo.find_element("#output1").text == initial_output |
| 65 | + assert dash_duo.find_element("#output2").text == initial_output |
| 66 | + |
| 67 | + assert not dash_duo.get_logs() |
| 68 | + |
| 69 | + dash_duo.percy_snapshot(name="aborted") |
| 70 | + |
| 71 | + |
| 72 | +def test_cbpu002_multi_output_no_update(dash_duo): |
| 73 | + app = Dash(__name__) |
| 74 | + |
| 75 | + app.layout = html.Div( |
| 76 | + [ |
| 77 | + html.Button("B", "btn"), |
| 78 | + html.P("initial1", "n1"), |
| 79 | + html.P("initial2", "n2"), |
| 80 | + html.P("initial3", "n3"), |
| 81 | + ] |
| 82 | + ) |
| 83 | + |
| 84 | + @app.callback( |
| 85 | + [Output("n1", "children"), Output("n2", "children"), Output("n3", "children")], |
| 86 | + [Input("btn", "n_clicks")], |
| 87 | + ) |
| 88 | + def show_clicks(n): |
| 89 | + # partial or complete cancelation of updates via no_update |
| 90 | + return [ |
| 91 | + no_update if n and n > 4 else n, |
| 92 | + no_update if n and n > 2 else n, |
| 93 | + # make a new instance, to mock up caching and restoring no_update |
| 94 | + copy(no_update), |
| 95 | + ] |
| 96 | + |
| 97 | + dash_duo.start_server(app) |
| 98 | + |
| 99 | + dash_duo.multiple_click("#btn", 10) |
| 100 | + |
| 101 | + dash_duo.wait_for_text_to_equal("#n1", "4") |
| 102 | + dash_duo.wait_for_text_to_equal("#n2", "2") |
| 103 | + dash_duo.wait_for_text_to_equal("#n3", "initial3") |
| 104 | + |
| 105 | + |
| 106 | +def test_cbpu003_no_update_chains(dash_duo): |
| 107 | + app = Dash(__name__) |
| 108 | + |
| 109 | + app.layout = html.Div( |
| 110 | + [ |
| 111 | + dcc.Input(id="a_in", value="a"), |
| 112 | + dcc.Input(id="b_in", value="b"), |
| 113 | + html.P("", id="a_out"), |
| 114 | + html.P("", id="a_out_short"), |
| 115 | + html.P("", id="b_out"), |
| 116 | + html.P("", id="ab_out"), |
| 117 | + ] |
| 118 | + ) |
| 119 | + |
| 120 | + @app.callback( |
| 121 | + [Output("a_out", "children"), Output("a_out_short", "children")], |
| 122 | + [Input("a_in", "value")], |
| 123 | + ) |
| 124 | + def a_out(a): |
| 125 | + return a, a if len(a) < 3 else no_update |
| 126 | + |
| 127 | + @app.callback(Output("b_out", "children"), [Input("b_in", "value")]) |
| 128 | + def b_out(b): |
| 129 | + return b |
| 130 | + |
| 131 | + @app.callback( |
| 132 | + Output("ab_out", "children"), |
| 133 | + [Input("a_out_short", "children")], |
| 134 | + [State("b_out", "children")], |
| 135 | + ) |
| 136 | + def ab_out(a, b): |
| 137 | + return a + " " + b |
| 138 | + |
| 139 | + dash_duo.start_server(app) |
| 140 | + |
| 141 | + a_in = dash_duo.find_element("#a_in") |
| 142 | + b_in = dash_duo.find_element("#b_in") |
| 143 | + |
| 144 | + b_in.send_keys("b") |
| 145 | + a_in.send_keys("a") |
| 146 | + dash_duo.wait_for_text_to_equal("#a_out", "aa") |
| 147 | + dash_duo.wait_for_text_to_equal("#b_out", "bb") |
| 148 | + dash_duo.wait_for_text_to_equal("#a_out_short", "aa") |
| 149 | + dash_duo.wait_for_text_to_equal("#ab_out", "aa bb") |
| 150 | + |
| 151 | + b_in.send_keys("b") |
| 152 | + a_in.send_keys("a") |
| 153 | + dash_duo.wait_for_text_to_equal("#a_out", "aaa") |
| 154 | + dash_duo.wait_for_text_to_equal("#b_out", "bbb") |
| 155 | + dash_duo.wait_for_text_to_equal("#a_out_short", "aa") |
| 156 | + # ab_out has not been triggered because a_out_short received no_update |
| 157 | + dash_duo.wait_for_text_to_equal("#ab_out", "aa bb") |
| 158 | + |
| 159 | + b_in.send_keys("b") |
| 160 | + a_in.send_keys(Keys.END) |
| 161 | + a_in.send_keys(Keys.BACKSPACE) |
| 162 | + dash_duo.wait_for_text_to_equal("#a_out", "aa") |
| 163 | + dash_duo.wait_for_text_to_equal("#b_out", "bbbb") |
| 164 | + dash_duo.wait_for_text_to_equal("#a_out_short", "aa") |
| 165 | + # now ab_out *is* triggered - a_out_short got a new value |
| 166 | + # even though that value is the same as the last value it got |
| 167 | + dash_duo.wait_for_text_to_equal("#ab_out", "aa bbbb") |
0 commit comments