File size: 7,287 Bytes
542285e
23097b5
 
 
 
 
 
 
 
 
 
542285e
 
23097b5
 
 
 
 
 
 
 
 
 
480c4d5
23097b5
480c4d5
68fe85e
 
 
 
 
 
23097b5
 
480c4d5
 
 
 
 
 
 
430ab8a
23097b5
b016277
480c4d5
 
 
 
 
 
 
23097b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
480c4d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23097b5
 
 
 
 
 
480c4d5
 
 
23097b5
480c4d5
23097b5
 
 
 
7758b50
23097b5
 
 
 
 
542285e
23097b5
 
 
542285e
23097b5
 
 
 
542285e
23097b5
 
9a0b27c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23097b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import dash
from dash import Dash, html, dcc, callback, Output, Input
import plotly.express as px
from app import app
import pandas as pd

import datetime
import requests
from io import StringIO
from datetime import date

# from jupyter_dash import JupyterDash
# from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
import plotly.express as px

server = app.server

url='https://drive.google.com/file/d/1NaXOYHQFF5UO5rQr4rn8Lr3bkYMSOq4_/view?usp=sharing'
url='https://drive.google.com/uc?id=' + url.split('/')[-2]

# reading of file
df = pd.read_csv(url)

df['date'] = pd.to_datetime(df['date'])

unique_domains = df['domain_folder_name'].unique()
print(unique_domains)

unique_topics = df['Topic'].unique()
print(unique_topics)

df = df.rename(columns={df.columns[4]: "Veículos de notícias"})

df['FinBERT_label'] = df['FinBERT_label'].astype(str)
df['FinBERT_label'].replace({
    '3.0': 'positive',
    '2.0': 'neutral',
    '1.0': 'negative'
}, inplace=True)



counts = df.groupby(['date', 'Topic', 'Veículos de notícias', 'FinBERT_label']).size().reset_index(name='count')
counts['count'] = counts['count'].astype('float64')
counts['rolling_mean_counts'] = counts['count'].rolling(window=30, min_periods=2).mean()
df_pos = counts[[x in ['positive'] for x in counts.FinBERT_label]]
df_neu = counts[[x in ['neutral'] for x in counts.FinBERT_label]]
df_neg = counts[[x in ['negative'] for x in counts.FinBERT_label]]



app.layout = dbc.Container(
[   dbc.Row([ # row 1
    dbc.Col([html.H1('Evolução temporal de sentimento em títulos de notícias')],
    className="text-center mt-3 mb-1")
]
),
    dbc.Row([ # row 2
        dbc.Label("Selecione um período (mm/dd/aaaa):", className="fw-bold")
]),

dbc.Row([ # row 3
        dcc.DatePickerRange(
            id='date-range',
            min_date_allowed=df['date'].min().date(),
            max_date_allowed=df['date'].max().date(),
            initial_visible_month=df['date'].min().date(),
            start_date=df['date'].min().date(),
            end_date=df['date'].max().date()
        )
]),

    dbc.Row([ # row 4
        dbc.Label("Escolha um tópico:", className="fw-bold")
]),

dbc.Row([ # row 5
        dbc.Col(
            dcc.Dropdown(
                id="topic-selector",
                options=[
                    {"label": topic, "value": topic} for topic in unique_topics
                ],
                value="Imigrantes",  # Set the initial value
                style={"width": "50%"})


        )
]),

dbc.Row([ # row 6
        dbc.Col(dcc.Graph(id='line-graph-1'),
                )
]),

 dbc.Row([ # row 7
        dbc.Label("Escolha um site de notícias:", className="fw-bold")
]),

dbc.Row([ # row 8
        dbc.Col(
            dcc.Dropdown(
                id="domain-selector",
                options=[
                    {"label": domain, "value": domain} for domain in unique_domains
                ],
                value="expresso-pt",  # Set the initial value
                style={"width": "50%"})


        )
]),

dbc.Row([ # row 9
        dbc.Col(dcc.Graph(id='line-graph-2'),
                )
]),

dbc.Row([ # row 10
        dbc.Col(dcc.Graph(id='line-graph-3'),
                )
]),

dbc.Row([ # row 11
        dbc.Col(dcc.Graph(id='line-graph-4'),
                )
])

])

# callback decorator
@app.callback(
    Output('line-graph-1', 'figure'),
    Output('line-graph-2', 'figure'),
    Output('line-graph-3', 'figure'),
    Output('line-graph-4', 'figure'),
    Input("topic-selector", "value"),
    Input ("domain-selector", "value"),
    Input('date-range', 'start_date'),
    Input('date-range', 'end_date')
)
# callback function
def update_output(selected_topic, selected_domain, start_date, end_date):
    # filter dataframes based on updated data range
    mask_1 = ((df["Topic"] == selected_topic) & (df['date'] >= start_date) & (df['date'] <= end_date))
    df_filtered = df.loc[mask_1]


    #create line graphs based on filtered dataframes
    line_fig_1 = px.line(df_filtered, x="date", y="normalised results",
                     color='Veículos de notícias', title="O gráfico mostra a evolução temporal de sentimento dos títulos de notícias. Numa escala de -1 (negativo) a 1 (positivo), sendo 0 (neutro).")

    #set x-axis title and y-axis title in line graphs
    line_fig_1.update_layout(
                   xaxis_title='Data',
                   yaxis_title='Classificação de Sentimento')

    #set label format on y-axis in line graphs
    line_fig_1.update_xaxes(tickformat="%b %d<br>%Y")

    # filter dataframes based on updated data range
    mask_2 = ((df_pos["Topic"] == selected_topic) & (df_pos["domain_folder_name"] == selected_domain) & (df_pos['date'] >= start_date) & (df_pos['date'] <= end_date))
    mask_3 = ((df_neu["Topic"] == selected_topic) & (df_neu["domain_folder_name"] == selected_domain) & (df_neu['date'] >= start_date) & (df_neu['date'] <= end_date))
    mask_4 = ((df_neg["Topic"] == selected_topic) & (df_neg["domain_folder_name"] == selected_domain) & (df_neg['date'] >= start_date) & (df_neg['date'] <= end_date))
    df2_filtered = df_pos.loc[mask_2]
    df3_filtered = df_neu.loc[mask_3]
    df4_filtered = df_neg.loc[mask_4]

#create line graphs based on filtered dataframes
    line_fig_2 = px.line(df2_filtered, x="date", y="rolling_mean_counts", line_group="FinBERT_label",
                     title="Positive")
    line_fig_3 = px.line(df3_filtered, x="date", y="rolling_mean_counts", line_group="FinBERT_label",
                     title="Neutral")
    line_fig_4 = px.line(df4_filtered, x="date", y="rolling_mean_counts", line_group="FinBERT_label",
                     title="Negative")

#set x-axis title and y-axis title in line graphs
    line_fig_2.update_layout(
                   xaxis_title='Data',
                   yaxis_title='Número de notícias com sentimento positivo')
    line_fig_3.update_layout(
                   xaxis_title='Data',
                   yaxis_title='Número de notícias com sentimento neutro')
    line_fig_4.update_layout(
                   xaxis_title='Data',
                   yaxis_title='Número de notícias com sentimento negativo')

#set label format on y-axis in line graphs
    line_fig_2.update_xaxes(tickformat="%b %d<br>%Y")
    line_fig_3.update_xaxes(tickformat="%b %d<br>%Y")
    line_fig_4.update_xaxes(tickformat="%b %d<br>%Y")

#set label format on y-axis in line graphs
    line_fig_2.update_traces(line_color='#1E88E5')
    line_fig_3.update_traces(line_color='#004D40')
    line_fig_4.update_traces(line_color='#D81B60')

    return line_fig_1, line_fig_2, line_fig_3, line_fig_4

    # return line_fig_1
    


# df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder_unfiltered.csv')



# app.layout = html.Div([
#     html.H1(children='Title of Dash App', style={'textAlign':'center'}),
#     dcc.Dropdown(df.country.unique(), 'Canada', id='dropdown-selection'),
#     dcc.Graph(id='graph-content')
# ])

# @callback(
#     Output('graph-content', 'figure'),
#     Input('dropdown-selection', 'value')
# )
# def update_graph(value):
#     dff = df[df.country==value]
#     return px.line(dff, x='year', y='pop')


if __name__ == '__main__':
    app.run_server(debug=True)