-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdash_app.py
72 lines (59 loc) · 1.95 KB
/
dash_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
from dash import Dash
from helpers.execute_query_postgresql import execute_query_postgresql
from helpers.layout_dash import get_layout
import os
import json
PATH_TO_SECRET = f"{os.path.dirname(os.path.abspath(__file__))}/secret.json"
with open(PATH_TO_SECRET, "r") as f:
secret = json.load(f)
table_name = secret["postgresql"]["tablename"]
aggregated_sentiment_stats = f"""
SELECT sentiment, COUNT(*) as count
FROM {table_name}
WHERE company_request_id = 'ZDFSDF'
GROUP BY sentiment
"""
aggregated_sentiment_stats = dict(execute_query_postgresql(PATH_TO_SECRET, aggregated_sentiment_stats))
sentiment_trend = f"""
SELECT date_extracted, sentiment, COUNT(*) as count
FROM {table_name}
WHERE company_request_id = 'ZDFSDF'
GROUP BY date_extracted, sentiment
ORDER BY date_extracted
"""
sentiment_trend = execute_query_postgresql(PATH_TO_SECRET, sentiment_trend)
sentiment_trend = [
{
"sentiment": "positive",
"date": [line[0] for line in sentiment_trend if line[1] == "positive"],
"count": [line[2] for line in sentiment_trend if line[1] == "positive"]
},
{
"sentiment": "negative",
"date": [line[0] for line in sentiment_trend if line[1] == "negative"],
"count": [line[2] for line in sentiment_trend if line[1] == "negative"]
},
{
"sentiment": "neutral",
"date": [line[0] for line in sentiment_trend if line[1] == "neutral"],
"count": [line[2] for line in sentiment_trend if line[1] == "neutral"]
}
]
reviews = f"""
SELECT text, sentiment
FROM {table_name}
WHERE company_request_id = 'ZDFSDF'
LIMIT 100
"""
reviews = execute_query_postgresql(PATH_TO_SECRET, reviews)
reviews = [
{
"text": review[0],
"sentiment": review[1]
}
for review in reviews
]
app = Dash(__name__)
app.layout = get_layout(sentiment_trend, aggregated_sentiment_stats, reviews)
if __name__ == "__main__":
app.run_server(debug=True)