From 9eb03ee763fed97f902d568f918cec07ce31da90 Mon Sep 17 00:00:00 2001 From: Antony Milne Date: Fri, 17 Nov 2023 10:06:25 +0000 Subject: [PATCH] Lint --- .../docs/pages/user_guides/navigation.md | 32 +++++++++---------- vizro-core/src/vizro/_vizro.py | 3 +- .../src/vizro/models/_navigation/accordion.py | 2 +- .../src/vizro/models/_navigation/nav_bar.py | 15 +++++---- .../_navigation/{nav_item.py => nav_link.py} | 5 ++- .../vizro/models/_navigation/navigation.py | 9 +++--- .../models/_navigation/test_accordion.py | 2 +- .../vizro/models/_navigation/test_nav_bar.py | 6 ++-- .../vizro/models/_navigation/test_nav_item.py | 12 +++---- .../models/_navigation/test_navigation.py | 3 -- 10 files changed, 44 insertions(+), 45 deletions(-) rename vizro-core/src/vizro/models/_navigation/{nav_item.py => nav_link.py} (95%) diff --git a/vizro-core/docs/pages/user_guides/navigation.md b/vizro-core/docs/pages/user_guides/navigation.md index 531368897..fcf13e2f0 100644 --- a/vizro-core/docs/pages/user_guides/navigation.md +++ b/vizro-core/docs/pages/user_guides/navigation.md @@ -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") ), ], @@ -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") ), ], @@ -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") ), ], @@ -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") ), ], @@ -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() ) ) @@ -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( @@ -248,13 +248,13 @@ To customize default accordion provide a `dict` mapping of the page group title ### 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"]) @@ -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: @@ -297,12 +297,12 @@ Navigation( ```py - + vm.Navigation( selector=vm.NavBar( pages={ "First icon": [ - "My first page", + "My first page", "My second page" ], "Second icon": [ @@ -311,7 +311,7 @@ Navigation( } ) ) - + ``` @@ -326,7 +326,7 @@ Navigation( ```py - + vm.Navigation( selector=vm.NavBar( items=[ @@ -342,8 +342,8 @@ Navigation( ] ) ) - + ``` - \ No newline at end of file + diff --git a/vizro-core/src/vizro/_vizro.py b/vizro-core/src/vizro/_vizro.py index f5f247ccd..3295b8d94 100644 --- a/vizro-core/src/vizro/_vizro.py +++ b/vizro-core/src/vizro/_vizro.py @@ -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. diff --git a/vizro-core/src/vizro/models/_navigation/accordion.py b/vizro-core/src/vizro/models/_navigation/accordion.py index 12895ebbb..23543e3fd 100644 --- a/vizro-core/src/vizro/models/_navigation/accordion.py +++ b/vizro-core/src/vizro/models/_navigation/accordion.py @@ -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 diff --git a/vizro-core/src/vizro/models/_navigation/nav_bar.py b/vizro-core/src/vizro/models/_navigation/nav_bar.py index 5d20227e7..c1a11c9a0 100644 --- a/vizro-core/src/vizro/models/_navigation/nav_bar.py +++ b/vizro-core/src/vizro/models/_navigation/nav_bar.py @@ -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 @@ -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. diff --git a/vizro-core/src/vizro/models/_navigation/nav_item.py b/vizro-core/src/vizro/models/_navigation/nav_link.py similarity index 95% rename from vizro-core/src/vizro/models/_navigation/nav_item.py rename to vizro-core/src/vizro/models/_navigation/nav_link.py index 951797764..426158afc 100644 --- a/vizro-core/src/vizro/models/_navigation/nav_item.py +++ b/vizro-core/src/vizro/models/_navigation/nav_link.py @@ -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 diff --git a/vizro-core/src/vizro/models/_navigation/navigation.py b/vizro-core/src/vizro/models/_navigation/navigation.py index a506dd009..9226753f1 100644 --- a/vizro-core/src/vizro/models/_navigation/navigation.py +++ b/vizro-core/src/vizro/models/_navigation/navigation.py @@ -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 @@ -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 @@ -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 diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py b/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py index 2d7f1d3ce..458e8bdcc 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_accordion.py @@ -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 diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py index cf223f777..67a1bbd19 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_bar.py @@ -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" @@ -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() diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_item.py b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_item.py index d1a66a458..87524e649 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_nav_item.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_nav_item.py @@ -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 == [] @@ -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): @@ -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 @@ -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) @@ -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) diff --git a/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py b/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py index 1315533c4..67750106d 100644 --- a/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py +++ b/vizro-core/tests/unit/vizro/models/_navigation/test_navigation.py @@ -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