-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
477 lines (379 loc) · 21.8 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import os
import components
import dash
import layouts
import loaders
import logging
import keydb
import psycopg2
from utils import callback_utils, data_utils, session_utils, app_utils, keydb_utils, plot_utils, cache_utils, UrlIndex
from dash.dash import no_update
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State, ALL, MATCH
# ==============================================================
# Define functions and variables of general use
# ==============================================================
def serve_layout():
try:
cache = keydb.KeyDB(connection_pool=keydb_pool)
cache.ping()
except (keydb.ConnectionError, TypeError, KeyError) as e:
app.logger.error('Redis connection error! {}'.format(e))
return layouts.RedisConnectionError()
session_id = session_utils.initiate_session(cache, app.logger, keydb_timeout)
return layouts.Base(session_id)
# ==============================================================
# Create dash.app
# ==============================================================
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.LUX, UrlIndex.FONT_AWESOME.value])
app.title = 'ConPlot'
server = app.server
app.config.suppress_callback_exceptions = True
if 'PRODUCTION_SERVER' in os.environ:
app.config.update({
'url_base_pathname': '/conplot/',
'routes_pathname_prefix': '/conplot/',
'requests_pathname_prefix': '/conplot/',
})
keydb_pool = keydb_utils.create_pool(os.environ.get('KEYDB_URL'))
keydb_timeout = os.environ.get('KEYDB_TIMEOUT')
app.layout = serve_layout
# ==============================================================
# Define callbacks for the app
# ==============================================================
@app.callback([Output('contact-alert-div', 'children'),
Output('submit-contact-form-button', 'disabled')],
[Input('issue-select', 'value')])
def toggle_alert(*args):
return callback_utils.toggle_alert(*args)
@app.callback(Output('custom-format-specs-modal', 'is_open'),
[Input('custom-format-specs-button', 'n_clicks')])
def toggle_customformatspecs_modal(n_clicks):
trigger = dash.callback_context.triggered[0]
return callback_utils.toggle_modal(trigger)
@app.callback(Output('gdpr-policy-modal', 'is_open'),
[Input('gdpr-policy-button', 'n_clicks')])
def toggle_gdpr_policy_modal(n_clicks):
trigger = dash.callback_context.triggered[0]
return callback_utils.toggle_modal(trigger)
@app.callback(Output('create-user-button', 'disabled'),
[Input('gdpr-agreement-checkbox', 'checked')],
[State('create-user-button', 'disabled')])
def toggle_createuser_button(checked, disabled):
trigger = dash.callback_context.triggered[0]
return callback_utils.toggle_createuserbutton(trigger, disabled)
@app.callback(Output({'type': 'tutorial-modal', 'index': MATCH}, 'is_open'),
[Input({'type': 'tutorial-button', 'index': MATCH}, 'n_clicks')])
def toggle_tutorial_modal(n_clicks):
trigger = dash.callback_context.triggered[0]
return callback_utils.toggle_modal(trigger)
@app.callback(Output({'type': 'palette-modal', 'index': MATCH}, 'is_open'),
[Input({'type': 'palette-button', 'index': MATCH}, 'n_clicks')])
def toggle_palette_modal(n_clicks):
trigger = dash.callback_context.triggered[0]
return callback_utils.toggle_modal(trigger)
@app.callback([Output('contact-form-modal-div', 'children'),
Output('contact-email-input', 'value'),
Output('contact-name-input', 'value'),
Output('issue-select', 'value'),
Output('contact-text-area-input', 'value')],
[Input('submit-contact-form-button', 'n_clicks')],
[State('contact-name-input', 'value'),
State('contact-email-input', 'value'),
State('issue-select', 'value'),
State('contact-text-area-input', 'value'),
State('session-id', 'data')])
def submit_contact_form(n_clicks, name, email, subject, description, session_id):
trigger = dash.callback_context.triggered[0]
cache = keydb.KeyDB(connection_pool=keydb_pool)
if session_utils.is_expired_session(session_id, cache, app.logger):
return components.SessionTimedOutModal(), None, None, None, None
elif not callback_utils.ensure_triggered(trigger):
return no_update, no_update, no_update, no_update, no_update
return callback_utils.submit_form(name, email, subject, description, app.logger), None, None, None, None
@app.callback([Output('track-selection-card', "color"),
Output('additional-tracks-upload', 'disabled')],
[Input('additional-track-selector', "value")])
def toggle_add_track_format(value):
return callback_utils.toggle_selection_alert(value)
@app.callback([Output("format-selection-card", "color"),
Output('upload-contact-component', 'disabled')],
[Input("contact-format-selector", 'value')])
def toggle_format_alert(*args):
return callback_utils.toggle_selection_alert(*args)
@app.callback(Output('page-content', 'children'),
[Input('url', 'pathname')],
[State('session-id', 'data')])
def display_page(url, session_id):
trigger = dash.callback_context.triggered[0]
cache = keydb.KeyDB(connection_pool=keydb_pool)
if url is None:
return no_update
elif session_utils.is_expired_session(session_id, cache, app.logger):
return layouts.SessionTimeout(session_id)
elif not callback_utils.ensure_triggered(trigger):
return no_update
app.logger.info('Session {} requested url {}'.format(session_id, url))
return app_utils.serve_url(url, session_id, cache, app.logger)
@app.callback([Output('invalid-login-collapse', 'is_open'),
Output('success-login-alert-div', 'children')],
[Input("require-user-login-button", 'n_clicks')],
[State('username-input', 'value'),
State('password-input', 'value'),
State('session-id', 'data')])
def require_user_login(n_clicks, username, password, session_id):
trigger = dash.callback_context.triggered[0]
cache = keydb.KeyDB(connection_pool=keydb_pool)
if session_utils.is_expired_session(session_id, cache, app.logger):
return no_update, components.SessionTimedOutToast()
elif not callback_utils.ensure_triggered(trigger):
return no_update, no_update
return app_utils.user_login(username, password, session_id, cache, app.logger)[:-1]
@app.callback([Output('user-portal-invalid-login-collapse', 'is_open'),
Output('user-portal-alert-div', 'children'),
Output('user-portal-div', 'children')],
[Input({'type': 'user-portal-button', 'idx': ALL}, 'n_clicks')],
[State('login-username-input', 'value'),
State('login-password-input', 'value'),
State('session-id', 'data')])
def user_portal(n_clicks, username, password, session_id):
trigger = dash.callback_context.triggered[0]
cache = keydb.KeyDB(connection_pool=keydb_pool)
if session_utils.is_expired_session(session_id, cache, app.logger):
return no_update, components.SessionTimedOutToast(), no_update
elif not callback_utils.ensure_triggered(trigger):
return no_update, no_update, no_update
if callback_utils.is_user_login(trigger):
return app_utils.user_login(username, password, session_id, cache, app.logger)
else:
return app_utils.user_logout(session_id, cache, app.logger)
@app.callback(Output('recovery-portal-modal-div', 'children'),
[Input('recover-account-button', 'n_clicks')],
[State('recovery-portal-username-input', 'value'),
State('recovery-portal-email-input', 'value'),
State('recovery-portal-secret-input', 'value'),
State('recovery-portal-password-1-input', 'value'),
State('recovery-portal-password-2-input', 'value')])
def recover_account(n_clicks, username, email, secret, password_1, password_2):
trigger = dash.callback_context.triggered[0]
if not callback_utils.ensure_triggered(trigger):
return no_update
return app_utils.recover_account(username, email, secret, password_1, password_2, app.logger)
@app.callback([Output('invalid-create-user-collapse', 'is_open'),
Output('create-user-modal-div', 'children'),
Output('create-username-input', 'value'),
Output('create-password-input', 'value'),
Output('create-email-input', 'value'),
Output('create-user-button-div', 'children')],
[Input("create-user-button", 'n_clicks')],
[State('create-username-input', 'value'),
State('create-password-input', 'value'),
State('create-email-input', 'value'),
State('session-id', 'data')])
def create_user(n_clicks, username, password, email, session_id):
trigger = dash.callback_context.triggered[0]
cache = keydb.KeyDB(connection_pool=keydb_pool)
if session_utils.is_expired_session(session_id, cache, app.logger):
return no_update, components.SessionTimedOutModal(), no_update, no_update, no_update, no_update
elif not callback_utils.ensure_triggered(trigger):
return no_update, no_update, no_update, no_update, no_update, no_update
return app_utils.create_user(username, password, email, session_id, cache, app.logger)
@app.callback(Output('success-change-password-alert-div', 'children'),
[Input('user-change-password-button', 'n_clicks')],
[State('old-password-input', 'value'),
State('new-password-input', 'value'),
State('session-id', 'data')])
def change_password(n_clicks, old_password, new_password, session_id):
trigger = dash.callback_context.triggered[0]
cache = keydb.KeyDB(connection_pool=keydb_pool)
if session_utils.is_expired_session(session_id, cache, app.logger):
return components.SessionTimedOutToast()
elif not callback_utils.ensure_triggered(trigger):
return no_update
return app_utils.change_password(new_password, old_password, cache, session_id, app.logger)
@app.callback(Output({'type': 'share-session-toast-div', 'index': MATCH}, 'children'),
[Input({'type': 'share-session-button', 'index': MATCH}, 'n_clicks')],
[State({'type': 'share-username-input', 'index': MATCH}, 'value'),
State({'type': 'share-username-input', 'index': MATCH}, 'id'),
State('session-id', 'data')])
def share_session(share_click, share_with, session_pkid, session_id):
trigger = dash.callback_context.triggered[0]
cache = keydb.KeyDB(connection_pool=keydb_pool)
session_pkid = session_pkid['index']
if session_utils.is_expired_session(session_id, cache, app.logger):
return components.SessionTimedOutToast()
elif not callback_utils.ensure_triggered(trigger):
return no_update
username, current_session_pkid = session_utils.get_current_info(session_id, cache)
if not share_with:
return components.InvalidUsernameToast()
elif username == share_with:
return components.ShareWithOwnerToast()
return session_utils.share_session(session_pkid, share_with, app.logger)
@app.callback([Output('session-storage-toast-div', 'children'),
Output('stored-sessions-list-spinner', 'children'),
Output('shared-sessions-list-spinner', 'children')],
[Input({'type': 'delete-session-button', 'index': ALL}, 'n_clicks'),
Input({'type': 'load-session-button', 'index': ALL}, 'n_clicks'),
Input({'type': 'stop-share-session-button', 'index': ALL}, 'n_clicks'),
Input({'type': 'load-share-session-button', 'index': ALL}, 'n_clicks')],
[State('session-id', 'data')])
def manage_stored_sessions(delete_clicks, load_click, stop_share, load_share, session_id):
trigger = dash.callback_context.triggered[0]
cache = keydb.KeyDB(connection_pool=keydb_pool)
if session_utils.is_expired_session(session_id, cache, app.logger):
return components.SessionTimedOutToast(), no_update, no_update
elif not callback_utils.ensure_triggered(trigger):
return no_update, no_update, no_update
selected_session_pkid, action = callback_utils.get_session_action(trigger)
username, current_session_pkid = session_utils.get_current_info(session_id, cache)
if action == callback_utils.ButtonActions.delete:
return session_utils.delete_session(selected_session_pkid, current_session_pkid, session_id, app.logger)
elif action == callback_utils.ButtonActions.load:
return session_utils.load_session(username, selected_session_pkid, session_id, cache, app.logger)
elif action == callback_utils.ButtonActions.stop:
return session_utils.stop_share_session(username, selected_session_pkid, current_session_pkid,
session_id, app.logger)
@app.callback(Output('store-session-modal-div', 'children'),
[Input('store-session-button', 'n_clicks')],
[State('new-session-name-input', 'value'),
State('session-id', 'data')])
def store_session(n_clicks, session_name, session_id):
trigger = dash.callback_context.triggered[0]
cache = keydb.KeyDB(connection_pool=keydb_pool)
if session_utils.is_expired_session(session_id, cache, app.logger):
return components.SessionTimedOutToast()
elif not callback_utils.ensure_triggered(trigger):
return no_update
return session_utils.store_session(session_name, session_id, cache, app.logger)
@app.callback([Output('sequence-filename-div', "children"),
Output('upload-sequence-component', 'contents'),
Output('sequence-upload-modal-div', 'children')],
[Input('upload-sequence-component', 'filename')],
[State('upload-sequence-component', 'contents'),
State('session-id', 'data')])
def upload_sequence(fname, fcontent, session_id):
trigger = dash.callback_context.triggered[0]
cache = keydb.KeyDB(connection_pool=keydb_pool)
if session_utils.is_expired_session(session_id, cache, app.logger):
return no_update, None, components.SessionTimedOutModal()
elif not callback_utils.ensure_triggered(trigger):
return callback_utils.retrieve_sequence_fname(session_id, cache), None, None
elif not cache_utils.is_valid_fname(fname):
return no_update, no_update, components.InvalidFnameModal(fname)
return data_utils.upload_sequence(fname, fcontent, session_id, cache, app.logger)
@app.callback([Output('contact-filenames-div', "children"),
Output('upload-contact-component', 'contents'),
Output('contact-upload-modal-div', 'children')],
[Input('upload-contact-component', 'filename')],
[State('upload-contact-component', 'contents'),
State("contact-format-selector", 'value'),
State('contact-filenames-div', "children"),
State('session-id', 'data')])
def upload_contact(fname, fcontent, input_format, fname_alerts, session_id):
trigger = dash.callback_context.triggered[0]
cache = keydb.KeyDB(connection_pool=keydb_pool)
if session_utils.is_expired_session(session_id, cache, app.logger):
return no_update, None, components.SessionTimedOutModal()
elif not callback_utils.ensure_triggered(trigger):
return callback_utils.retrieve_contact_fnames(session_id, cache), None, None
elif not cache_utils.is_valid_fname(fname):
return no_update, no_update, components.InvalidFnameModal(fname)
return data_utils.upload_dataset(fname, fcontent, input_format, fname_alerts, session_id, cache, app.logger,
dataset=loaders.DatasetReference.CONTACT_MAP.value)
@app.callback([Output('additional-tracks-filenames-div', 'children'),
Output('additional-tracks-upload', 'contents'),
Output('additional-tracks-upload-modal-div', 'children')],
[Input('additional-tracks-upload', 'filename')],
[State('additional-tracks-upload', 'contents'),
State('additional-track-selector', 'value'),
State('additional-tracks-filenames-div', 'children'),
State('session-id', 'data')])
def upload_additional_track(fname, fcontent, input_format, fname_alerts, session_id):
trigger = dash.callback_context.triggered[0]
cache = keydb.KeyDB(connection_pool=keydb_pool)
if session_utils.is_expired_session(session_id, cache, app.logger):
return no_update, None, components.SessionTimedOutModal()
elif not callback_utils.ensure_triggered(trigger):
return callback_utils.retrieve_additional_fnames(session_id, cache), None, None
return data_utils.upload_dataset(fname, fcontent, input_format, fname_alerts, session_id, cache, app.logger)
@app.callback(Output('removefiles-modal-div', 'children'),
[Input({'type': 'filename-alert', 'index': ALL}, 'is_open')],
[State('session-id', 'data')])
def remove_dataset(alerts_open, session_id):
trigger = dash.callback_context.triggered[-1]
cache = keydb.KeyDB(connection_pool=keydb_pool)
if not callback_utils.ensure_triggered(trigger):
return None
elif session_utils.is_expired_session(session_id, cache, app.logger):
return components.SessionTimedOutModal()
data_utils.remove_dataset(trigger, cache, session_id, app.logger)
return None
@app.callback([Output('javascript-exe', 'run'),
Output('javascript-exe-modal-div', 'children'),
Output('session-id', 'data')],
[Input({'type': 'javascript-exe-button', 'index': ALL}, 'n_clicks')],
[State('session-id', 'data')])
def javascript_exe_button(n_clicks, session_id):
trigger = dash.callback_context.triggered[-1]
cache = keydb.KeyDB(connection_pool=keydb_pool)
if not callback_utils.ensure_triggered(trigger):
return no_update, no_update, no_update
elif 'new-session' in trigger['prop_id'] or session_utils.is_expired_session(session_id, cache, app.logger):
cache = keydb.KeyDB(connection_pool=keydb_pool)
new_session_id = session_utils.initiate_session(cache, app.logger, keydb_timeout)
return "location.reload();", no_update, new_session_id
else:
app.logger.info('Fetching example data')
try:
session_utils.load_session('user_1', 46, session_id, cache, app.logger)
except (psycopg2.OperationalError, AttributeError) as e:
app.logger.error('Unable to fetch example data: {}'.format(e))
return no_update, components.ExampleSessionConnectionErrorModal(), no_update
return "location.reload();", no_update, no_update
@app.callback([Output('plot-div', 'children'),
Output('plot-modal-div', 'children'),
Output('display-control-cardbody', 'children'),
Output('refresh-button-2', 'disabled')],
[Input('plot-button', 'n_clicks'),
Input('refresh-button-2', 'n_clicks')],
[State('L-cutoff-input', 'value'),
State('contact-marker-size-input', 'value'),
State('track-marker-size-input', 'value'),
State('track-separation-size-input', 'value'),
State({'type': "track-select", 'index': ALL}, 'value'),
State({'type': "halfsquare-select", 'index': ALL}, 'value'),
State("transparent-tracks-switch", 'value'),
State('superimpose-maps-switch', 'value'),
State('distance-matrix-switch', 'value'),
State('verbose-labels-switch', 'value'),
State({'type': 'colorpalette-select', 'index': ALL}, 'value'),
State('session-id', 'data')])
def create_ConPlot(plot_click, refresh_click, factor, contact_marker_size, track_marker_size, track_separation,
track_selection, cmap_selection, transparent, superimpose, distance_matrix, verbose_labels,
selected_palettes, session_id):
trigger = dash.callback_context.triggered[0]
cache = keydb.KeyDB(connection_pool=keydb_pool)
if not callback_utils.ensure_triggered(trigger):
return no_update, None, no_update, no_update
elif session_utils.is_expired_session(session_id, cache, app.logger):
return components.PlotPlaceHolder(), components.SessionTimedOutModal(), components.DisplayControlCard(), True
app.logger.info('Session {} plot requested'.format(session_id))
if any([True for x in (factor, contact_marker_size, track_marker_size, track_separation) if x is None or x < 0]):
app.logger.info('Session {} invalid display control value detected'.format(session_id))
return no_update, components.InvalidInputModal(), no_update, no_update
elif superimpose and ('--- Empty ---' in cmap_selection or len(set(cmap_selection)) == 1):
return no_update, components.InvalidMapSelectionModal(), no_update, no_update
app.logger.info('Session {} creating conplot'.format(session_id))
return plot_utils.create_ConPlot(session_id, cache, trigger, track_selection, cmap_selection, selected_palettes,
factor, contact_marker_size, track_marker_size, track_separation, transparent,
superimpose, distance_matrix, verbose_labels)
# ==============================================================
# Start server
# ==============================================================
if __name__ != '__main__':
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
if __name__ == '__main__':
app.run_server(debug=True)