This repository has been archived by the owner on Oct 7, 2023. It is now read-only.
/
__init__.py
106 lines (78 loc) · 3.63 KB
/
__init__.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from flask import Flask, render_template
from flask import request
import sqlite3
import pandas as pd
import pygal
from pygal.style import Style
app = Flask(__name__)
DATABASE = '/home/fedecarles/asmd/ASMD.db'
def connect_to_database():
return sqlite3.connect(DATABASE)
c = connect_to_database()
df = pd.read_sql_query("SELECT * FROM entidades", c)
df['dateStamp'] = pd.to_datetime(df['dateStamp'], format="%Y-%m-%d")
df['counts'] = df.groupby('entidad')['entidad'].transform('count')
df = df.sort(['counts', 'dateStamp'], ascending=False)
# Estas son las entidades que aparecen en el listado de la izquierda.
# Cada entidad esta asociada su ultimo valor medio, que indica si esta
# en alza o en baja. Primero se agrupan las por entidad y fecha.
# Luego se agrupa nuevamente extrayendo el primer valor (o sea el ultimo dia)
# para cada entidad.
entities = df[['entidad', 'dateStamp', 'valor']].groupby(['entidad','dateStamp'],sort=False).mean()
entities = entities.reset_index()
entities= entities.groupby(['entidad'], sort=False).head(1)
@app.route('/', methods=["GET", "POST"])
def homepage():
# Contenedores locales.
selected_entity = []
se_subset = pd.DataFrame()
asociated_words = pd.DataFrame()
sources = pd.DataFrame()
graph_data = []
# Las lineas que siguen son las acciones del lado del servidor que
# corren cada vez que el usuario hace click en una entidad.
if request.method == 'POST':
selected_entity = request.form.get('entidades', None)
se_subset = df[df.entidad == selected_entity]
se_subset = se_subset.sort(['dateStamp'])
last_week_days = se_subset['dateStamp'].iloc[-7]
last_week_subset = se_subset[se_subset.dateStamp == last_week_days]
asociated_words = (last_week_subset[['adjetivo', 'valor']]
.groupby('adjetivo').sum()
.sort('valor', ascending=False))
urls = list(pd.unique(last_week_subset.link.ravel()))
titles = list(pd.unique(last_week_subset.titulo.ravel()))
sources = pd.DataFrame(urls, titles)
# Creacion del grafico con Pygal.
custom_style = Style(background='transparent',
plot_background='transparent',
title_font_size=32)
graph = pygal.Line(show_legend=False, x_label_rotation=20, width=1500,
height=450, explicit_size=True, range=(-1.2, 1.2),
background="transparent", foreground="transparent",
plot_background="transparent", margin=0,
style=custom_style, show_minor_x_labels = False)
graph.title = "Sentimiento para '"+selected_entity+"'"
agg = se_subset.groupby('dateStamp').mean()
m_avg = pd.rolling_mean(agg, 3)
m_avg = m_avg.fillna(0)
# graph.add(selected_entity, list(agg['valor']))
graph.add(selected_entity, list(m_avg['valor']))
date = pd.DatetimeIndex(agg.index)
graph.x_labels = map(str, date)
graph.x_labels_major = map(str, date[0::5])
graph_data = graph.render_data_uri()
return render_template('index.html', entities=entities,
graph_data=graph_data,
asociated_words=asociated_words,
se_subset=se_subset, sources=sources)
@app.route('/comofunciona/', methods=["GET", "POST"])
def comofunciona():
return render_template('comofunciona.html')
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html")
if __name__ == "__main__":
app.run()