Spaces:
Sleeping
Sleeping
Create index.py
Browse files
index.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dash import Dash, html, dcc, callback, Output, Input
|
2 |
+
import plotly.express as px
|
3 |
+
from app import app
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
import datetime
|
7 |
+
import requests
|
8 |
+
from io import StringIO
|
9 |
+
from datetime import date
|
10 |
+
|
11 |
+
import dash
|
12 |
+
from jupyter_dash import JupyterDash
|
13 |
+
from dash.dependencies import Input, Output
|
14 |
+
import dash_bootstrap_components as dbc
|
15 |
+
import plotly.express as px
|
16 |
+
|
17 |
+
server = app.server
|
18 |
+
|
19 |
+
url='https://drive.google.com/file/d/1NaXOYHQFF5UO5rQr4rn8Lr3bkYMSOq4_/view?usp=sharing'
|
20 |
+
url='https://drive.google.com/uc?id=' + url.split('/')[-2]
|
21 |
+
|
22 |
+
# reading of file
|
23 |
+
df = pd.read_csv(url)
|
24 |
+
df['FinBERT_label'] = df['FinBERT_label'].astype(str)
|
25 |
+
df['date'] = pd.to_datetime(df['date'])
|
26 |
+
df = df.rename(columns={df.columns[4]: "Veículos de notícias"})
|
27 |
+
|
28 |
+
unique_topics = df['Topic'].unique()
|
29 |
+
print(unique_topics)
|
30 |
+
|
31 |
+
|
32 |
+
app.layout = dbc.Container(
|
33 |
+
[ dbc.Row([ # row 1
|
34 |
+
dbc.Col([html.H1('Evolução temporal de sentimento em títulos de notícias')],
|
35 |
+
className="text-center mt-3 mb-1")
|
36 |
+
]
|
37 |
+
),
|
38 |
+
dbc.Row([ # row 2
|
39 |
+
dbc.Label("Selecione um período (mm/dd/aaaa):", className="fw-bold")
|
40 |
+
]),
|
41 |
+
|
42 |
+
dbc.Row([ # row 3
|
43 |
+
dcc.DatePickerRange(
|
44 |
+
id='date-range',
|
45 |
+
min_date_allowed=df['date'].min().date(),
|
46 |
+
max_date_allowed=df['date'].max().date(),
|
47 |
+
initial_visible_month=df['date'].min().date(),
|
48 |
+
start_date=df['date'].min().date(),
|
49 |
+
end_date=df['date'].max().date()
|
50 |
+
)
|
51 |
+
]),
|
52 |
+
|
53 |
+
dbc.Row([ # row 4
|
54 |
+
dbc.Label("Escolha um tópico:", className="fw-bold")
|
55 |
+
]),
|
56 |
+
|
57 |
+
dbc.Row([ # row 5
|
58 |
+
dbc.Col(
|
59 |
+
dcc.Dropdown(
|
60 |
+
id="topic-selector",
|
61 |
+
options=[
|
62 |
+
{"label": topic, "value": topic} for topic in unique_topics
|
63 |
+
],
|
64 |
+
value="Imigrantes", # Set the initial value
|
65 |
+
style={"width": "50%"})
|
66 |
+
|
67 |
+
|
68 |
+
)
|
69 |
+
]),
|
70 |
+
|
71 |
+
dbc.Row([ # row 6
|
72 |
+
dbc.Col(dcc.Graph(id='line-graph-1'),
|
73 |
+
)
|
74 |
+
]),
|
75 |
+
|
76 |
+
|
77 |
+
])
|
78 |
+
|
79 |
+
# callback decorator
|
80 |
+
@app.callback(
|
81 |
+
Output('line-graph-1', 'figure'),
|
82 |
+
Input("topic-selector", "value"),
|
83 |
+
Input('date-range', 'start_date'),
|
84 |
+
Input('date-range', 'end_date')
|
85 |
+
)
|
86 |
+
|
87 |
+
# callback function
|
88 |
+
def update_output(selected_topic, start_date, end_date):
|
89 |
+
# filter dataframes based on updated data range
|
90 |
+
mask_1 = ((df["Topic"] == selected_topic) & (df['date'] >= start_date) & (df['date'] <= end_date))
|
91 |
+
df_filtered = df.loc[mask_1]
|
92 |
+
|
93 |
+
|
94 |
+
#create line graphs based on filtered dataframes
|
95 |
+
line_fig_1 = px.line(df_filtered, x="date", y="normalised results",
|
96 |
+
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).")
|
97 |
+
|
98 |
+
#set x-axis title and y-axis title in line graphs
|
99 |
+
line_fig_1.update_layout(
|
100 |
+
xaxis_title='Data',
|
101 |
+
yaxis_title='Classificação de Sentimento')
|
102 |
+
|
103 |
+
#set label format on y-axis in line graphs
|
104 |
+
line_fig_1.update_xaxes(tickformat="%b %d<br>%Y")
|
105 |
+
|
106 |
+
return line_fig_1
|
107 |
+
|
108 |
+
|
109 |
+
# df['FinBERT_label'].replace({
|
110 |
+
# '3.0': 'positive',
|
111 |
+
# '2.0': 'neutral',
|
112 |
+
# '1.0': 'negative'
|
113 |
+
# }, inplace=True)
|
114 |
+
|
115 |
+
# df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder_unfiltered.csv')
|
116 |
+
|
117 |
+
|
118 |
+
|
119 |
+
# app.layout = html.Div([
|
120 |
+
# html.H1(children='Title of Dash App', style={'textAlign':'center'}),
|
121 |
+
# dcc.Dropdown(df.country.unique(), 'Canada', id='dropdown-selection'),
|
122 |
+
# dcc.Graph(id='graph-content')
|
123 |
+
# ])
|
124 |
+
|
125 |
+
# @callback(
|
126 |
+
# Output('graph-content', 'figure'),
|
127 |
+
# Input('dropdown-selection', 'value')
|
128 |
+
# )
|
129 |
+
# def update_graph(value):
|
130 |
+
# dff = df[df.country==value]
|
131 |
+
# return px.line(dff, x='year', y='pop')
|
132 |
+
|
133 |
+
|
134 |
+
if __name__ == '__main__':
|
135 |
+
app.run_server(debug=True)
|