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 0ab4dfc commit 9eb03ee
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 45 deletions.
32 changes: 16 additions & 16 deletions vizro-core/docs/pages/user_guides/navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ If you want to deviate from the default title `SELECT PAGE` and instead provide
title="My first page",
components=[
vm.Graph(
id="scatter_chart",
id="scatter_chart",
figure=px.scatter(iris, x="sepal_length", y="petal_width", color="species")
),
],
Expand All @@ -44,7 +44,7 @@ If you want to deviate from the default title `SELECT PAGE` and instead provide
title="My second page",
components=[
vm.Graph(
id="line_chart",
id="line_chart",
figure=px.line(iris, x="sepal_length", y="petal_width", color="species")
),
],
Expand Down Expand Up @@ -124,7 +124,7 @@ Currently available selectors are [`NavBar`][vizro.models.NavBar] and [`Accordio
title="My first page",
components=[
vm.Graph(
id="scatter_chart",
id="scatter_chart",
figure=px.scatter(iris, x="sepal_length", y="petal_width", color="species")
),
],
Expand All @@ -133,7 +133,7 @@ Currently available selectors are [`NavBar`][vizro.models.NavBar] and [`Accordio
title="My second page",
components=[
vm.Graph(
id="line_chart",
id="line_chart",
figure=px.line(iris, x="sepal_length", y="petal_width", color="species")
),
],
Expand All @@ -142,7 +142,7 @@ Currently available selectors are [`NavBar`][vizro.models.NavBar] and [`Accordio
dashboard = vm.Dashboard(
pages=[page_1, page_2],
navigation=vm.Navigation(
pages=["My first page", "My second page"],
pages=["My first page", "My second page"],
selector=vm.NavBar()
)
)
Expand Down Expand Up @@ -194,7 +194,7 @@ Currently available selectors are [`NavBar`][vizro.models.NavBar] and [`Accordio
## Customizing Selectors
### Accordion
To customize default accordion provide a `dict` mapping of the page group title and a list of page IDs to the `pages` argument of the [`Accordion`][vizro.models.Accordion] model.
To customize default accordion provide a `dict` mapping of the page group title and a list of page IDs to the `pages` argument of the [`Accordion`][vizro.models.Accordion] model.
```
Navigation(
Expand Down Expand Up @@ -248,13 +248,13 @@ To customize default accordion provide a `dict` mapping of the page group title
</table>
### NavBar
To add navigation bar with icons to your dashboard provide [`NavBar`][vizro.models.NavBar] model to the `selector` argument of the `Navigation`.
To add navigation bar with icons to your dashboard provide [`NavBar`][vizro.models.NavBar] model to the `selector` argument of the `Navigation`.
NavBar model has two arguments `pages` and `items`.
NavBar model has two arguments `pages` and `items`.
- **`NavBar.pages`**
Use `pages` for default icon image and configuration.
Use `pages` for default icon image and configuration.
```
Navigation(
selector=NavBar(pages=["My first page", "My second page"])
Expand All @@ -263,7 +263,7 @@ Use `pages` for default icon image and configuration.
- **`NavBar.items`**
Use `items` to add customization to the icons. `NavBar.items` is provided as list of `NavLink` models. `NavLink` model is responsible for building individual icons within navigation bar.
Use `items` to add customization to the icons. `NavBar.items` is provided as list of `NavLink` models. `NavLink` model is responsible for building individual icons within navigation bar.
For further customizations of icons, you can refer to the [`NavLink`][vizro.models.NavLink] reference. Some popular choices are:
Expand Down Expand Up @@ -297,12 +297,12 @@ Navigation(
<td style="width:360px">
```py
vm.Navigation(
selector=vm.NavBar(
pages={
"First icon": [
"My first page",
"My first page",
"My second page"
],
"Second icon": [
Expand All @@ -311,7 +311,7 @@ Navigation(
}
)
)
```
</td>
Expand All @@ -326,7 +326,7 @@ Navigation(
<td style="width:360px">
```py
vm.Navigation(
selector=vm.NavBar(
items=[
Expand All @@ -342,8 +342,8 @@ Navigation(
]
)
)
```
</td>
</tr>
</table>
</table>
3 changes: 2 additions & 1 deletion vizro-core/src/vizro/_vizro.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def __init__(self, **kwargs):
"""
self.dash = dash.Dash(**kwargs, use_pages=True, pages_folder="")
self.dash.config.external_stylesheets.append(
"https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined")
"https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined"
)

# Include Vizro assets (in the static folder) as external scripts and stylesheets. We extend self.dash.config
# objects so the user can specify additional external_scripts and external_stylesheets via kwargs.
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
@@ -1,6 +1,6 @@
import itertools
from collections.abc import Mapping
from typing import Dict, List, Literal, Optional
from typing import Dict, List, Literal

import dash
import dash_bootstrap_components as dbc
Expand Down
15 changes: 9 additions & 6 deletions vizro-core/src/vizro/models/_navigation/nav_bar.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations
from collections.abc import Mapping

import itertools
from typing import List, Optional, cast, Literal, Dict
from collections.abc import Mapping
from typing import Dict, List, Literal

from dash import html
from pydantic import validator, Field
from pydantic import Field, validator

from vizro.models import VizroBaseModel
from vizro.models._models_utils import _log_call
Expand Down Expand Up @@ -40,10 +39,14 @@ def coerce_pages_type(cls, pages):

@_log_call
def pre_build(self):
self.items = self.items or [NavLink(text=group_title, pages=pages) for group_title, pages in self.pages.items()]
self.items = self.items or [
NavLink(label=group_title, pages=pages) for group_title, pages in self.pages.items()
]

for position, item in enumerate(self.items, 1):
item.icon = item.icon or f"filter_{position}" if position <= 9 else "filter_9+"
# The filter icons are named filter_1, filter_2, etc. up to filter_9. If there are more than 9 items, then
# the 10th and all subsequent items are named filter_9+.
item.icon = item.icon or f"filter_{position}" if position <= 9 else "filter_9+" # noqa: PLR2004

# Since models instantiated in pre_build do not themselves have pre_build called on them, we call it manually
# here.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from __future__ import annotations

import itertools
import os
from typing import Optional, Literal, Dict, List, Union
from typing import Optional

import dash
import dash_bootstrap_components as dbc
from dash import html
from pydantic import Field, root_validator, validator, PrivateAttr
from pydantic import Field, PrivateAttr, validator

from vizro.models import VizroBaseModel
from vizro.models._models_utils import _log_call
Expand Down
9 changes: 4 additions & 5 deletions vizro-core/src/vizro/models/_navigation/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from vizro.models._models_utils import _log_call
from vizro.models._navigation._navigation_utils import _validate_pages
from vizro.models._navigation.accordion import Accordion
from vizro.models._navigation.nav_bar import NavBar
from vizro.models.types import NavPagesType, NavSelectorType


Expand All @@ -23,7 +22,7 @@ class Navigation(VizroBaseModel):
Defaults to `None`.
"""

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

# validators
Expand All @@ -43,9 +42,9 @@ def pre_build(self):
def build(self, *, active_page_id=None):
nav_selector = self.nav_selector.build(active_page_id=active_page_id)
if "nav_bar_outer" not in nav_selector:
# e.g. nav_selector is Accordion and nav_selector.build returns single html.Div with id="nav_panel_outer". This will
# make it match the case e.g. nav_selector is NavBar and nav_selector.build returns html.Div containing children
# with id="nav_bar_outer" and id="nav_panel_outer"
# e.g. nav_selector is Accordion and nav_selector.build returns single html.Div with id="nav_panel_outer".
# This will make it match the case e.g. nav_selector is NavBar and nav_selector.build returns html.Div
# containing children with id="nav_bar_outer" and id="nav_panel_outer"
nav_selector = html.Div([html.Div(className="hidden", id="nav_bar_outer"), nav_selector])

return nav_selector
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from dash import html
from pydantic import ValidationError

from vizro._constants import ACCORDION_DEFAULT_TITLE
import vizro.models as vm
from vizro._constants import ACCORDION_DEFAULT_TITLE


@pytest.fixture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_nav_bar_mandatory_only(self):
assert nav_bar.items == []

def test_nav_bar_mandatory_and_optional(self, pages_as_dict):
nav_link = vm.NavLink(label="Text")
nav_link = vm.NavLink(label="Label")
nav_bar = vm.NavBar(id="nav_bar", pages=pages_as_dict, items=[nav_link])

assert nav_bar.id == "nav_bar"
Expand Down Expand Up @@ -60,8 +60,8 @@ def test_default_items(self, pages_as_dict):

def test_items_with_with_pages_icons(self, pages_as_dict):
nav_links = [
vm.NavLink(label="Text", pages={"Group 1": ["Page 1"]}, icon="Home"),
vm.NavLink(label="Text", pages={"Group 2": ["Page 2"]}),
vm.NavLink(label="Label", pages={"Group 1": ["Page 1"]}, icon="Home"),
vm.NavLink(label="Label", pages={"Group 2": ["Page 2"]}),
]
nav_bar = vm.NavBar(pages=pages_as_dict, items=nav_links)
nav_bar.pre_build()
Expand Down
12 changes: 6 additions & 6 deletions vizro-core/tests/unit/vizro/models/_navigation/test_nav_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class TestNavLinkInstantiation:
"""Tests NavLink model instantiation."""

def test_nav_link_mandatory_only(self):
nav_link = vm.NavLink(label="Text")
nav_link = vm.NavLink(label="Label")

assert hasattr(nav_link, "id")
assert nav_link.label == "Text"
assert nav_link.label == "Label"
assert nav_link.icon is None
assert nav_link.pages == []

Expand All @@ -32,7 +32,7 @@ def test_nav_link_mandatory_and_optional(self, pages_as_list):
assert nav_link.pages == pages_as_list

def test_nav_link_valid_pages_as_dict(self, pages_as_dict):
nav_link = vm.NavLink(pages=pages_as_dict, label="Text")
nav_link = vm.NavLink(pages=pages_as_dict, label="Label")
assert nav_link.pages == pages_as_dict

def test_mandatory_label_missing(self):
Expand All @@ -59,7 +59,7 @@ def test_invalid_page(self, pages):
@pytest.mark.usefixtures("vizro_app", "prebuilt_dashboard")
class TestNavLinkPreBuildMethod:
def test_nav_link(self, pages_as_dict):
nav_link = vm.NavLink(label="Text", pages=pages_as_dict)
nav_link = vm.NavLink(label="Label", pages=pages_as_dict)
nav_link.pre_build()
assert isinstance(nav_link._nav_selector, vm.Accordion)
assert nav_link._nav_selector.pages == pages_as_dict
Expand All @@ -70,7 +70,7 @@ class TestNavLinkBuildMethod:
"""Tests NavLink model build method."""

def test_nav_link_active(self, pages_as_dict):
nav_link = vm.NavLink(label="Text", pages=pages_as_dict)
nav_link = vm.NavLink(label="Label", pages=pages_as_dict)
nav_link.pre_build()
built_nav_link = nav_link.build(active_page_id="Page 1")
assert isinstance(built_nav_link[nav_link.id], dbc.Button)
Expand All @@ -79,7 +79,7 @@ def test_nav_link_active(self, pages_as_dict):
assert isinstance(built_nav_link["nav_panel_outer"], html.Div)

def test_nav_link_not_active(self, pages_as_dict):
nav_link = vm.NavLink(label="Text", pages=pages_as_dict)
nav_link = vm.NavLink(label="Label", pages=pages_as_dict)
nav_link.pre_build()
built_nav_link = nav_link.build(active_page_id="Page 3")
assert isinstance(built_nav_link[nav_link.id], dbc.Button)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
"""Unit tests for vizro.models.Navigation."""
import json
import re

import plotly
import pytest
from dash import html
from pydantic import ValidationError

import vizro.models as vm
from vizro._constants import ACCORDION_DEFAULT_TITLE


# AM: move to dashboard tests
Expand Down

0 comments on commit 9eb03ee

Please sign in to comment.