Skip to content

Commit 630679a

Browse files
authored
Merge pull request #496 from facultyai/update-examples
Update examples to use new active matching feature of navlink
2 parents 008543f + c05775e commit 630679a

File tree

6 files changed

+61
-128
lines changed

6 files changed

+61
-128
lines changed

examples/multi-page-apps/collapsible-sidebar-with-icons/app.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
disappear and only the icons remain. Visit www.fontawesome.com to find
55
alternative icons to suit your needs!
66
7-
dcc.Location is used to track the current location, setting the page content
8-
and the active menu item via callbacks.
7+
dcc.Location is used to track the current location, a callback uses the current
8+
location to render the appropriate page content. The active prop of each
9+
NavLink is set automatically according to the current pathname. To use this
10+
feature you must install dash-bootstrap-components >= 0.11.0.
911
1012
For more details on building multi-page Dash applications, check out the Dash
1113
documentation: https://dash.plot.ly/urls
@@ -38,23 +40,23 @@
3840
dbc.NavLink(
3941
[html.I(className="fas fa-home mr-2"), html.Span("Home")],
4042
href="/",
41-
id="home-link",
43+
active="exact",
4244
),
4345
dbc.NavLink(
4446
[
4547
html.I(className="fas fa-calendar-alt mr-2"),
4648
html.Span("Calendar"),
4749
],
4850
href="/calendar",
49-
id="calendar-link",
51+
active="exact",
5052
),
5153
dbc.NavLink(
5254
[
5355
html.I(className="fas fa-envelope-open-text mr-2"),
5456
html.Span("Messages"),
5557
],
5658
href="/messages",
57-
id="messages-link",
59+
active="exact",
5860
),
5961
],
6062
vertical=True,
@@ -70,7 +72,7 @@
7072

7173

7274
# set the content according to the current pathname
73-
@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
75+
@app.callback(Output("page-content", "children"), Input("url", "pathname"))
7476
def render_page_content(pathname):
7577
if pathname == "/":
7678
return html.P("This is the home page!")
@@ -88,23 +90,5 @@ def render_page_content(pathname):
8890
)
8991

9092

91-
# sets the active property on the navlink corresponding to the current page
92-
@app.callback(
93-
[
94-
Output(link_id, "active")
95-
for link_id in ["home-link", "calendar-link", "messages-link"]
96-
],
97-
[Input("url", "pathname")],
98-
)
99-
def toggle_active_links(pathname):
100-
if pathname == "/":
101-
return True, False, False
102-
elif pathname == "/calendar":
103-
return False, True, False
104-
elif pathname == "/messages":
105-
return False, False, True
106-
return False, False, False
107-
108-
10993
if __name__ == "__main__":
11094
app.run_server(debug=True)

examples/multi-page-apps/navbar.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
"""
22
This app uses NavbarSimple to navigate between three different pages.
33
4-
dcc.Location is used to track the current location. There are two callbacks,
5-
one uses the current location to render the appropriate page content, the other
6-
uses the current location to toggle the "active" properties of the navigation
7-
links. This means the link corresponding to the current page appears active,
8-
indicating to the user which page they are looking at.
4+
dcc.Location is used to track the current location. A callback uses the current
5+
location to render the appropriate page content. The active prop of each
6+
NavLink is set automatically according to the current pathname. To use this
7+
feature you must install dash-bootstrap-components >= 0.11.0.
98
109
For more details on building multi-page Dash applications, check out the Dash
1110
documentation: https://dash.plot.ly/urls
@@ -23,9 +22,9 @@
2322
dcc.Location(id="url"),
2423
dbc.NavbarSimple(
2524
children=[
26-
dbc.NavLink("Page 1", href="/page-1", id="page-1-link"),
27-
dbc.NavLink("Page 2", href="/page-2", id="page-2-link"),
28-
dbc.NavLink("Page 3", href="/page-3", id="page-3-link"),
25+
dbc.NavLink("Home", href="/", active="exact"),
26+
dbc.NavLink("Page 1", href="/page-1", active="exact"),
27+
dbc.NavLink("Page 2", href="/page-2", active="exact"),
2928
],
3029
brand="Navbar with active links",
3130
color="primary",
@@ -36,27 +35,14 @@
3635
)
3736

3837

39-
# this callback uses the current pathname to set the active state of the
40-
# corresponding nav link to true, allowing users to tell see page they are on
41-
@app.callback(
42-
[Output(f"page-{i}-link", "active") for i in range(1, 4)],
43-
[Input("url", "pathname")],
44-
)
45-
def toggle_active_links(pathname):
46-
if pathname == "/":
47-
# Treat page 1 as the homepage / index
48-
return True, False, False
49-
return [pathname == f"/page-{i}" for i in range(1, 4)]
50-
51-
5238
@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
5339
def render_page_content(pathname):
54-
if pathname in ["/", "/page-1"]:
55-
return html.P("This is the content of page 1!")
40+
if pathname == "/":
41+
return html.P("This is the content of the home page!")
42+
elif pathname == "/page-1":
43+
return html.P("This is the content of page 1. Yay!")
5644
elif pathname == "/page-2":
57-
return html.P("This is the content of page 2. Yay!")
58-
elif pathname == "/page-3":
59-
return html.P("Oh cool, this is page 3!")
45+
return html.P("Oh cool, this is page 2!")
6046
# If the user tries to reach a different page, return a 404 message
6147
return dbc.Jumbotron(
6248
[

examples/multi-page-apps/responsive-collapsible-sidebar/sidebar.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
collapse when on a small screen, and the custom CSS to hide the toggle, and
88
force the collapse to stay open when the screen is large.
99
10-
dcc.Location is used to track the current location. There are two callbacks,
11-
one uses the current location to render the appropriate page content, the other
12-
uses the current location to toggle the "active" properties of the navigation
13-
links.
10+
dcc.Location is used to track the current location, a callback uses the current
11+
location to render the appropriate page content. The active prop of each
12+
NavLink is set automatically according to the current pathname. To use this
13+
feature you must install dash-bootstrap-components >= 0.11.0.
1414
1515
For more details on building multi-page Dash applications, check out the Dash
1616
documentation: https://dash.plot.ly/urls
@@ -89,9 +89,9 @@
8989
dbc.Collapse(
9090
dbc.Nav(
9191
[
92-
dbc.NavLink("Page 1", href="/page-1", id="page-1-link"),
93-
dbc.NavLink("Page 2", href="/page-2", id="page-2-link"),
94-
dbc.NavLink("Page 3", href="/page-3", id="page-3-link"),
92+
dbc.NavLink("Home", href="/", active="exact"),
93+
dbc.NavLink("Page 1", href="/page-1", active="exact"),
94+
dbc.NavLink("Page 2", href="/page-2", active="exact"),
9595
],
9696
vertical=True,
9797
pills=True,
@@ -107,27 +107,14 @@
107107
app.layout = html.Div([dcc.Location(id="url"), sidebar, content])
108108

109109

110-
# this callback uses the current pathname to set the active state of the
111-
# corresponding nav link to true, allowing users to tell see page they are on
112-
@app.callback(
113-
[Output(f"page-{i}-link", "active") for i in range(1, 4)],
114-
[Input("url", "pathname")],
115-
)
116-
def toggle_active_links(pathname):
117-
if pathname == "/":
118-
# Treat page 1 as the homepage / index
119-
return True, False, False
120-
return [pathname == f"/page-{i}" for i in range(1, 4)]
121-
122-
123110
@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
124111
def render_page_content(pathname):
125-
if pathname in ["/", "/page-1"]:
126-
return html.P("This is the content of page 1!")
112+
if pathname == "/":
113+
return html.P("This is the content of the home page!")
114+
elif pathname == "/page-1":
115+
return html.P("This is the content of page 1. Yay!")
127116
elif pathname == "/page-2":
128-
return html.P("This is the content of page 2. Yay!")
129-
elif pathname == "/page-3":
130-
return html.P("Oh cool, this is page 3!")
117+
return html.P("Oh cool, this is page 2!")
131118
# If the user tries to reach a different page, return a 404 message
132119
return dbc.Jumbotron(
133120
[

examples/multi-page-apps/responsive-sidebar/sidebar.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
collapse when on a small screen, and the custom CSS to hide the toggle, and
88
force the collapse to stay open when the screen is large.
99
10-
dcc.Location is used to track the current location. There are two callbacks,
11-
one uses the current location to render the appropriate page content, the other
12-
uses the current location to toggle the "active" properties of the navigation
13-
links.
10+
dcc.Location is used to track the current location, a callback uses the current
11+
location to render the appropriate page content. The active prop of each
12+
NavLink is set automatically according to the current pathname. To use this
13+
feature you must install dash-bootstrap-components >= 0.11.0.
1414
1515
For more details on building multi-page Dash applications, check out the Dash
1616
documentation: https://dash.plot.ly/urls
@@ -76,9 +76,9 @@
7676
dbc.Collapse(
7777
dbc.Nav(
7878
[
79-
dbc.NavLink("Page 1", href="/page-1", id="page-1-link"),
80-
dbc.NavLink("Page 2", href="/page-2", id="page-2-link"),
81-
dbc.NavLink("Page 3", href="/page-3", id="page-3-link"),
79+
dbc.NavLink("Home", href="/", active="exact"),
80+
dbc.NavLink("Page 1", href="/page-1", active="exact"),
81+
dbc.NavLink("Page 2", href="/page-2", active="exact"),
8282
],
8383
vertical=True,
8484
pills=True,
@@ -94,27 +94,14 @@
9494
app.layout = html.Div([dcc.Location(id="url"), sidebar, content])
9595

9696

97-
# this callback uses the current pathname to set the active state of the
98-
# corresponding nav link to true, allowing users to tell see page they are on
99-
@app.callback(
100-
[Output(f"page-{i}-link", "active") for i in range(1, 4)],
101-
[Input("url", "pathname")],
102-
)
103-
def toggle_active_links(pathname):
104-
if pathname == "/":
105-
# Treat page 1 as the homepage / index
106-
return True, False, False
107-
return [pathname == f"/page-{i}" for i in range(1, 4)]
108-
109-
11097
@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
11198
def render_page_content(pathname):
112-
if pathname in ["/", "/page-1"]:
113-
return html.P("This is the content of page 1!")
99+
if pathname == "/":
100+
return html.P("This is the content of the home page!")
101+
elif pathname == "/page-1":
102+
return html.P("This is the content of page 1. Yay!")
114103
elif pathname == "/page-2":
115-
return html.P("This is the content of page 2. Yay!")
116-
elif pathname == "/page-3":
117-
return html.P("Oh cool, this is page 3!")
104+
return html.P("Oh cool, this is page 2!")
118105
# If the user tries to reach a different page, return a 404 message
119106
return dbc.Jumbotron(
120107
[

examples/multi-page-apps/sidebar-with-submenus/sidebar.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
This app creates a simple sidebar layout using inline style arguments and the
33
dbc.Nav component.
44
5-
dcc.Location is used to track the current location. There are two callbacks,
5+
dcc.Location is used to track the current location. There are three callbacks,
66
one uses the current location to render the appropriate page content, the other
7-
uses the current location to toggle the "active" properties of the navigation
8-
links.
7+
two are used to toggle the collapsing sections in the sidebar. They control the
8+
collapse component and the CSS that rotates the chevron icon respectively.
99
1010
For more details on building multi-page Dash applications, check out the Dash
1111
documentation: https://dash.plot.ly/urls
@@ -52,6 +52,7 @@
5252
],
5353
className="my-1",
5454
),
55+
style={"cursor": "pointer"},
5556
id="submenu-1",
5657
),
5758
# we use the Collapse component to hide and reveal the navigation links
@@ -75,6 +76,7 @@
7576
],
7677
className="my-1",
7778
),
79+
style={"cursor": "pointer"},
7880
id="submenu-2",
7981
),
8082
dbc.Collapse(

examples/multi-page-apps/simple_sidebar.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
This app creates a simple sidebar layout using inline style arguments and the
33
dbc.Nav component.
44
5-
dcc.Location is used to track the current location. There are two callbacks,
6-
one uses the current location to render the appropriate page content, the other
7-
uses the current location to toggle the "active" properties of the navigation
8-
links.
5+
dcc.Location is used to track the current location, and a callback uses the
6+
current location to render the appropriate page content. The active prop of
7+
each NavLink is set automatically according to the current pathname. To use
8+
this feature you must install dash-bootstrap-components >= 0.11.0.
99
1010
For more details on building multi-page Dash applications, check out the Dash
1111
documentation: https://dash.plot.ly/urls
@@ -46,9 +46,9 @@
4646
),
4747
dbc.Nav(
4848
[
49-
dbc.NavLink("Page 1", href="/page-1", id="page-1-link"),
50-
dbc.NavLink("Page 2", href="/page-2", id="page-2-link"),
51-
dbc.NavLink("Page 3", href="/page-3", id="page-3-link"),
49+
dbc.NavLink("Home", href="/", active="exact"),
50+
dbc.NavLink("Page 1", href="/page-1", active="exact"),
51+
dbc.NavLink("Page 2", href="/page-2", active="exact"),
5252
],
5353
vertical=True,
5454
pills=True,
@@ -62,27 +62,14 @@
6262
app.layout = html.Div([dcc.Location(id="url"), sidebar, content])
6363

6464

65-
# this callback uses the current pathname to set the active state of the
66-
# corresponding nav link to true, allowing users to tell see page they are on
67-
@app.callback(
68-
[Output(f"page-{i}-link", "active") for i in range(1, 4)],
69-
[Input("url", "pathname")],
70-
)
71-
def toggle_active_links(pathname):
72-
if pathname == "/":
73-
# Treat page 1 as the homepage / index
74-
return True, False, False
75-
return [pathname == f"/page-{i}" for i in range(1, 4)]
76-
77-
7865
@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
7966
def render_page_content(pathname):
80-
if pathname in ["/", "/page-1"]:
81-
return html.P("This is the content of page 1!")
67+
if pathname == "/":
68+
return html.P("This is the content of the home page!")
69+
elif pathname == "/page-1":
70+
return html.P("This is the content of page 1. Yay!")
8271
elif pathname == "/page-2":
83-
return html.P("This is the content of page 2. Yay!")
84-
elif pathname == "/page-3":
85-
return html.P("Oh cool, this is page 3!")
72+
return html.P("Oh cool, this is page 2!")
8673
# If the user tries to reach a different page, return a 404 message
8774
return dbc.Jumbotron(
8875
[

0 commit comments

Comments
 (0)