-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (54 loc) · 2.73 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
import dash
from dash import Output, Input
import utils.data
import visualizations.bar
import visualizations.country
import visualizations.geo
import visualizations.root
from utils.data import pirate_attacks, format_colname
external_stylesheets = [
'https://codepen.io/chriddyp/pen/bWLwgP.css',
]
app = dash.Dash(__name__, title='Global Maritime Attacks',
external_stylesheets=external_stylesheets,
suppress_callback_exceptions=True)
app.layout = visualizations.root.layout
server = app.server
@app.callback(Output(component_id='map-graph', component_property='figure'),
Input(component_id='range-slider', component_property='value'),
Input(component_id='attack-type-dropdown', component_property='value'),
Input(component_id='map-graph', component_property='selectedData'))
def update_map(slider, attack_types, selected_data):
data = pirate_attacks.copy()
if selected_data:
# start_time = time.time()
ref_idx = [p['customdata'][-1] for p in selected_data['points']]
data['gray_mask'] = data.reference_id.apply(lambda ref_id: ref_id not in ref_idx)
data.loc[data['gray_mask'], 'shaded_color'] = 'gray'
# print(f"Color updated in {time.time() - start_time}!")
data = utils.data.filter_data(df=data, range=slider, attack_types=attack_types)
visualizations.geo.update_map(visualizations.geo.map, data)
return visualizations.geo.map
@app.callback(Output(component_id='hist', component_property='figure'),
Output(component_id='plot-name', component_property='children'),
Input(component_id='range-slider', component_property='value'),
Input(component_id='attack-type-dropdown', component_property='value'),
Input(component_id='plot-type-dropdown', component_property='value'),
Input(component_id='map-graph', component_property='selectedData'))
def update_plot(slider, attack_types, plot_type, selected_data):
data = pirate_attacks.copy()
data = utils.data.filter_data(df=data, range=slider, attack_types=attack_types, selected_data=selected_data)
bar = visualizations.bar.create_bar(data, col=plot_type)
return bar, format_colname(plot_type)
@app.callback(Output(component_id='country-viz', component_property='children'),
Input(component_id='map-graph', component_property='clickData'))
def update_plot(clicked_data):
if clicked_data:
customdata = clicked_data['points'][0]['customdata']
country_code = customdata[3]
year = customdata[4]
return visualizations.country.get_viz(country_code, year)
else:
raise dash.exceptions.PreventUpdate
if __name__ == '__main__':
app.run_server(debug=True)