Skip to content

Commit

Permalink
addressing pr comments part 3
Browse files Browse the repository at this point in the history
  • Loading branch information
nadijagraca committed Oct 27, 2023
1 parent d6eae78 commit 388a93b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 39 deletions.
5 changes: 0 additions & 5 deletions vizro-core/examples/default/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,6 @@ def create_home_page():
create_country_analysis(),
],
navigation=vm.Navigation(
pages={
"Analysis": ["Homepage", "Variable Analysis"],
"Summary": ["Relationship Analysis", "Continent Summary", "Country Analysis"],
},
selector=vm.NavBar(
items=[
vm.NavItem(
Expand All @@ -541,7 +537,6 @@ def create_home_page():
),
vm.NavItem(
pages=["Country Analysis"],
text="Country Analysis is the set text for this icon",
),
]
),
Expand Down
1 change: 0 additions & 1 deletion vizro-core/src/vizro/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@
FILTER_ACTION_PREFIX = "filter_action"
PARAMETER_ACTION_PREFIX = "parameter_action"
ACCORDION_DEFAULT_TITLE = "SELECT PAGE"
MIN_NO_OF_PAGES = 2
2 changes: 1 addition & 1 deletion vizro-core/src/vizro/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
"Dropdown",
"Filter",
"Graph",
"NavItem",
"Layout",
"NavBar",
"NavItem",
"Navigation",
"Page",
"Parameter",
Expand Down
2 changes: 1 addition & 1 deletion vizro-core/src/vizro/models/_navigation/accordion.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from dash import html
from pydantic import Field, validator

from vizro._constants import ACCORDION_DEFAULT_TITLE, MIN_NO_OF_PAGES
from vizro._constants import ACCORDION_DEFAULT_TITLE
from vizro.models import VizroBaseModel
from vizro.models._models_utils import _log_call
from vizro.models._navigation._navigation_utils import _validate_pages
Expand Down
25 changes: 10 additions & 15 deletions vizro-core/src/vizro/models/_navigation/nav_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from typing import List, Optional

from dash import html
from pydantic import validator

from vizro.managers import model_manager
from vizro.models import VizroBaseModel
from vizro.models._models_utils import _log_call
from vizro.models._navigation.nav_item import NavItem
Expand All @@ -18,26 +18,21 @@ class NavBar(VizroBaseModel):
Args:
pages (Optional[NavPagesType]): See [`NavPagesType`][vizro.models.types.NavPagesType].
Defaults to `None`.
items (List[NavItem]): List of NavItem models. Defaults to `[]`.
items (List[NavItem]): See [`NavItem`][vizro.models.NavItem]. Defaults to `[]`.
"""

pages: Optional[NavPagesType] = None
items: List[NavItem] = []

@_log_call
def pre_build(self):
from vizro.models._navigation import Navigation

_, navigation = next(model_manager._items_with_type(Navigation))

if self.pages is None:
self.pages = navigation.pages
@validator("items", always=True)
def validate_items(cls, items, values):
if not items:
if isinstance(values.get("pages"), list):
return [NavItem(pages=[page]) for page in values.get("pages")]
if isinstance(values.get("pages"), dict):
return [NavItem(pages=value) for page, value in values.get("pages").items()]

if not self.items:
if isinstance(self.pages, list):
self.items = [NavItem(pages=[page]) for page in self.pages]
if isinstance(self.pages, dict):
self.items = [NavItem(pages=value) for page, value in self.pages.items()]
return items

@_log_call
def build(self, active_page_id):
Expand Down
50 changes: 34 additions & 16 deletions vizro-core/src/vizro/models/_navigation/nav_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ class NavItem(VizroBaseModel):
"""Icon to be used in Navigation Panel of Dashboard.
Args:
tooltip (Optional[str]): Text to be displayed in the tooltip on icon hover.
icon (Optional[str]): URI (relative or absolute) of the embeddable content.
pages (NavPagesType): See [NavPagesType][vizro.models.types.NavPagesType].
selector (Optional[Accordion]): See [`Accordion`][vizro.models.Accordion]. Defaults to `None`.
icon (Optional[str]): URI (relative or absolute) of the embeddable content.
max_text_length (int): Character limit for `text` argument. Defaults to 9.
text (str): Text to be display below the icon. Defaults to `""`.
tooltip (Optional[str]): Text to be displayed in the tooltip on icon hover.
"""

tooltip: Optional[str]
icon: str = "home"
pages: NavPagesType
selector: Optional[Accordion] = None
text: Optional[str] = ""
max_text_length: int = 9
text: str = ""
tooltip: Optional[str] = None

# Re-used validators
_validate_pages = validator("pages", allow_reuse=True, always=True)(_validate_pages)
Expand All @@ -47,28 +50,46 @@ def pre_build(self):
self.tooltip = self.text
self.text = self.text[:self.max_text_length]

@validator("text", always=True)
def set_text(cls, text, values):
if text and len(text) > values.get("max_text_length"):
return text[: values.get("max_text_length")]
return text

@validator("tooltip", always=True, pre=True)
def set_tooltip(cls, tooltip, values):
if tooltip is None:
if values.get("text") and len(values.get("text")) > values.get("max_text_length"):
return values.get("text")
return tooltip

@_log_call
def build(self, active_page_id):
icon_first_page = (
list(itertools.chain(*self.pages.values()))[0] if isinstance(self.pages, dict) else self.pages[0]
)
icon_div = (
html.Span(self.icon, className="material-symbols-outlined")
if self.icon
else html.Div(className="hidden")
)
text_div = html.Div(children=[self.text], className="icon-text") if self.text else html.Div(className="hidden")

return dbc.Button(
id=self.id,
children=[
html.Div(
children=[
html.Span(self.icon, className="material-symbols-outlined"),
html.Div(
children=[self.text],
className="icon-text",
)
if self.text
else html.Div(className="hidden"),
icon_div,
text_div,
],
className="nav-icon-text",
),
self._create_icon_tooltip(),
],
className="icon-button",
href=dash.page_registry[self._get_first_page()]["relative_path"],
active=self._get_first_page() == active_page_id,
href=dash.page_registry[icon_first_page]["relative_path"],
active=icon_first_page == active_page_id,
)

def _create_icon_tooltip(self):
Expand All @@ -80,6 +101,3 @@ def _create_icon_tooltip(self):
className="custom-tooltip",
)
return tooltip

def _get_first_page(self):
return list(itertools.chain(*self.pages.values()))[0] if isinstance(self.pages, dict) else self.pages[0]
6 changes: 6 additions & 0 deletions vizro-core/src/vizro/models/_navigation/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class Navigation(VizroBaseModel):
def set_selector(cls, selector, values):
if selector is None:
return Accordion(pages=values.get("pages"))

if isinstance(selector, NavBar):
if selector.pages is None and selector.items is None:
selector.pages = values.get("pages")
selector.items = []
return selector
return selector

@_log_call
Expand Down
2 changes: 2 additions & 0 deletions vizro-core/src/vizro/models/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,5 @@ class OptionsDictType(TypedDict):
Union["Accordion", "NavBar"],
Field(discriminator="type", description="Component that makes up part of the navigation panel"),
]
"""Discriminated union. Permissible value types for selector attribute:
[`Accordion`][vizro.models.Accordion], [`NavBar`][vizro.models.NavBar]."""

0 comments on commit 388a93b

Please sign in to comment.