|
1 | 1 | import pytest
|
2 | 2 | import dash
|
3 |
| -from dash import Dash, dcc, html |
| 3 | +from dash import Dash, Input, State, dcc, html |
| 4 | +from dash.dash import _ID_LOCATION |
4 | 5 | from dash.exceptions import NoLayoutException
|
5 | 6 |
|
6 | 7 |
|
@@ -179,3 +180,65 @@ def test_pala004_no_layout_exception(clear_pages_state):
|
179 | 180 | Dash(__name__, use_pages=True, pages_folder="pages_error")
|
180 | 181 |
|
181 | 182 | assert error_msg in err.value.args[0]
|
| 183 | + |
| 184 | + |
| 185 | +def get_routing_inputs_app(): |
| 186 | + app = Dash( |
| 187 | + __name__, |
| 188 | + use_pages=True, |
| 189 | + routing_callback_inputs={ |
| 190 | + "hash": State(_ID_LOCATION, "hash"), |
| 191 | + "language": Input("language", "value"), |
| 192 | + }, |
| 193 | + ) |
| 194 | + # Page with layout from a variable: should render and not be impacted |
| 195 | + # by routing callback inputs |
| 196 | + dash.register_page( |
| 197 | + "home", |
| 198 | + layout=html.Div("Home", id="contents"), |
| 199 | + path="/", |
| 200 | + ) |
| 201 | + |
| 202 | + # Page with a layout function, should see the routing callback inputs |
| 203 | + # as keyword arguments |
| 204 | + def layout1(hash: str = None, language: str = "en", **kwargs): |
| 205 | + if language == "fr": |
| 206 | + return html.Div(f"Le hash dit: {hash}", id="contents") |
| 207 | + return html.Div(f"Hash says: {hash}", id="contents") |
| 208 | + |
| 209 | + dash.register_page( |
| 210 | + "function_layout", |
| 211 | + path="/function-layout", |
| 212 | + layout=layout1, |
| 213 | + ) |
| 214 | + app.layout = html.Div( |
| 215 | + [ |
| 216 | + dcc.Dropdown(id="language", options=["en", "fr"], value="en"), |
| 217 | + dash.page_container, |
| 218 | + ] |
| 219 | + ) |
| 220 | + return app |
| 221 | + |
| 222 | + |
| 223 | +def test_pala005_routing_inputs(dash_duo, clear_pages_state): |
| 224 | + dash_duo.start_server(get_routing_inputs_app()) |
| 225 | + dash_duo.wait_for_page(url=f"http://localhost:{dash_duo.server.port}#123") |
| 226 | + dash_duo.wait_for_text_to_equal("#contents", "Home") |
| 227 | + dash_duo.wait_for_page(url=f"http://localhost:{dash_duo.server.port}/") |
| 228 | + dash_duo.wait_for_text_to_equal("#contents", "Home") |
| 229 | + dash_duo.wait_for_page( |
| 230 | + url=f"http://localhost:{dash_duo.server.port}/function-layout" |
| 231 | + ) |
| 232 | + dash_duo.wait_for_text_to_equal("#contents", "Hash says:") |
| 233 | + # hash is a State therefore navigating to the same page with hash will not |
| 234 | + # re-render the layout function |
| 235 | + dash_duo.wait_for_page( |
| 236 | + url=f"http://localhost:{dash_duo.server.port}/function-layout#123" |
| 237 | + ) |
| 238 | + dash_duo.wait_for_text_to_equal("#contents", "Hash says:") |
| 239 | + # Refreshing the page re-runs the layout function |
| 240 | + dash_duo.driver.refresh() |
| 241 | + dash_duo.wait_for_text_to_equal("#contents", "Hash says: #123") |
| 242 | + # Changing the language Input re-runs the layout function |
| 243 | + dash_duo.select_dcc_dropdown("#language", "fr") |
| 244 | + dash_duo.wait_for_text_to_equal("#contents", "Le hash dit: #123") |
0 commit comments