-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
228 lines (228 loc) · 9.75 KB
/
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
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
from flask import render_template
import os
from flask import Flask, request
from werkzeug.utils import secure_filename
import json
import csv
import pandas as pd
ALLOWED_EXTENSIONS =['txt']
ALLOWED_FILES =['201810','201811','201812','201901','201902','201903','201904','201905','201906']
app = Flask(__name__)
from plotter import getPlot
from predictor import getFuturePlot
from testing_file import Handler
from matcharticles import findArticles
from preprocess import preprocessTweets
@app.route('/')
def index():
return render_template('home.html')
@app.route('/viewpaths')
def viewpaths():
return render_template('viewpaths.html')
@app.route('/validate',methods=['POST', 'GET'])
def validate():
secret_key="ad123"
if request.method == 'POST':
result = request.form
key=result["key"]
if key==secret_key:
return render_template('admin_uploadfile.html')
else:
return render_template('adminkey.html')
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def get_file_path(field):
if field=="1":
UPLOAD_FOLDER = 'politics_tweetdb'
elif field=="2":
UPLOAD_FOLDER = 'education_tweetdb'
elif field=="3":
UPLOAD_FOLDER = 'terrorism_tweetdb'
elif field == "4":
UPLOAD_FOLDER = 'food_tweetdb'
return UPLOAD_FOLDER
@app.route('/uploadcheck', methods = ['GET', 'POST'])
def uploadcheck():
if request.method == 'POST':
field = request.form["field"]
tweetdate = request.form["tweetdate"]
try:
# write to permanant store
processedfolder=get_file_path(field)
file = open(os.path.join(processedfolder, tweetdate), 'r')
error = "You have already uploaded dataset!"
return render_template('admin_uploadfile_check.html', error=error)
except FileNotFoundError:
error = "No file was found!"
return render_template('admin_uploadfile.html', error=error)
@app.route('/upload', methods = ['GET', 'POST'])
def upload():
if request.method == 'POST':
field = request.form["field"]
tweetdate = request.form["tweetdate"]
if field not in ["1","2","3","4"]:
error="No field selected"
return render_template('admin_uploadfile.html', error=error)
# check if the post request has the file part
if 'file' not in request.files:
error="No file selected"
return render_template('admin_uploadfile.html', error=error)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
error = "No file selected"
return render_template('admin_uploadfile.html', error=error)
if file and file.filename in ALLOWED_FILES:
filename = secure_filename(file.filename)
print(filename)
path = get_file_path(field)
print(path)
file.save(os.path.join(path, filename))
'''
app.config['TEMP_FOLDER'] = 'Temp'
file.save(os.path.join('Temp', filename))
path=get_file_path(field)
'''
preprocessTweets(tweetdate=tweetdate,file=filename,storeFolder=path)
success = "Successfully uploaded"
return render_template('admin_uploadfile.html', success=success)
return render_template('admin_uploadfile.html')
else:
error = "Invalid File Name!"
return render_template('admin_uploadfile.html', error=error)
error="Invalid file type"
return render_template('admin_uploadfile.html', error=error)
@app.route('/testpy',methods = ['POST', 'GET'])
def testpy():
if request.method == 'POST':
result = request.form
field=result["field"]
start_date=result["start"]
end_date = result["end"]
if int(start_date) >= int(end_date):
error = "Invalid Month Range"
#raise ValueError
return render_template('viewpaths.html', error=error)
files = ['OutputDTM0.csv', 'OutputDTM1.csv', 'OutputDTM2.csv', 'OutputDTM3.csv', 'OutputDTM4.csv']
resultstore = start_date + end_date
if field == '1':
storeinfolder = 'result_topics_pol'
elif field == '2':
storeinfolder = 'result_topics_edu'
elif field == '3':
storeinfolder = 'result_topics_ter'
elif field == '4':
storeinfolder = 'result_topics_food'
try:
# write to permanant store
file = open(os.path.join(storeinfolder, resultstore), 'r')
renderlist = []
i = 0
for line in file:
renderlist.append(json.loads(line))
print(renderlist[i])
i = i + 1
print(renderlist[2])
# write the topics just viewd-for future plot
xdates = renderlist[0]
dataseries = renderlist[1]
print("++++++++++++++++++++++++++++++++++")
for i in range(5):
with open(files[i], 'w', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
# Write header
writer.writerow(['TopicID', 'Word', 'Year', 'Probability'])
for year_i in range(len(xdates)):
write=[int(i), dataseries[i]['annotes'][year_i], xdates[year_i], dataseries[i]['data'][year_i]]
writer.writerow(write)
print(write)
return render_template('testgraph.html', xdates=renderlist[0], dataseries=renderlist[1], articles=renderlist[2])
except FileNotFoundError:
print("File not found -but creating now")
path = get_file_path(field)
Handler(storeFolder=path, start=start_date, end=end_date).handle()
print("Successful in handler")
getdict = getPlot()
dataseries = getdict["datalist"]
articles = findArticles(field, dataseries)
print(articles)
xdates = getdict["xdates"]
print(xdates)
print(dataseries)
mylist = [xdates, dataseries, articles]
f = open(os.path.join(storeinfolder, resultstore), 'w', encoding='utf-8')
for jsonobj in mylist:
jsonstr = json.dumps(jsonobj)
f.write(jsonstr + "\n")
f.flush()
f.close()
return render_template('testgraph.html', xdates=xdates, dataseries=dataseries,articles=articles)
return
@app.route('/testfuturepy',methods = ['POST', 'GET'])
def testfuturepy():
if request.method == 'POST':
result = request.form
topic=int(result["topic"])
end_date = result["end"]
'''
try:
with open('OutputDTM0.csv', 'r') as csvfile:
csvFileReader = csv.reader(csvfile)
for row in csvFileReader:
print(row)
if row != '\n':
print(row)
csvfile.close()
getdict = getFuturePlot(topic_number=topic, end=end_date)
if getdict == "":
error = "Invalid End Month"
# raise ValueError
return render_template('viewfuturepaths.html', error=error)
print("row exists.....................................................")
dataseries = getdict["prob"]
xdates = getdict["dates"]
annotes = getdict["annotes"]
except:
print("came.........................")
error = "First view a topic evolution, before any predictions!"
# raise ValueError
return render_template('viewfuturepaths.html', error=error)
'''
print("trying.............................")
with open('OutputDTM0.csv', 'r') as csvfile:
csvFileReader = csv.reader(csvfile)
print("woooooooooooooooooooow")
for row in csvFileReader:
print(row)
if row != '\n':
print(row)
# csvfile.close()
getdict = getFuturePlot(topic_number=topic, end=end_date)
if getdict == "":
error = "Invalid End Month"
# raise ValueError
return render_template('viewfuturepaths.html', error=error)
print("row exists.....................................................")
dataseries = getdict["prob"]
xdates = getdict["dates"]
annotes = getdict["annotes"]
return render_template('testfuturegraph.html', xdates=xdates, dataseries=dataseries,
annotes=annotes)
print("came.........................")
error = "First view a topic evolution, before any predictions!"
# raise ValueError
return render_template('viewfuturepaths.html', error=error)
print("eeeeeeeeeeeeeeeeeeee")
@app.route('/viewfuturepaths')
def viewfuturepaths():
return render_template('viewfuturepaths.html')
@app.route('/adminkey')
def adminkey():
return render_template('adminkey.html')
@app.route('/adminupload')
def adminupload():
return render_template('admin_uploadfile.html')
if __name__=='__main__':
app.run()