-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.py
86 lines (77 loc) · 2.28 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import dash_bootstrap_components as dbc
from dash import Input, Output, State, html, dcc, register_page, callback
import dash
from application import app, server
from flask_login import current_user
from utils.config import config
header = dbc.Navbar(
dbc.Container(
[
dbc.NavbarBrand(
[
html.Img(src="/assets/favicon.ico", height="30px"),
html.Span("Dash Auth Flow", style=dict(marginLeft="10px")),
],
href=config["HOME_PATH"],
style=dict(maxWidth="300px"),
),
dbc.Nav(
[
dbc.NavItem(dbc.NavLink("Home", href=config["HOME_PATH"])),
dbc.NavItem(dbc.NavLink("Page", href="/page")),
dbc.NavItem(
dbc.NavLink(
html.Span(id="user-name-nav"),
href="/profile",
)
),
dbc.NavItem(dbc.NavLink("Login", id="user-action", href="/login")),
],
),
],
),
# className="mb-5",
color="dark",
dark=True,
)
app.layout = html.Div(
[
header,
html.Br(),
dbc.Container(dash.page_container),
dcc.Location(id="url", refresh=True),
html.Div(id="profile-trigger", style=dict(display="none")),
]
)
@callback(
Output("user-name-nav", "children"),
Input("url", "pathname"),
Input("profile-trigger", "children"),
)
def profile_link(_, __):
"""
Returns a navbar link to the user profile if the user is authenticated
"""
if current_user.is_authenticated:
return [
html.I(className="bi bi-person-circle", style=dict(marginRight="5px")),
current_user.first,
]
else:
return ""
@callback(
Output("user-action", "children"),
Output("user-action", "href"),
Input("url", "pathname"),
)
def user_logout(_):
"""
returns a navbar link to /logout or /login, respectively, if the user is authenticated or not
"""
if current_user.is_authenticated:
out = "Logout", "/logout"
else:
out = "Login", "/login"
return out
if __name__ == "__main__":
app.run_server(debug=True)