-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
314 lines (205 loc) · 9.55 KB
/
server.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""Flask site for """
from flask import Flask, render_template, session, request, jsonify
from flask_debugtoolbar import DebugToolbarExtension
import jinja2
from model import Tweet, Candidate, connect_to_db, db
from datetime import datetime
import sqlalchemy
from sqlalchemy import func
from global_dicts import POSITIVE_COLORS, NEGATIVE_COLORS, GOOGLE_MAPS_COLORS, SEED_DATA_DIR
app = Flask(__name__)
app.secret_key = "h65Tgx4RTzS21"
def load_sentiment_data(filename):
"""
Load file with sentiment data for a given candidate, and returns a list of lists
containing data on dates, number of negative tweets on that day, and number of
positive tweets on that day
>>> load_sentiment_data("test_data/sentiment_data_test.txt")
[['2016/01/01', '2016/01/02'], ['704', '832'], ['125', '781']]
"""
dates = []
pos_scores = []
neg_scores = []
for row in open(filename):
row = row.rstrip()
items = row.split("|")
dates.append(items[0])
neg_scores.append(items[1])
pos_scores.append(items[2])
return [dates, neg_scores, pos_scores]
################################################################################
def load_location_data(filename):
"""
Load file with location based data - returns a list containing tuples with the latlong
geotagged in the tweets, the number of tweets for a particular candidate/valence
>>> load_location_data('test_data/location_data_test.txt')
[({'lat': 33.836081, 'lng': -81.1637245}, 4.0, 'Trump', 'neg'), ({'lat': 42.9506714, 'lng': -73.2636079}, 1.0, 'Both', 'neg')]
"""
location_data = []
for row in open(filename):
row = row.rstrip()
lat, lng, tweets, candidate, sentiment = row.split("|")
location_data.append(({"lat" : float(lat), "lng" : float(lng)}, float(tweets), candidate, sentiment))
return location_data
################################################################################
def pos_line_chart(candidate):
"""
Generate a json object for sentiment line chart using global dictionary
>>> dict = pos_line_chart('Clinton')
>>> dict['backgroundColor']
'rgba(143, 211, 228, 0.2)'
>>> dict = pos_line_chart('Trump')
>>> dict['borderColor']
'rgba(253, 175, 175,1)'
>>> dict = pos_line_chart('Both')
>>> dict['pointBorderColor']
'rgba(128, 239, 133, 1)'
"""
chart_specs = {
"label": "",
"fill": False,
"lineTension": 0.5,
"backgroundColor": POSITIVE_COLORS[candidate]["backgroundColor"],
"borderColor": POSITIVE_COLORS[candidate]["borderColor"],
"borderCapStyle": 'butt',
"borderDash": [],
"borderDashOffset": 0.0,
"borderJoinStyle": 'miter',
"pointBorderColor": POSITIVE_COLORS[candidate]["pointBorderColor"],
"pointBackgroundColor": "#fff",
"pointBorderWidth": 1,
"pointHoverRadius": 5,
"pointHoverBackgroundColor": "#fff",
"pointHoverBorderColor": POSITIVE_COLORS[candidate]["pointHoverBorderColor"],
"pointHoverBorderWidth": 2,
"pointRadius": 3,
"pointHitRadius": 10,
"data": [0],
"spanGaps": False}
return chart_specs
################################################################################
def neg_line_chart(candidate):
"""Generate a json object for sentiment line chart using a global dictionary
>>> dict = neg_line_chart('Clinton')
>>> dict['pointHoverBorderColor']
'rgba(55,7,247,1)'
>>> dict = neg_line_chart('Trump')
>>> dict['pointBorderColor']
'rgba(182,6,6,1)'
>>> dict = neg_line_chart('Both')
>>> dict['backgroundColor']
'rgba(17, 130, 53, 0.2)'
"""
chart_specs = {
"label": "",
"fill": False,
"lineTension": 0.5,
"backgroundColor": NEGATIVE_COLORS[candidate]["backgroundColor"],
"borderColor": NEGATIVE_COLORS[candidate]["borderColor"],
"borderCapStyle": 'butt',
"borderDash": [],
"borderDashOffset": 0.0,
"borderJoinStyle": 'miter',
"pointBorderColor": NEGATIVE_COLORS[candidate]["pointBorderColor"],
"pointBackgroundColor": "#fff",
"pointBorderWidth": 1,
"pointHoverRadius": 5,
"pointHoverBackgroundColor": "#fff",
"pointHoverBorderColor": NEGATIVE_COLORS[candidate]["pointHoverBorderColor"],
"pointHoverBorderWidth": 2,
"pointHitRadius": 10,
"data": [0],
"spanGaps": False}
return chart_specs
################################################################################
@app.route("/")
def homepage():
"""Data visualization page"""
return render_template("homepage_charts.html")
################################################################################
@app.route("/sentiment_data.json")
def load_csv():
"""Load in data from csv format
"""
options = {SEED_DATA_DIR + "/clinton_data.txt" : {"pos_label": "Clinton - Positive", "neg_label": "Clinton - Negative", "candidate" : "Clinton"},
SEED_DATA_DIR + "/trump_data.txt" : {"pos_label": "Trump - Positive", "neg_label": "Trump - Negative", "candidate" : "Trump"},
SEED_DATA_DIR + "/both_data.txt" : {"pos_label" : "Both Referenced - Positive", "neg_label": "Both Referenced - Negative", "candidate" : "Both"}}
datasets = []
for option in options.keys():
dates, neg_nums, pos_nums = load_sentiment_data(option)
pos_json_object = pos_line_chart(options[option]["candidate"])
pos_json_object["label"] = options[option]["pos_label"]
pos_json_object["data"] = pos_nums
datasets.append(pos_json_object)
neg_json_object = neg_line_chart(options[option]["candidate"])
neg_json_object["label"] = options[option]["neg_label"]
neg_json_object["data"] = neg_nums
datasets.append(neg_json_object)
sentiment_json = {
"labels": dates,
"datasets": datasets
}
return jsonify(sentiment_json)
################################################################################
@app.route("/donut_chart.json/<candidate>")
def load_clinton_donut(candidate):
"""Create donut chart representing total neg/pos of tweets about the passed in candidate"""
pos_tweets = db.session.query(func.count(Tweet.tweet_id)).filter((Tweet.naive_bayes == "pos") & (Tweet.referenced_candidate == candidate)).one()
neg_tweets = db.session.query(func.count(Tweet.tweet_id)).filter((Tweet.naive_bayes == "neg") & (Tweet.referenced_candidate == candidate)).one()
datasets = [{"data" : [pos_tweets[0], neg_tweets[0]],
"backgroundColor" : [POSITIVE_COLORS[candidate]["borderColor"], NEGATIVE_COLORS[candidate]["borderColor"]]}]
donut_json = {
"labels": ["Positive", "Negative"],
"datasets": datasets
}
return jsonify(donut_json)
################################################################################
@app.route("/sum_comparison.json")
def sum_comparison():
"""Create horizontal chart representing total neg/pos of tweets about Clinton"""
clinton_tweets = db.session.query(func.count(Tweet.tweet_id)).filter(Tweet.referenced_candidate == 'Clinton').one()
trump_tweets = db.session.query(func.count(Tweet.tweet_id)).filter(Tweet.referenced_candidate == 'Trump').one()
both_tweets = db.session.query(func.count(Tweet.tweet_id)).filter(Tweet.referenced_candidate == 'Both').one()
# Potentially - backup database - have flask check which database it should be connected to
# And from then, use given data source
# Could come in with AJAX
# Also could connect to which server
datasets = []
datasets.append({"data" : [clinton_tweets[0], trump_tweets[0], both_tweets[0]],
"backgroundColor" : ["rgba(55,7,247,1)", "rgba(182,6,6,1)", "rgba(17, 130, 53, 1)"],
"label": "All Tweets"})
sum_comparison_json = {
"labels": ["Clinton", "Trump", "Both"],
"datasets": datasets
}
return jsonify(sum_comparison_json)
################################################################################
@app.route("/map.json")
def load_maps_data():
"""Create json object for google maps API"""
location_data = load_location_data("seed_data/location_data.txt")
json_list = []
for location in location_data:
location_specs = {"coordinates": location[0],
"num_tweets": float(location[1]),
"color": GOOGLE_MAPS_COLORS[location[2]][location[3]]}
json_list.append(location_specs)
test_json = {"location_data" : json_list}
return jsonify(test_json)
################################################################################
if __name__ == "__main__":
import doctest
import sys
print
result = doctest.testmod()
if not result.failed:
print "ALL TESTS PASSED"
print
if sys.argv[-1] == "jstest":
JS_TESTING_MODE = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# app.debug = False
# app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
# DebugToolbarExtension(app)
connect_to_db(app)
app.run(host='0.0.0.0')