Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
antonymilne committed Nov 17, 2023
1 parent 9eb03ee commit aa20379
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 16 deletions.
3 changes: 1 addition & 2 deletions vizro-core/src/vizro/models/_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Dashboard(VizroBaseModel):
theme: Literal["vizro_dark", "vizro_light"] = Field(
"vizro_dark", description="Layout theme to be applied across dashboard. Defaults to `vizro_dark`"
)
navigation: Optional[Navigation] = None
navigation: Navigation = Navigation()
title: Optional[str] = Field(None, description="Dashboard title to appear on every page on top left-side.")

@validator("pages", always=True)
Expand All @@ -51,7 +51,6 @@ def set_navigation_pages(cls, navigation, values):
if "pages" not in values:
return navigation

navigation = navigation or Navigation()
navigation.pages = navigation.pages or [page.id for page in values["pages"]]
return navigation

Expand Down
6 changes: 3 additions & 3 deletions vizro-core/src/vizro/models/_navigation/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Navigation(VizroBaseModel):
Defaults to `None`.
"""

pages: NavPagesType = [] # AM: yes but NavPagesType: note breaking change, maybe put whole type ht in
nav_selector: Optional[NavSelectorType] = None
pages: NavPagesType = []
nav_selector: NavSelectorType = Accordion()

# validators
_validate_pages = validator("pages", allow_reuse=True)(_validate_pages)
Expand All @@ -32,7 +32,7 @@ class Navigation(VizroBaseModel):
def pre_build(self):
# Since models instantiated in pre_build do not themselves have pre_build called on them, we call it manually
# here. Note that not all nav_selectors have pre_build (Accordion does not).
self.nav_selector = self.nav_selector or Accordion()
# self.nav_selector = self.nav_selector or Accordion()
self.nav_selector.pages = self.nav_selector.pages or self.pages
if hasattr(self.nav_selector, "pre_build"):
self.nav_selector.pre_build()
Expand Down
2 changes: 1 addition & 1 deletion vizro-core/src/vizro/models/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ class OptionsDictType(TypedDict):
[`Button`][vizro.models.Button], [`Card`][vizro.models.Card], [`Table`][vizro.models.Table] or
[`Graph`][vizro.models.Graph]."""

# Types used for pages values in the Navigation model.
NavPagesType = Annotated[
Union[List[str], Dict[str, List[str]]],
Field(
Expand All @@ -349,3 +348,4 @@ class OptionsDictType(TypedDict):
]
"""Discriminated union. Permissible value types for selector attribute:
[`Accordion`][vizro.models.Accordion], [`NavBar`][vizro.models.NavBar]."""
# AM: docstrings
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,14 @@
import vizro.models as vm


# AM: move to dashboard tests
@pytest.mark.parametrize("navigation", [None, vm.Navigation()])
def test_navigation_default(page1, page2, navigation):
# Navigation is optional inside Dashboard and navigation.pages will always be auto-populated if not provided
dashboard = vm.Dashboard(pages=[page1, page2], navigation=navigation)
assert hasattr(dashboard.navigation, "id")
assert dashboard.navigation.pages == ["Page 1", "Page 2"]


@pytest.mark.usefixtures("vizro_app", "prebuilt_dashboard")
class TestNavigationInstantiation:
def test_navigation_mandatory_only(self):
navigation = vm.Navigation()

assert hasattr(navigation, "id")
assert navigation.pages == []
assert navigation.nav_selector is None
assert isinstance(navigation.nav_selector, vm.Accordion)

def test_navigation_mandatory_and_optional(self):
accordion = vm.Accordion()
Expand Down
8 changes: 8 additions & 0 deletions vizro-core/tests/unit/vizro/models/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ def test_create_dashboard_mandatory_and_optional(self, page1, page2):
assert isinstance(dashboard.navigation, vm.Navigation)
assert dashboard.navigation.pages == ["Page 1", "Page 2"]

def test_navigation_pages_automatically_populated(self, page1, page2):
dashboard = vm.Dashboard(pages=[page1, page2])
assert dashboard.navigation.pages == ["Page 1", "Page 2"]

def test_navigation_with_pages(page1, page2):
dashboard = vm.Dashboard(pages=[page1, page2], navigation=vm.Navigation(pages=["Page 1"]))
assert dashboard.navigation.pages == ["Page 1"]

def test_mandatory_pages_missing(self):
with pytest.raises(ValidationError, match="field required"):
vm.Dashboard()
Expand Down

0 comments on commit aa20379

Please sign in to comment.