-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
160 lines (140 loc) · 4.42 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# package imports
import dash
import dash_bootstrap_components as dbc
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output, State
from dash import no_update
from flask import session, copy_current_request_context
# local imports
from auth import authenticate_user, validate_login_session
from server import app, server
# login layout content
def login_layout():
return html.Div(
[
dcc.Location(id='login-url',pathname='/login',refresh=False),
dbc.Container(
[
dbc.Row(
dbc.Col(
dbc.Card(
[
html.H4('Login',className='card-title'),
dbc.Input(id='login-email',placeholder='User'),
dbc.Input(id='login-password',placeholder='Assigned password',type='password'),
dbc.Button('Submit',id='login-button',color='success',block=True),
html.Br(),
html.Div(id='login-alert')
],
body=True
),
width=6
),
justify='center'
)
]
)
]
)
# home layout content
@validate_login_session
def app_layout():
return \
html.Div([
dcc.Location(id='home-url',pathname='/home'),
dbc.Container(
[
dbc.Row(
dbc.Col(
[
html.H2('Home page.')
],
),
justify='center'
),
html.Br(),
dbc.Row(
dbc.Col(
dbc.Button('Logout',id='logout-button',color='danger',block=True,size='sm'),
width=4
),
justify='center'
),
html.Br()
],
)
]
)
# main app layout
app.layout = html.Div(
[
dcc.Location(id='url',refresh=False),
html.Div(
login_layout(),
id='page-content'
),
]
)
###############################################################################
# utilities
###############################################################################
# router
@app.callback(
Output('page-content','children'),
[Input('url','pathname')]
)
def router(url):
if url=='/home':
return app_layout()
elif url=='/login':
return login_layout()
else:
return login_layout()
# authenticate
@app.callback(
[Output('url','pathname'),
Output('login-alert','children')],
[Input('login-button','n_clicks')],
[State('login-email','value'),
State('login-password','value')])
def login_auth(n_clicks,email,pw):
'''
check credentials
if correct, authenticate the session
otherwise, authenticate the session and send user to login
'''
if n_clicks is None or n_clicks==0:
return no_update,no_update
credentials = {'user':email,"password":pw}
if authenticate_user(credentials):
session['authed'] = True
return '/home',''
session['authed'] = False
return no_update,dbc.Alert('Incorrect credentials.',color='danger',dismissable=True)
@app.callback(
Output('home-url','pathname'),
[Input('logout-button','n_clicks')]
)
def logout_(n_clicks):
'''clear the session and send user to login'''
if n_clicks is None or n_clicks==0:
return no_update
session['authed'] = False
return '/login'
###############################################################################
# callbacks
###############################################################################
# @app.callback(
# Output('...'),
# [Input('...')]
# )
# def func(...):
# ...
###############################################################################
# run app
###############################################################################
if __name__ == "__main__":
app.run_server(
debug=True
)