-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
1178 lines (1017 loc) · 42.9 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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import re
import time
import datetime
# import dash_bootstrap_components as dbc
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
import pandas as pd
import plotly.express as px
from dash.dependencies import Input, Output, State
from utils.formatting import create_event_array
from utils.formatting import format_df_ma_stats, format_df_ma_sent, format_df_ma_tweet_vol, format_df_corr, \
format_df_notable_days, format_df_ma_sent_comp
from utils.plotting import plot_dropdown_sent_vs_vol, plot_covid_stats, plot_hashtag_table, \
plot_sentiment, plot_corr_mat, plot_sentiment_bar, plot_emoji_bar_chart, emoji_to_colour, \
plot_notable_days, plot_sentiment_comp
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
app.title = 'Sentiment Towards COVID-19 in the UK'
app.config.suppress_callback_exceptions = True
# READ DATA
df_covid_stats = pd.read_csv(
'data/COVID-Dataset/uk_covid_stats.csv', skipinitialspace=True)
uk_counties = json.load(open('data/Geojson/uk_counties_simpler.json', 'r'))
r_numbers = pd.read_csv('data/COVID-Dataset/r_numbers.csv')
df_events = pd.read_csv('data/events/key_events.csv',
skipinitialspace=True, usecols=['Date', 'Event'])
counties = pd.read_csv(
'data/Geojson/uk-district-list-all.csv')['county'].tolist()
hashtags_covid = pd.read_csv('data/covid/top_ten_hashtags_per_day.csv')
hashtags_lockdown = pd.read_csv('data/lockdown/top_ten_hashtags_per_day.csv')
geo_df_covid = pd.read_csv(
'data/covid/daily_sentiment_county_updated_locations.csv')
geo_df_lockdown = pd.read_csv(
'data/lockdown/daily_sentiment_county_updated_locations.csv')
tweet_count_covid = pd.read_csv('data/covid/daily_tweet_count_country.csv')
tweet_count_lockdown = pd.read_csv(
'data/lockdown/daily_tweet_count_country.csv')
all_sentiments_covid = pd.read_csv('data/covid/all_tweet_sentiments.csv')
all_sentiments_lockdown = pd.read_csv('data/lockdown/all_tweet_sentiments.csv')
notable_days_covid = pd.read_csv('data/covid/notable_days_months.csv')
notable_days_lockdown = pd.read_csv('data/lockdown/notable_days_months.csv')
scatter_covid = pd.read_csv('data/covid/scatter.csv')
scatter_lockdown = pd.read_csv('data/lockdown/scatter.csv')
emojis_covid = pd.read_csv('data/covid/weekly_emojis_with_colours.csv')
emojis_lockdown = pd.read_csv('data/lockdown/weekly_emojis_with_colours.csv')
news_df = pd.read_csv('data/events/news_timeline.csv')
countries = ['England', 'Scotland', 'Northern Ireland', 'Wales']
# Data Sources
emoji_wordcloud_urls = {'covid': 'covid_emoji_wordcloud.png',
'lockdown': 'lockdown_emoji_wordcloud.png'}
wordcloud_urls = {'covid': 'covid_wordcloud.png',
'lockdown': 'lockdown_wordcloud.png'}
hashtag_data_sources = {'covid': hashtags_covid,
'lockdown': hashtags_lockdown}
geo_df_data_sources = {'covid': geo_df_covid,
'lockdown': geo_df_lockdown}
complete_data_sources = {'covid': all_sentiments_covid,
'lockdown': all_sentiments_lockdown}
sentiment_dropdown_value_to_avg_score = {'nn': 'nn-score_avg', 'textblob': 'textblob-score_avg',
'vader': 'vader-score_avg', 'native': 'native-score_avg'}
sentiment_dropdown_value_to_score = {'nn': 'nn-score', 'textblob': 'textblob-score',
'vader': 'vader-score', 'native': 'native-score'}
sentiment_dropdown_value_to_predictions = {'nn': 'nn-predictions', 'textblob': 'textblob-predictions',
'vader': 'vader-predictions', 'native': 'native-predictions'}
tweet_counts_sources = {'covid': tweet_count_covid,
'lockdown': tweet_count_lockdown}
regions_lists = {'county': counties, 'country': countries}
notable_days_sources = {'covid': notable_days_covid,
'lockdown': notable_days_lockdown}
scatter_sources = {'covid': scatter_covid, 'lockdown': scatter_lockdown}
# Formatted
formatted_tweet_count = {'covid': format_df_ma_tweet_vol(tweet_count_covid, countries),
'lockdown': format_df_ma_tweet_vol(tweet_count_lockdown, countries)}
formatted_tweet_sent = {'covid': format_df_ma_sent(
geo_df_covid), 'lockdown': format_df_ma_sent(geo_df_lockdown)}
formatted_covid_stats = format_df_ma_stats(df_covid_stats, countries)
formatted_sent_comp = {'covid': format_df_ma_sent_comp(all_sentiments_covid),
'lockdown': format_df_ma_sent_comp(all_sentiments_lockdown)}
emojis_weekly_source = {'covid': emojis_covid, 'lockdown': emojis_lockdown}
# Dates
weeks = r_numbers['date'].tolist()
week_pairs = [(weeks[i], weeks[i + 1]) for i in range(0, len(weeks) - 1)]
start_global = '2020-03-20'
end_global = '2021-03-25'
dates_list = pd.date_range(start=start_global, end=end_global)
#
events_array = create_event_array(df_events, start_global, end_global)
# Initial map
covid_geo_df = geo_df_data_sources['covid'].loc[geo_df_data_sources['covid']
['date'] == start_global]
fig_0 = px.choropleth_mapbox(
covid_geo_df,
locations="id",
featureidkey='properties.id',
geojson=uk_counties,
color=sentiment_dropdown_value_to_avg_score['nn'],
hover_name='county',
mapbox_style='white-bg',
color_continuous_scale=px.colors.diverging.Temps_r,
zoom=3.5,
center={"lat": 55, "lon": 0},
animation_frame='date',
range_color=[-1, 1],
)
emoji_covid_fig = plot_emoji_bar_chart(emojis_covid, start_global)
def check_between_dates(start, end, current):
start, end, current = pd.to_datetime(start, format='%d/%m/%Y'), \
pd.to_datetime(end, format='%d/%m/%Y'), \
pd.to_datetime(current, format='%Y-%m-%d')
return start < current <= end
def indicator(color, text, id_value):
return html.Div(
[
html.P(
children=text,
className="info_text"
),
html.H4(
id=id_value,
className="indicator_value"),
],
className="pretty_container three columns",
)
def covid_stats_indicators():
return html.Div(
[
indicator("#00cc96", "Total Deaths", "total_deaths_indicator"),
indicator(
"#119DFF", "Total Cases", "total_cases_indicator"
),
indicator("#EF553B", "R-Number", "r_number_indicator"),
indicator(None, 'Current Date', 'current_date_indicator')
],
className="twelve columns"
)
def filters():
return html.Div(
[
html.Div(children=[
html.P(id='df-selected', children='Select Tweet Data-set '),
dcc.Dropdown(
id="source-dropdown",
options=[
{"label": "COVID", "value": "covid"},
{"label": "Lockdown", "value": "lockdown"}
],
value="covid",
clearable=False,
)],
className="pretty_container three columns",
),
html.Div(children=[
html.P(id='nlp-selected', children='Select NLP Technique'),
dcc.Dropdown(
id="nlp-dropdown",
options=[
{"label": "Vader", "value": "vader"},
{"label": "Text Blob", "value": "textblob"},
{"label": "LSTM", "value": "nn"},
{"label": 'Naive Bayes', "value": 'native'}
],
value="vader",
clearable=False,
)],
className="pretty_container three columns",
)
],
className="eight columns",
style={"margin-bottom": "10px"},
)
# the style arguments for the sidebar. We use position:fixed and a fixed width
SIDEBAR_STYLE = {
"position": "fixed",
"top": 62.5,
"left": 0,
"bottom": 0,
"width": "16rem",
"height": "40%",
"z-index": 1,
"overflow-x": "hidden",
"transition": "all 0.5s",
"padding": "0.5rem 1rem",
"background-color": "#f8f9fa",
}
CONTENT_STYLE = {
"margin-left": "18rem",
"margin-right": "2rem",
"padding": "2rem 1rem",
}
sidebar = html.Div(
[
html.H2("Sidebar", className="display-4"),
html.Hr(),
dbc.Nav(
[
dbc.NavLink("Home\n\n", href="/", active="exact"),
html.Br(),
html.Br(),
dbc.NavLink("Timeline Exploration,\n\n", href="/page-1", active="exact"),
html.Br(),
html.Br(),
dbc.NavLink("Data Analysis,\n\n", href="/page-2", active="exact"),
],
vertical='md',
pills=True,
),
],
style=SIDEBAR_STYLE,
)
content = html.Div(id="page-content", style=CONTENT_STYLE)
app.layout = html.Div([dcc.Location(id="url"), sidebar, content])
# App Layout
layout_main = [
html.Div(
id="app-container",
children=[dcc.Interval(
id="interval-component",
interval=2 * 1000, # in milliseconds
n_intervals=0, # start at batch 50
disabled=True,
),
dcc.Loading(html.Div(id='scatter-loading'),
loading_state={
'component_name': 'app-container', 'is_loading': True},
fullscreen=True, type='graph'),
html.Div(
children=[
html.Div(
id="analysis-header",
children=[
html.H2(
children='Timeline Exploration'
),
],
className='pretty_container twelve columns'
)
],
className='row'
),
html.Div(children=[
filters(),
covid_stats_indicators()],
className='row'),
html.Div(children=[
html.Div(
id='button-container',
children=[
html.Button(
'Previous Date', id='prev-button', n_clicks=0
),
html.Button(
'Next Date', id='next-button', n_clicks=0
),
html.Button(
'Play', id='play-button', n_clicks=0
)
]
)
],
className='row'),
html.Div(children=[
html.Div(
id="slider-container",
children=[
html.P(
id="slider-text",
children="Drag the slider to change the date:",
),
dcc.Slider(
id="days-slider",
min=0,
max=364,
value=0,
marks={
str(x): {
"label": dates_list[x].date()
}
for x in range(0, 364, 30)
},
),
],
className='pretty_container twelve columns'
)],
className='row'),
html.Div(
id='toggle-container',
children=[
html.Div(
dcc.Checklist(
id='heatmap-container-toggle',
options=[
{'label': 'Sentiment Heatmap',
'value': 'on'},
],
value=['on'],
),
style={'padding': '5px', 'display': 'inline-block'},
),
html.Div(
dcc.Checklist(
id='bar_chart_div-toggle',
options=[
{'label': 'Sentiment and Emoji Bar Charts',
'value': 'on'},
],
value=['on'],
),
style={'padding': '5px', 'display': 'inline-block'},
),
html.Div(
dcc.Checklist(
id='news-hashtag-div-toggle',
options=[
{'label': 'News and Hashtags',
'value': 'on'},
],
value=['on'],
),
style={'display': 'inline-block'},
),
html.Div(
dcc.Checklist(
id='deaths-and-cases-div-toggle',
options=[
{'label': 'Deaths and Cases',
'value': 'on'},
],
value=['on'],
),
style={'padding': '5px', 'display': 'inline-block'},
),
html.Div(
dcc.Checklist(
id='7-day-MA-div-toggle',
options=[
{'label': '7 Day Moving Average',
'value': 'on'},
],
value=['on'],
),
style={'padding': '5px', 'display': 'inline-block'},
),
],
className='pretty_container'
),
html.Div(children=[
html.Div(
id="left-column",
children=[
html.Div(
id="heatmap-container",
children=[
html.H6(
"Heatmap of sentiment towards COVID-19 in the UK on day: ",
id="heatmap-title",
),
dcc.Graph(
id='county-choropleth',
figure=fig_0
),
],
),
],
className='pretty_container five columns'
),
html.Div(
id='bar_chart_div',
children=[
html.Div(
id="bar-chart-container",
children=[
html.Div(children=[
html.H6(
children='Sentiment Count Per Day'
),
dcc.Checklist(
id='sentiment_bar_chart_toggle',
options=[
{'label': 'Show/Hide',
'value': 'on'},
],
value=['on']
),
dcc.Graph(
id='sentiment_bar_chart'
),
html.H6(
"Top Weekly Emojis",
),
dcc.Checklist(
id='emoji-bar-chart-toggle',
options=[
{'label': 'Show/Hide',
'value': 'on'},
],
value=['on']
),
dcc.Graph(
id='emoji-bar-chart',
figure=emoji_covid_fig
)
],
className='pretty_container twelve columns'
)
],
className='row'
),
],
className='pretty_container four columns'
),
html.Div(
id='news-hashtag-div',
children=[
html.Div(
children=[
html.Div(children=[
html.H6(
"Top News Stories",
),
html.H6(
style={
"color": "#2a3f5f",
"fontSize": "13px",
"textAlign": "center",
"marginBottom": "50",
},
),
dcc.Checklist(
id='daily-news-toggle',
options=[
{'label': 'Show/Hide',
'value': 'on'},
],
value=['on']
),
dcc.Markdown(
id="daily-news",
style={
"padding": "10px 13px 5px 13px", "marginBottom": "5"},
),
html.H6(
"Top 10 Hashtags",
),
dcc.Checklist(
id='hashtag-table-toggle',
options=[
{'label': 'Show/Hide',
'value': 'on'},
],
value=['on']
),
dcc.Graph(
id='hashtag-table'
),
],
className='pretty_container twelve columns'
),
],
className='row'
)
],
className="pretty_container four columns",
),
],
className='row'
),
html.Div(
id='covid-stats',
children=[
html.Div(
id="covid-stats-container",
children=[
html.H6(
"Deaths and Cases Over Time",
id="stats-title",
),
dcc.Graph(
id='stats-graph'
),
],
className='pretty_container six columns'
),
html.Div(
id="ma-sent-container",
children=[
html.H6(
"7 Day Moving Average of Covid Sentiment For Each Country\n(Starts at 2020-03-27)",
id="sent-vol-title",
),
dcc.Graph(
id='ma-sent-graph'
),
],
className='pretty_container six columns'
)
],
className='row',
style={'height': '850px'}
),
]
)
]
layout_analysis = [
dcc.Loading(html.Div(id='scatter-loading'),
loading_state={
'component_name': 'app-container', 'is_loading': True},
fullscreen=True, type='graph'),
html.Div(
children=[
html.Div(
id="analysis-header",
children=[
html.H2(
children='Data Analysis and Exploration'
),
],
className='pretty_container twelve columns'
)
],
className='row'
),
html.Div(children=[
filters(),
],
className='row'),
html.Div(children=[
html.H6(
"Notable Days",
),
dcc.Graph(
id='notable-day-table'
),
],
className='pretty_container four columns'
),
html.Div(children=[
html.Div(
id="graph-container",
children=[
html.P(id="chart-selector",
children="Select Chart:"),
dcc.Dropdown(
options=[
{
"label": "7 Day MA COVID Sentiment vs Tweet Volume (Starts at 2020-03-27)",
"value": "show_sentiment_vs_time",
},
{
"label": "Comparison of Sentiment Generation Techniques (Starts at 2020-03-27)",
"value": "show_sentiment_comparison",
},
],
value="show_sentiment_comparison",
id="chart-dropdown",
),
dcc.Graph(
id='dropdown-figure'
),
],
className='pretty_container twelve columns'
),
],
className='row'
),
html.Div(
children=[
html.Div(
id='emoji-wordcloud-div',
children=[
html.H4(
children="Popular Emoji's"
),
html.Img(
id='emoji-wordcloud',
src=app.get_asset_url(
'covid_emoji_wordcloud.png'),
style={
'height': '200px',
'width': '90%'
}),
html.H4(
children="Popular Words's(Excluding Keywords)"
),
html.Img(
id='wordcloud',
src=app.get_asset_url(
'covid_wordcloud.png'),
style={
'height': '200px',
'width': '90%'
})
],
className='pretty_container four columns'
),
html.Div(
children=[
html.H4(
children=[
'Correlation Between Features(Scaled Volume, Deaths, Cases)',
dcc.Graph(
id='corr-mat'
),
]
)
],
className='pretty_container eight columns'
)
],
className='row'
)]
layout_faq = [
html.Div(children=[
dcc.Loading(html.Div(id='scatter-loading'),
loading_state={
'component_name': 'app-container', 'is_loading': True},
fullscreen=True, type='graph'),
html.Div(
children=[
html.Div(
children=[
html.H4(
"About this Dashboard",
),
dcc.Markdown('''
The Coronavirus (COVID-19) pandemic has been at the forefront
of the global population’s concerns over the last year with the consequence
having a large impact on individuals daily lives. Twitter is one of the largest
social media platforms where people from all over the country(and the world)
express their opinions regarding a wide range of topics, and it is of
no surprise that there has been a surge of discussion around
pandemic and pandemic adjacent topics.
The focus of this analysis is on ’tweet’s posted within the United
Kingdom with the aim to measure the pulse of general sentiment
towards COVID and lockdown over the past year. A multitude of sentiment analysis
techniques are applied to the data to measure sentiment change over time.
This dashboard provides insight and visualises the change in language expression over a year.
Specifically, exploring reactions to relevant events combined with COVID statistics
within the UK. Ultimately, with the aim to make exploration and analysis of our
data easily accessible to a nontechnical audience.
The end-user will be able to compare the results of different sentiment techniques and how public opinion compares between
countries/regions in relation to the number of deaths, the number of cases, and certain pandemic related news events over the last year.
**Please use the sidebar to navigate to the rest of the dashboard!**
Have fun :)!
''')
],
),
],
className='pretty_container five columns'
),
html.Div(
id='bar_chart_div',
children=[
html.Div(
id="bar-chart-container",
children=[
html.Div(children=[
html.H4(
children='Data Collected'
),
dcc.Markdown(
'''
This dashboard contains two datasets corresponding to COVID and lockdown
related Tweets posted within the United Kingdom.
Each dataset contains approximately 300 thousand tweets, starting from
the 20 March 2020 to the 25 March 2021.
The keywords used to scrape the COVID dataset include:
"coronavirus OR covid OR covid19 OR covid-19"
The keyword used to scrape the lockdown dataset include:
"lockdown"
We used [SNScrape](https://github.com/JustAnotherArchivist/snscrape/tree/master/snscrape)
, an open source wrapper of the Twitter API to collect historical
Tweets within the UK containing our key words over the past year.
See Twitter's Developer Policy regarding API and content usage
[here](https://developer.twitter.com/en/developer-terms/agreement-and-policy).
The news events were collected from the [Guardian News API](https://open-platform.theguardian.com/)
The relavant COVID statistics were collected from the [Public Health England](https://coronavirus.data.gov.uk/)
'''
),
html.H4(
"Sentiment Techniques",
),
dcc.Markdown(
'''
This dashboard analyses and compares sentiment of tweet content using four
sentiment generating techniques:
[Vader](https://github.com/cjhutto/vaderSentiment)
[Naive Bayes](https://web.stanford.edu/~jurafsky/slp3/4.pdf)
[LSTM](https://www.aclweb.org/anthology/O18-1021.pdf)
[Textblob](https://github.com/sloria/TextBlob)
'''
),
],
className='pretty_container twelve columns'
)
],
className='row'
),
],
className='pretty_container five columns'
),
html.Div(
id='news-hashtag-div',
children=[
html.Div(
children=[
html.Div(children=[
html.H4(
"About the Creators",
),
dcc.Markdown('''
This was a project by a group of Undergraduates at the University of Bristol
composed of 2 Engineering Maths and 3 Computer Science Students.
Specifically, this was for the Applied Data Science unit.
''')
],
className='pretty_container twelve columns'
),
],
className='row'
)
],
className="pretty_container three columns",
),
],
className='row'
),
]
@app.callback(Output('r_number_indicator', 'children'), [Input("days-slider", "value")])
def update_r_text(date_index):
selected_date = str(dates_list[date_index].date())
for i, (start, end) in enumerate(week_pairs):
if check_between_dates(start, end, selected_date):
df = r_numbers.loc[r_numbers['date'] == start]
avg_r = round(float((df['upper'] + df['lower']) / 2), 2)
if avg_r == 0:
return 'N/A'
return "~{}".format(avg_r)
return 'N/A'
@app.callback(Output('total_cases_indicator', 'children'), [Input("days-slider", "value")])
def update_cases_text(date_index):
selected_date = str(dates_list[date_index].date())
cases = df_covid_stats.loc[df_covid_stats['date']
== selected_date, 'cumCasesByPublishDate'].sum()
return cases
@app.callback(Output('total_deaths_indicator', 'children'), [Input("days-slider", "value")])
def update_deaths_text(date_index):
selected_date = str(dates_list[date_index].date())
deaths = df_covid_stats.loc[df_covid_stats['date']
== selected_date, 'cumDeathsByDeathDate'].sum()
return deaths
@app.callback(Output("current_date_indicator", "children"), [Input("days-slider", "value")])
def update_date_box(selected_date):
return dates_list[selected_date].date()
@app.callback(Output("heatmap-title", "children"), [Input("days-slider", "value"), Input('source-dropdown', 'value')])
def update_map_title(selected_date, source):
return "Heatmap of Sentiment Within {} Related Tweets in the UK. Date: {}".format(source,
dates_list[
selected_date].date()
)
@app.callback(
Output('sentiment_bar_chart', 'figure'),
[Input("days-slider", "value"), Input('source-dropdown',
'value'), Input('nlp-dropdown', 'value')]
)
def update_bar_chart(selected_date, source, nlp):
data = complete_data_sources[source]
data['date'] = pd.to_datetime(data['date']).dt.date
df = data[data['date'] == dates_list[selected_date]]
label = sentiment_dropdown_value_to_predictions[nlp]
return plot_sentiment_bar(df, label, countries)
#
@app.callback(
Output('hashtag-table', 'figure'),
[Input("days-slider", "value"), Input('source-dropdown', 'value')]
)
def update_hashtag_table(selected_date, source):
selected_date = str(dates_list[selected_date].date())
hashtags_df = hashtag_data_sources[source]
hashtag_date = hashtags_df.loc[hashtags_df['date'] == selected_date]
hashtags = [tuple(x.split(',')) for x in re.findall(
"\((.*?)\)", hashtag_date['top_ten_hashtags'].values[0])]
hash_dict = {'Hashtag': [], 'Count': []}
for hashtag, count in hashtags:
hash_dict['Hashtag'].append('#' + hashtag.replace("'", ''))
hash_dict['Count'].append(count)
hash_df = pd.DataFrame(hash_dict)
return plot_hashtag_table(hash_df)
@app.callback(
[Output('interval-component', 'n_intervals'), Output('interval-component', 'disabled'),
Output('play-button', 'children')],
[Input('next-button', 'n_clicks'), Input('prev-button', 'n_clicks'), Input('play-button', 'n_clicks'),
State('days-slider', 'value'), State('interval-component', 'disabled'), State('play-button', 'children')]
)
def button_pressed(inc_btn, dec_btn, play_btn, day, disabled, play_text):
changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
if 'next-button' in changed_id and disabled:
if day < 364:
return day, disabled, play_text
if 'prev-button' in changed_id and disabled:
if day > 0:
return day - 2, disabled, play_text
if 'play-button' in changed_id:
if disabled:
return day, not disabled, 'stop'
else:
return day, True, 'start'
return day - 1, True, play_text
@app.callback(Output("days-slider", "value"),
[Input('interval-component', 'n_intervals')])
def update_interval(current_interval):
if current_interval < 364:
return current_interval + 1
@app.callback(
Output("county-choropleth", "figure"),
[Input("days-slider", "value"), Input("nlp-dropdown", "value"),
Input("source-dropdown", "value")
]
)
def display_map(day, nlp, topic):
geo_df = geo_df_data_sources[topic]
color = sentiment_dropdown_value_to_avg_score[nlp]
# Initial map
date = str(dates_list[day].date())
geo_df = geo_df.loc[geo_df['date'] == date]
fig = px.choropleth_mapbox(
geo_df,
locations="id",
featureidkey='properties.id',
geojson=uk_counties,
color=color,
hover_name='county',
mapbox_style='white-bg',
color_continuous_scale=px.colors.diverging.Temps_r,
zoom=3.5,
center={"lat": 55, "lon": 0},
animation_frame='date',
range_color=[-1, 1],
)
fig.update_layout(autosize=True, height=900)
return fig
@app.callback(
Output("stats-graph", "figure"),
[Input("days-slider", "value")
]
)
def display_stats(day):
today = str(dates_list[day].date())
return plot_covid_stats(formatted_covid_stats, countries, events_array, start_global, today)
@app.callback(
Output('ma-sent-graph', 'figure'),
[Input("days-slider", "value"), Input('source-dropdown',
'value'), Input('nlp-dropdown', 'value')]
)
def display_sentiments(day, topic, sentiment_type):
selected_date = str(dates_list[day].date())
sentiment_col = sentiment_dropdown_value_to_avg_score[sentiment_type]
tweet_sent_df = formatted_tweet_sent[topic]
# tweet_sent_df = formatted_scaled_sent[topic]
return plot_sentiment(tweet_sent_df, sentiment_col, start_global, selected_date)
@app.callback(
Output("daily-news", "children"),
[Input("days-slider", "value")],
)
def display_news(day):
date = str(dates_list[day].date())
df = news_df.loc[news_df['Date'] == date]
# news_fig = ff.create_table(news_df.drop('Date', 1))
# return df_to_table(news_df)
links = ''
for ind in df.index:
headline = news_df['Headline'][ind]
URL = df['URL'][ind]
link = '[**' + headline + '**](' + URL + ') '
blank = '''