-
Notifications
You must be signed in to change notification settings - Fork 1
/
layouts.py
444 lines (338 loc) · 20 KB
/
layouts.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
#This script contains the layout of the app; It uses html and css components to style the page which are available
#through Dash
#Import all required libraries
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
#Function to create layout and to display the plots
#def layout_(df, fig, fig1, fig10, fig3, fig4, fig72, fig82, fig31, fig32, fig42, fig40, fig50, fig51, filter_options, filter_india_options):
def layout_(df, fig, fig1, fig10, fig3, fig4, fig82, fig31, fig32, fig42, fig40, fig50, fig51, filter_options, filter_india_options):
#Pre=defined CSS styles to be used for tab style, text colour etc
colors = {
'background': '#141e30',
'text': '#000000'
}
tabs_styles = {
'height': '44px'
}
tab_style = {
'borderBottom': '1px solid #d6d6d6',
'padding': '6px',
'fontWeight': 'bold'
}
tab_selected_style = {
'borderTop': '1px solid #d6d6d6',
'borderBottom': '1px solid #d6d6d6',
'backgroundColor': '#012a47',
'color': 'white',
'padding': '6px'
}
tab_style_sub = {
'borderBottom': '1px solid #d6d6d6',
'padding': '6x',
'fontWeight': 'bold'
}
tab_selected_style_sub = {
'borderTop': '1px solid #d6d6d6',
'borderBottom': '1px solid #d6d6d6',
'backgroundColor': '#274052',
'color': 'white',
'padding': '6px'
}
#code to get region names according to number; countries_dict contains an iterator where each region is paired
#with its region number, then region_options contains a list of the names of each region mapped with its number
#and then the list is sorted and displayed with an all regions option
regions = df.region_txt.unique()
region_numbers = df.region.unique()
regions_dict = dict(zip(regions, region_numbers))
region_options = [{'label': region, 'value': number} for region, number in regions_dict.items()]
region_options = sorted(region_options, key = lambda i: i['label'])
region_options.insert(0, {'label' : 'ALL', 'value' : 0})
#Import standard stylesheet to be used and initialise the app
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
#Main layout of webpage
app.layout = html.Div(style={'background': 'url("assets/terror-min.png") no-repeat center center fixed', 'backgroundRepeat': 'no-repeat', 'backgroundSize': 'cover', 'max-width': '100%', 'height': '100%', 'overflow-x': 'hidden'}, children=[
#Create tabs
dcc.Tabs(id="tabs-styled-with-inline", value='tab-1', children=[
#First tab to display the map; also the default tab
dcc.Tab(label='Map tab', value='tab-1', style=tab_style, selected_style=tab_selected_style, children=[
html.Br(),
#Header
html.H1(
children='Welcome to Global Terrorism Database Dashboard',
style={
'textAlign': 'center',
'color': '#FFFFFF'
}
),
html.Br(),
#Sub-Header
html.Div(children='Enter the details to generate infographics.', style={
'textAlign': 'center',
'fontSize': '20px',
'color': '#FFFFFF'
}),
html.Br(),
#Define div component that contains month and region filters
html.Div(style={'width' : '20%', 'display' : 'inline-block', 'verticalAlign' : 'left', 'textAlign' : 'center', 'color' : colors['text'], 'marginLeft' : '120px'}, children=[
html.Br(),
html.Br(),
html.Label('Select Month', style={'color' : '#FFFFFF', 'fontSize': '16px'}),
dcc.Dropdown(
id='Month',
options = [
{'label' : 'January', 'value' : 1},
{'label' : 'February', 'value' : 2},
{'label' : 'March', 'value' : 3},
{'label' : 'April', 'value' : 4},
{'label' : 'May', 'value' : 5},
{'label' : 'June', 'value' : 6},
{'label' : 'July', 'value' : 7},
{'label' : 'August', 'value' : 8},
{'label' : 'September', 'value' : 9},
{'label' : 'October', 'value' : 10},
{'label' : 'November', 'value' : 11},
{'label' : 'December', 'value' : 12},
],
placeholder = 'Choose a month'
),
html.Br(),
html.Label('Select Region', style={'color' : '#FFFFFF'}),
dcc.Dropdown(
id = 'Region',
options = region_options[0:12],
placeholder = 'Choose a region'
),
]),
#Define div component that contains date and country filters
html.Div(style={'width' : '20%', 'display' : 'inline-block', 'verticalAlign' : 'left', 'textAlign' : 'center', 'color' : colors['text'], 'marginLeft' : '135px'}, children=[
html.Br(),
html.Br(),
html.Label('Select Date', style={'color' : '#FFFFFF', 'fontSize': '16px'}),
dcc.Dropdown(
id='Date',
options = [{'label': date, 'value': date} for date in range(1, 32)],
placeholder = 'Choose a date'
),
html.Br(),
html.Label('Select Country', style={'color' : '#FFFFFF', 'fontSize': '16px'}),
dcc.Dropdown(
id='Country',
placeholder = 'Choose a country'
),
]),
#Define div component that contains type of attack and city filters
html.Div(style={'width' : '20%', 'display' : 'inline-block', 'verticalAlign' : 'right', 'textAlign' : 'center', 'color' : colors['text'], 'marginLeft' : '150px'}, children=[
html.Br(),
html.Br(),
html.Label('Select Type of Attack', style={'color' : '#FFFFFF', 'fontSize': '16px'}),
dcc.Dropdown(
id='Type of Attack',
options = [
{'label' : 'ALL', 'value' : 0},
{'label' : 'Assasination', 'value' : 1},
{'label' : 'Armed Assault', 'value' : 2},
{'label' : 'Bombing/Explosion', 'value' : 3},
{'label' : 'Hijacking', 'value' : 4},
{'label' : 'Barricade Incident', 'value' : 5},
{'label' : 'Kidnapping', 'value' : 6},
{'label' : 'Facility/Infrastructure Attack', 'value' : 7},
{'label' : 'Unarmed Assault', 'value' : 8},
{'label' : 'Unknown', 'value' : 9},
],
placeholder = 'Choose an attack type'
),
html.Br(),
html.Label('Select City', style={'color' : '#FFFFFF', 'fontSize': '16px'}),
dcc.Dropdown(
id='City',
placeholder = 'Choose a city'
),
]),
html.Br(),
html.Br(),
html.Br(),
#Div that contains the 2 buttons- Generate Map and Clear Fields
html.Div(style={'width' : '100%', 'display' : 'inline-block', 'textAlign' : 'center', 'color' : colors['text'], 'marginLeft' : '10px'}, children=[
html.Br(),
html.Br(),
html.Br(),
html.Button('Generate Map', id = 'generate', style={'color' : '#FFFFFF', 'fontSize' : '12px', 'width': '180px', 'display' : 'inline-block', 'marginRight': '30px', 'textAlign' : 'center'}, hidden=False),
#html.Button('Clear', id = 'clear', style={'color' : '#FFFFFF', 'fontSize' : '12px', 'width': '180px', 'display': 'inline-block', 'marginRight': '30px', 'textAlign' : 'center'}),
html.Br(),
html.Br(),
html.Br(),
]),
html.Div(id='mymap', style={'width' : '100%', 'display' : 'inline-block', 'verticalAlign' : 'middle', 'textAlign' : 'center', 'color' : colors['text']}, children=[
html.Div(id='mapmessage', style={'color' : '#FFFFFF', 'fontSize' : '20px', 'marginTop' : '-20px'}, hidden=True),
html.Br(),
html.Br(),
html.Iframe(id='map', srcDoc=None, width='100%', height='600', hidden=True)
]),
]),
#Second tab that contains all the infographics
dcc.Tab(label='Infographics tab', id='info_', value='tab-2', style=tab_style, selected_style=tab_selected_style ,children=[
#Create subtabs
dcc.Tabs(id='subtabs', value='subtab', children=[
#First subtab that displays region-wise and world infographics
dcc.Tab(label='World Infographics', id='subtab1', value='subtab1', style=tab_style_sub, selected_style=tab_selected_style_sub, children=[
#html.Div(style={'background': 'url("assets/terrorism-1.png") no-repeat center center fixed',
# 'background-repeat': 'no-repeat', 'backgroundSize': 'cover', 'width': '100%', 'height': '100%'}, children=[
html.Div(style={'width': '100%', 'height': '100%'}, children=[
#Div that contains different infographics and plots
html.Div(id='plots',style={'width' : '100%', 'display' : 'none'}, children=[
#Display the attack density map that shows the concentration of attacks worldwide over all years
html.Div(children=[
dcc.Graph(id='densitymap', figure=fig, style={'width': 1370, 'height': 700, 'marginLeft': '-20px', 'marginBottom' : '-45px'}),
]),
#Display stacked bar chart that displays number of attacks according to attack type
#for each region per year
html.Div(children=[
dcc.Graph(id='stackedbarmap', figure=fig1, style={'width': 1370, 'height': 700, 'marginLeft': '-20px', 'marginBottom' : '0px'}),
]),
#Display animated bar plot that shows the people killed in each region per year
html.Div(children=[
dcc.Graph(id='barplot', figure=fig10, style={'width': 1370, 'height': 700, 'marginLeft': '0px', 'marginBottom' : '0px'}),
]),
#Display heatmap that shows the number of attacks per region over all years
html.Div(children=[
dcc.Graph(id='regionheatmap', figure=fig3, style={'width': 1370, 'height': 700, 'marginLeft': '0px', 'marginBottom' : '-45px'}),
]),
#Display scatter geo plot that shows an animated plot of attacks worldwide per year
html.Div(children=[
dcc.Graph(id='scattergeo', figure=fig4, style={'width': 1370, 'height': 600, 'marginLeft': '0px', 'marginBottom' : '0px'}),
]),
]),
]),
]),
#Second subtab that displays India specific infographics
dcc.Tab(label='India Infographics', id='subtab2', value='subtab2', style=tab_style_sub, selected_style=tab_selected_style_sub, children=[
html.Div(style={'width': '100%', 'height': '100%'}, children=[
#Div that contains different infographics and plot
html.Div(id='plots2',style={'width' : '100%', 'display' : 'none'}, children=[
#Stacked bar graph showing number of attacks per state over all years
html.Div(children=[
dcc.Graph(id='indgeo', figure=fig40, style={'width': 1365, 'height': 650, 'marginLeft': '-5px', 'marginBottom' : '-38px'}),
]),
html.Br(),
html.Br(),
#Animated bar chart that displays the fatalities for each state per year
html.Div(children=[
dcc.Graph(id='indbar-3', figure=fig31, style={'width': 1370, 'height': 700, 'marginLeft': '0px', 'marginBottom' : '-19px'}),
]),
html.Br(),
#Bar chart displaying people killed in each state over all years
html.Div(children=[
dcc.Graph(id='indbar-4', figure=fig32, style={'width': 1370, 'height': 700, 'marginLeft': '0px', 'marginBottom' : '-45px'}),
]),
html.Br(),
html.Br(),
#Pie chart displaying the percentage of type of weapons used in attacks over all years
html.Div(children=[
dcc.Graph(id='indbar-2', figure=fig82, style={'width': 1365, 'height': 600, 'marginLeft': '0px', 'marginBottom' : '0px'}),
]),
#A world map that plays the locations of each attack in India for each year
html.Div(children=[
dcc.Graph(id='indpie', figure=fig42, style={'width': 1370, 'height': 600, 'marginLeft': '0px', 'marginBottom' : '0px'}),
]),
]),
]),
]),
]),
]),
#Third tab that displays the Database Explorer
dcc.Tab(label='Explorer tab', id='exp', value='tab-3', style=tab_style, selected_style=tab_selected_style ,children=[
dcc.Tabs(id='subtabs2', value='subtab2', children=[
dcc.Tab(label='World Explorer', id='subtab3', value='subtab3', style=tab_style_sub, selected_style=tab_selected_style_sub, children=[
html.Div(style={'backgroundColor': '#FFFFFF'}, children=[
html.Br(),
html.H1(style={'textAlign': 'center', 'color': '#000000', 'textDecoration': 'underline', 'marginTop': '20px'}, children='Global Terrorism Database Explorer'),
html.Br(),
html.Div(style={'width': '80%'}, children=[
html.Div(style={'width': '100%', 'display': 'flex', 'textAlign': 'center', 'marginLeft': '350px'}, children=[
html.Label('Incidents per Year Grouped by ', style={'color' : ' #000000', 'fontSize': '30px', 'textAlign': 'center', 'float': 'left'}),
dcc.Dropdown(
id='filter',
options=[{'label': j, 'value': i} for i, j in enumerate(filter_options)],
value=6,
clearable=False,
style={'width': '250px', 'marginLeft': '10px', 'marginTop': '3px'}
),
]),
html.Br(),
html.Br(),
dcc.Graph(id='plots3', figure=fig50, style={'width': 1300, 'height': 600}),
html.Br(),
html.Br(),
html.Div(style={'width': '40%', 'display': 'inline-block', 'textAlign': 'center', 'marginLeft': '450px'}, children=[
dcc.Input(id='searchtext', style={'color' : '#000000', 'textAlign': 'center', 'width': '300px'}, placeholder='Enter filter text.'),
html.Div(id='msg', children="")
]),
html.Br(),
html.Br(),
html.Br(),
html.Div(style={'marginLeft': '250px', 'marginBottom': '40px'}, children=[
dcc.RangeSlider(
id='yearslider',
min=1970,
max=2018,
value=[1970, 2018],
marks={str(i): str(i) for i in range(1970, 2019, 5)},
allowCross=False,
)
]),
html.Br(),
]),
]),
]),
dcc.Tab(label='India Explorer', id='subtab4', value='subtab4', style=tab_style_sub, selected_style=tab_selected_style_sub, children=[
html.Div(style={'backgroundColor': '#FFFFFF'}, children=[
html.Br(),
html.H1(style={'textAlign': 'center', 'color': '#000000', 'textDecoration': 'underline', 'marginTop': '20px'}, children='Global Terrorism Database Explorer'),
html.Br(),
html.Div(style={'width': '80%'}, children=[
html.Div(style={'width': '100%', 'display': 'flex', 'textAlign': 'center', 'marginLeft': '350px'}, children=[
html.Label('Incidents per Year Grouped by ', style={'color' : ' #000000', 'fontSize': '30px', 'textAlign': 'center', 'float': 'left'}),
dcc.Dropdown(
id='filter2',
options=[{'label': j, 'value': i} for i, j in enumerate(filter_india_options)],
value=5,
clearable=False,
style={'width': '250px', 'marginLeft': '10px', 'marginTop': '3px'}
),
]),
html.Br(),
html.Br(),
dcc.Graph(id='plots4', figure=fig51, style={'width': 1370, 'height': 600}),
html.Br(),
html.Br(),
html.Div(style={'width': '40%', 'display': 'inline-block', 'textAlign': 'center', 'marginLeft': '450px'}, children=[
dcc.Input(id='searchtext2', style={'color' : '#000000', 'textAlign': 'center', 'width': '300px'}, placeholder='Enter filter text.'),
html.Div(id='msg2', children="")
]),
html.Br(),
html.Br(),
html.Br(),
html.Div(style={'marginLeft': '250px', 'marginBottom': '40px'}, children=[
dcc.RangeSlider(
id='yearslider2',
min=1970,
max=2018,
value=[1970, 2018],
marks={str(i): str(i) for i in range(1970, 2019, 5)},
allowCross=False,
)
]),
html.Br(),
]),
]),
]),
]),
]),
]),
])
return app