|
| 1 | +import json |
| 2 | + |
| 3 | +import dash_html_components as html |
| 4 | +import dash_core_components as dcc |
| 5 | +from dash import Dash |
| 6 | +from dash.dependencies import Output, Input |
| 7 | + |
| 8 | + |
| 9 | +def test_rdrh001_request_hooks(dash_duo): |
| 10 | + app = Dash(__name__) |
| 11 | + |
| 12 | + app.index_string = """<!DOCTYPE html> |
| 13 | + <html> |
| 14 | + <head> |
| 15 | + {%metas%} |
| 16 | + <title>{%title%}</title> |
| 17 | + {%favicon%} |
| 18 | + {%css%} |
| 19 | + </head> |
| 20 | + <body> |
| 21 | + <div>Testing custom DashRenderer</div> |
| 22 | + {%app_entry%} |
| 23 | + <footer> |
| 24 | + {%config%} |
| 25 | + {%scripts%} |
| 26 | + <script id="_dash-renderer" type"application/json"> |
| 27 | + const renderer = new DashRenderer({ |
| 28 | + request_pre: (payload) => { |
| 29 | + var output = document.getElementById('output-pre') |
| 30 | + var outputPayload = document.getElementById('output-pre-payload') |
| 31 | + if(output) { |
| 32 | + output.innerHTML = 'request_pre changed this text!'; |
| 33 | + } |
| 34 | + if(outputPayload) { |
| 35 | + outputPayload.innerHTML = JSON.stringify(payload); |
| 36 | + } |
| 37 | + }, |
| 38 | + request_post: (payload, response) => { |
| 39 | + var output = document.getElementById('output-post') |
| 40 | + var outputPayload = document.getElementById('output-post-payload') |
| 41 | + var outputResponse = document.getElementById('output-post-response') |
| 42 | + if(output) { |
| 43 | + output.innerHTML = 'request_post changed this text!'; |
| 44 | + } |
| 45 | + if(outputPayload) { |
| 46 | + outputPayload.innerHTML = JSON.stringify(payload); |
| 47 | + } |
| 48 | + if(outputResponse) { |
| 49 | + outputResponse.innerHTML = JSON.stringify(response); |
| 50 | + } |
| 51 | + } |
| 52 | + }) |
| 53 | + </script> |
| 54 | + </footer> |
| 55 | + <div>With request hooks</div> |
| 56 | + </body> |
| 57 | + </html>""" |
| 58 | + |
| 59 | + app.layout = html.Div( |
| 60 | + [ |
| 61 | + dcc.Input(id="input", value="initial value"), |
| 62 | + html.Div( |
| 63 | + html.Div( |
| 64 | + [ |
| 65 | + html.Div(id="output-1"), |
| 66 | + html.Div(id="output-pre"), |
| 67 | + html.Div(id="output-pre-payload"), |
| 68 | + html.Div(id="output-post"), |
| 69 | + html.Div(id="output-post-payload"), |
| 70 | + html.Div(id="output-post-response"), |
| 71 | + ] |
| 72 | + ) |
| 73 | + ), |
| 74 | + ] |
| 75 | + ) |
| 76 | + |
| 77 | + @app.callback(Output("output-1", "children"), [Input("input", "value")]) |
| 78 | + def update_output(value): |
| 79 | + return value |
| 80 | + |
| 81 | + dash_duo.start_server(app) |
| 82 | + |
| 83 | + _in = dash_duo.find_element("#input") |
| 84 | + dash_duo.clear_input(_in) |
| 85 | + |
| 86 | + _in.send_keys("fire request hooks") |
| 87 | + |
| 88 | + dash_duo.wait_for_text_to_equal("#output-1", "fire request hooks") |
| 89 | + dash_duo.wait_for_text_to_equal("#output-pre", "request_pre changed this text!") |
| 90 | + dash_duo.wait_for_text_to_equal("#output-post", "request_post changed this text!") |
| 91 | + |
| 92 | + assert json.loads(dash_duo.find_element("#output-pre-payload").text) == { |
| 93 | + "output": "output-1.children", |
| 94 | + "outputs": {"id": "output-1", "property": "children"}, |
| 95 | + "changedPropIds": ["input.value"], |
| 96 | + "inputs": [{"id": "input", "property": "value", "value": "fire request hooks"}], |
| 97 | + } |
| 98 | + |
| 99 | + assert json.loads(dash_duo.find_element("#output-post-payload").text) == { |
| 100 | + "output": "output-1.children", |
| 101 | + "outputs": {"id": "output-1", "property": "children"}, |
| 102 | + "changedPropIds": ["input.value"], |
| 103 | + "inputs": [{"id": "input", "property": "value", "value": "fire request hooks"}], |
| 104 | + } |
| 105 | + |
| 106 | + assert json.loads(dash_duo.find_element("#output-post-response").text) == { |
| 107 | + "output-1": {"children": "fire request hooks"} |
| 108 | + } |
| 109 | + |
| 110 | + dash_duo.percy_snapshot(name="request-hooks render") |
| 111 | + |
| 112 | + |
| 113 | +def test_rdrh002_with_custom_renderer_interpolated(dash_duo): |
| 114 | + |
| 115 | + renderer = """ |
| 116 | + <script id="_dash-renderer" type="application/javascript"> |
| 117 | + console.log('firing up a custom renderer!') |
| 118 | + const renderer = new DashRenderer({ |
| 119 | + request_pre: () => { |
| 120 | + var output = document.getElementById('output-pre') |
| 121 | + if(output) { |
| 122 | + output.innerHTML = 'request_pre was here!'; |
| 123 | + } |
| 124 | + }, |
| 125 | + request_post: () => { |
| 126 | + var output = document.getElementById('output-post') |
| 127 | + if(output) { |
| 128 | + output.innerHTML = 'request_post!!!'; |
| 129 | + } |
| 130 | + } |
| 131 | + }) |
| 132 | + </script> |
| 133 | + """ |
| 134 | + |
| 135 | + class CustomDash(Dash): |
| 136 | + def interpolate_index(self, **kwargs): |
| 137 | + return """<!DOCTYPE html> |
| 138 | + <html> |
| 139 | + <head> |
| 140 | + <title>My App</title> |
| 141 | + </head> |
| 142 | + <body> |
| 143 | +
|
| 144 | + <div id="custom-header">My custom header</div> |
| 145 | + {app_entry} |
| 146 | + {config} |
| 147 | + {scripts} |
| 148 | + {renderer} |
| 149 | + <div id="custom-footer">My custom footer</div> |
| 150 | + </body> |
| 151 | + </html>""".format( |
| 152 | + app_entry=kwargs["app_entry"], |
| 153 | + config=kwargs["config"], |
| 154 | + scripts=kwargs["scripts"], |
| 155 | + renderer=renderer, |
| 156 | + ) |
| 157 | + |
| 158 | + app = CustomDash() |
| 159 | + |
| 160 | + app.layout = html.Div( |
| 161 | + [ |
| 162 | + dcc.Input(id="input", value="initial value"), |
| 163 | + html.Div( |
| 164 | + html.Div( |
| 165 | + [ |
| 166 | + html.Div(id="output-1"), |
| 167 | + html.Div(id="output-pre"), |
| 168 | + html.Div(id="output-post"), |
| 169 | + ] |
| 170 | + ) |
| 171 | + ), |
| 172 | + ] |
| 173 | + ) |
| 174 | + |
| 175 | + @app.callback(Output("output-1", "children"), [Input("input", "value")]) |
| 176 | + def update_output(value): |
| 177 | + return value |
| 178 | + |
| 179 | + dash_duo.start_server(app) |
| 180 | + |
| 181 | + input1 = dash_duo.find_element("#input") |
| 182 | + dash_duo.clear_input(input1) |
| 183 | + |
| 184 | + input1.send_keys("fire request hooks") |
| 185 | + |
| 186 | + dash_duo.wait_for_text_to_equal("#output-1", "fire request hooks") |
| 187 | + assert dash_duo.find_element("#output-pre").text == "request_pre was here!" |
| 188 | + assert dash_duo.find_element("#output-post").text == "request_post!!!" |
| 189 | + |
| 190 | + dash_duo.percy_snapshot(name="request-hooks interpolated") |
0 commit comments