-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_graphs.py
More file actions
162 lines (126 loc) · 4.56 KB
/
Copy pathplot_graphs.py
File metadata and controls
162 lines (126 loc) · 4.56 KB
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
import datetime
import json
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly
from dash.dependencies import Input, Output
from analysis import Analysis
from lib.tools import save_to_file
from lib.tools import load_from_file
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
streamer = None
# Init files
save_to_file({ 'count': 0 }, filename="number_tweets.json")
save_to_file({}, filename="most_retweeted.json")
save_to_file({}, filename="sources.json")
save_to_file({}, filename="countries.json")
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(
[
html.H1(children='Tweet-Analysis'),
html.Div([
dcc.Input(id='input-on-submit', type='text'),
html.Button('INICIAR', id='submit-val', n_clicks=0),
]),
html.Div(id='container-button-basic', children='Enter a value and press submit'),
html.H3(id='number_tweets', children=''),
html.Hr(),
html.H4(children='Análise de frequência de substantivos'),
dcc.Graph(id='live-graph', animate=False),
html.Hr(),
html.H4(children='Análise de frequência de fontes'),
dcc.Graph(id='live-graph-sources', animate=False),
dcc.Interval(
id='graph-update',
interval=1*1000, # in milliseconds
n_intervals=0
),
html.Hr(),
html.H4(children='Análise de Localização dos usuários'),
html.P(children='Análise das localizações que os usuários colocam em seus perfis'),
dcc.Graph(id='live-graph-countries', animate=False),
html.Hr(),
html.H4(children='Análise de Retweets'),
html.P(children='Conteúdo mais Retweetado desde o início da pesquisa'),
html.Div(id='most-rts')
]
)
# Callback do botão de enviar keyword
@app.callback(
dash.dependencies.Output('container-button-basic', 'children'),
[dash.dependencies.Input('submit-val', 'n_clicks')],
[dash.dependencies.State('input-on-submit', 'value')]
)
def update_output_div(n_clicks, input_value):
global streamer
if streamer is not None:
Analysis.stop(streamer)
streamer = Analysis.perform(input_value)
return 'Iniciado'
# Callback da label que mostra a qtd de tweets analisados
@app.callback(
Output('number_tweets', 'children'),
[Input('graph-update', 'n_intervals')]
)
def show_num_tweets(n):
data = load_from_file(filename="number_tweets.json")
return f'{data["count"]} tweets analisados'
# Callback do gráfico de frequência de termos (substantivos)
@app.callback(
Output('live-graph', 'figure'),
[Input('graph-update', 'n_intervals')]
)
def update_graph_tweets(n):
data = load_from_file()
data = {k: v for k, v in sorted(data.items(), key=lambda item: item[1], reverse=True)}
data = {'x': list(data.keys())[:15], 'y': list(data.values())[:15], 'type': 'bar', 'name': 'SF'}
return { 'data': [data] }
# Callback do gráfico de frequência de países
@app.callback(
Output('live-graph-countries', 'figure'),
[Input('graph-update', 'n_intervals')]
)
def update_graph_countries(n):
data = load_from_file('countries.json')
data = {k: v for k, v in sorted(data.items(), key=lambda item: item[1], reverse=True)}
data = {'x': list(data.keys())[:15], 'y': list(data.values())[:15], 'type': 'bar', 'name': 'SF'}
return { 'data': [data] }
# Callback do gráfico de frequência de fontes
@app.callback(
Output('live-graph-sources', 'figure'),
[Input('graph-update', 'n_intervals')]
)
def update_graph_sources(n):
data = load_from_file('sources.json')
data = {k: v for k, v in sorted(data.items(), key=lambda item: item[1], reverse=True)}
data = {'x': list(data.keys())[:15], 'y': list(data.values())[:15], 'type': 'bar', 'name': 'SF'}
return { 'data': [data] }
# Callback da lista de maiores RTs
@app.callback(
Output('most-rts', 'children'),
[Input('graph-update', 'n_intervals')]
)
def update_rts(n):
data = load_from_file('most_retweeted.json')
data = {k: v for k, v in sorted(data.items(), key=lambda item: item[1]['count'], reverse=True)}
top_rts = list(data.values())[:5]
return list(map(map_rts, top_rts))
def map_rts(tweet):
return html.Div(
children=[
html.P(children=[
html.B(children='Tweet: '),
html.A(href=tweet['link'], children=tweet['link'])
]),
html.P(children=[
html.B(children='Qtd: '),
html.P(children=[ tweet['count'] ])
])
],
style={ 'border': 'solid', 'padding': '5px', 'margin': '5px' }
)
############################3
# RUN SERVER
if __name__ == '__main__':
app.run_server(debug=True)