diff --git a/src/ssb_dash_framework/setup/main_layout.py b/src/ssb_dash_framework/setup/main_layout.py index cfebf8a6..b9342b19 100644 --- a/src/ssb_dash_framework/setup/main_layout.py +++ b/src/ssb_dash_framework/setup/main_layout.py @@ -3,6 +3,7 @@ import dash_bootstrap_components as dbc from dash import html +from dash import dcc from ..utils.alert_handler import AlertHandler from ..utils.functions import sidebar_button @@ -12,6 +13,17 @@ logger = logging.getLogger(__name__) +STANDARD_LAYOUT = None + + +def get_standard_layout(): + if globals()["STANDARD_LAYOUT"]: + return globals()["STANDARD_LAYOUT"] + else: + raise ValueError( + "'STANDARD_LAYOUT' not defined, should exist after running 'main_layout()'" + ) + def main_layout( window_list: list[WindowModule], @@ -74,8 +86,43 @@ def main_layout( (tab if isinstance(tab, dbc.Tab) else dbc.Tab(tab.layout(), label=tab.label)) for tab in tab_list ] + + globals()["STANDARD_LAYOUT"] = [ + html.Div( + id="main-layout-sidebar", + className="main-layout-sidebar bg-secondary", + children=window_modules_list, + ), + html.Div( + children=[ + html.Div( + id="main-layout-offcanvas", + children=[ + dbc.Offcanvas( + html.Div( + children=variable_selector.layout(), + ), + id="variable-selector-offcanvas", + className="main-layout-offcanvas-variable-selector", + title="Variabler", + is_open=False, + placement="end", + backdrop=False, + ), + ], + ), + html.Div( + id="main-layout-tab-div", + className="main-layout-tab-container", + children=dbc.Tabs(selected_tab_list), + ), + ], + ), + ] + layout = dbc.Container( [ + dcc.Location(id="url", refresh=False), html.Div( id="notifications-container", className="main-layout-notifications-container", @@ -87,42 +134,13 @@ def main_layout( html.Div( id="main-layout", className="main-layout", - children=[ - html.Div( - id="main-layout-sidebar", - className="main-layout-sidebar bg-secondary", - children=window_modules_list, - ), - html.Div( - children=[ - html.Div( - id="main-layout-offcanvas", - children=[ - dbc.Offcanvas( - html.Div( - children=variable_selector.layout(), - ), - id="variable-selector-offcanvas", - className="main-layout-offcanvas-variable-selector", - title="Variabler", - is_open=False, - placement="end", - backdrop=False, - ), - ], - ), - html.Div( - id="main-layout-tab-div", - className="main-layout-tab-container", - children=dbc.Tabs(selected_tab_list), - ), - ], - ), - ], + children=globals()["STANDARD_LAYOUT"], ), ], fluid=True, className="dbc dbc-ag-grid", ) logger.debug("Generated layout.") + from .urls import add_urls + add_urls() return layout diff --git a/src/ssb_dash_framework/setup/urls.py b/src/ssb_dash_framework/setup/urls.py new file mode 100644 index 00000000..9b29093b --- /dev/null +++ b/src/ssb_dash_framework/setup/urls.py @@ -0,0 +1,27 @@ +import logging +import re + +from dash import callback +from dash import Output +from dash import Input +from dash.exceptions import PreventUpdate + +from .main_layout import get_standard_layout + +logger = logging.getLogger(__name__) + + +def add_urls(): + logger.debug("Adding urls.") + @callback( + Output("main-layout", "children"), + Input("url", "pathname") + ) + def routing(path): + logger.info(f"Routing input: {path}") + if bool(re.fullmatch(r'/proxy/\d{4}/', path)): + logger.debug("Returning standard layout.") + return get_standard_layout() + else: + logger.debug("Preventing update.") + raise PreventUpdate \ No newline at end of file