Skip to content

Commit

Permalink
Refactor helper function for hideable parent container and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
huong-li-nguyen committed Dec 18, 2023
1 parent 3c789e7 commit 88d29ab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
16 changes: 8 additions & 8 deletions vizro-core/src/vizro/models/_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging
from functools import partial
from typing import TYPE_CHECKING, List, Literal
from typing import TYPE_CHECKING, List, Literal, Optional

import dash
import dash_bootstrap_components as dbc
Expand Down Expand Up @@ -31,12 +31,12 @@
# TODO: Use IDs instead of className and remove className


def _get_parent_container(children: List[html.Div], parent_id: str):
def _get_hideable_parent_div(children: List[html.Div], parent_id: Optional[str] = None):
"""Hides the parent container if all the children containers are either hidden or None."""
return (
html.Div(children=children, id=parent_id)
if all(getattr(div, "hidden", False) or None for div in children)
else html.Div(id=parent_id, hidden=True)
html.Div(id=parent_id, hidden=True)
if all(div is None or getattr(div, "hidden", False) for div in children)
else html.Div(children=children, id=parent_id)
)


Expand Down Expand Up @@ -147,13 +147,13 @@ def _arrange_page_divs(self, page_divs: html.Div):
left_header_divs = [page_divs["dashboard-title"]]
left_sidebar_divs = [page_divs["nav_bar_outer"]]
left_main_divs = [
_get_parent_container(children=left_header_divs, parent_id="left-header"),
_get_hideable_parent_div(children=left_header_divs, parent_id="left-header"),
page_divs["nav_panel_outer"],
page_divs["control_panel_outer"],
]

left_sidebar = _get_parent_container(children=left_sidebar_divs, parent_id="left-sidebar")
left_main = _get_parent_container(children=left_main_divs, parent_id="left-main")
left_sidebar = _get_hideable_parent_div(children=left_sidebar_divs, parent_id="left-sidebar")
left_main = _get_hideable_parent_div(children=left_main_divs, parent_id="left-main")
left_side = html.Div(children=[left_sidebar, left_main], className="left_side", id="left_side_outer")

right_header = html.Div(
Expand Down
24 changes: 24 additions & 0 deletions vizro-core/tests/unit/vizro/models/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import vizro
import vizro.models as vm
from vizro.actions._action_loop._action_loop import ActionLoop
from vizro.models._dashboard import _get_hideable_parent_div


@pytest.fixture()
Expand Down Expand Up @@ -173,3 +174,26 @@ def test_dashboard_build(self, dashboard_container, prebuilt_two_page_dashboard)
result = json.loads(json.dumps(prebuilt_two_page_dashboard.build(), cls=plotly.utils.PlotlyJSONEncoder))
expected = json.loads(json.dumps(dashboard_container, cls=plotly.utils.PlotlyJSONEncoder))
assert result == expected


@pytest.mark.parametrize(
"children",
[
[html.Div(hidden=False)],
[html.Div(id="children-id")],
[html.Div(hidden=True), None, html.Div(hidden=False)],
[html.Div(hidden=True), None, html.Div(id="children-id")],
],
)
def test_get_hideable_parent_div_visible(children):
parent = _get_hideable_parent_div(children=children, parent_id="parent-id")
assert parent.id == "parent-id"
assert parent.children == children
assert getattr(parent, "hidden", False) is False


@pytest.mark.parametrize("children", [[html.Div(hidden=True)], [None], [html.Div(hidden=True), None]])
def test_get_hideable_parent_div_hidden(children):
parent = _get_hideable_parent_div(children=children, parent_id="parent-id")
assert parent.id == "parent-id"
assert getattr(parent, "hidden", False) is True

0 comments on commit 88d29ab

Please sign in to comment.