-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·332 lines (285 loc) · 11.9 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python3
import glob
import json
import time
import hashlib
import os
import os.path
import threading
import webbrowser
import requests
import datetime as DT
from flask import Flask, render_template, session, request, redirect, url_for
from werkzeug.utils import secure_filename
from json2html import *
import traceback
from aws import aws_fileupload, aws_read, replace
from nlp import correct_json
CUR_DIR = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = CUR_DIR + '/static/files/uploads'
RESULTS_FOLDER = CUR_DIR + '/static/files/results'
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['RESULTS_FOLDER'] = RESULTS_FOLDER
@app.route("/")
def index():
return render_template('index.html')
@app.route("/uploadfile", methods=['POST'])
def uploadfile():
# check if the post request has the file part
files = request.files.getlist("file[]")
# if 'file' not in request.files:
# raise Exception('No file part')
# return redirect(request.url)
for file in files:
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
raise Exception('No selected file')
return redirect(request.url)
if file:
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return ('success', 200)
@app.route("/processfile/<filename>")
def processfile(filename):
print("begin")
try:
aws_fileupload.file_upload(filename, UPLOAD_FOLDER)
print("uploaded")
aws_result = aws_read.file_read(filename, UPLOAD_FOLDER)
print ("start")
corrected_result = correct_json.main(aws_result)
print ("end")
replace.main(json.loads(corrected_result), filename, UPLOAD_FOLDER)
return (filename, 200)
except Exception as e:
print(e)
traceback.print_exc()
return (filename, 500)
@app.route("/history")
def history():
kwargs = {}
files = glob.glob(RESULTS_FOLDER+'/*')
resultfiles= []
idnames=[]
uploadfiles = []
scans = []
for file in files:
resultfiles.append(file.replace(CUR_DIR,""))
for file in resultfiles:
uploadfiles.append(file.replace("results","uploads"))
for file in files:
idnames.append(DT.datetime.utcfromtimestamp(os.stat(file).st_mtime+19800).isoformat())
for i,idname in enumerate(idnames):
temp={
'id' : idname,
'original_filename' : uploadfiles[i],
'output': resultfiles[i],
}
scans.append(temp)
kwargs['scans'] = scans
return render_template('history.html', **kwargs)
@app.route("/insights", methods=['GET', 'POST'])
def insights():
token = '71c4161312f0f36b120f80f4b015717bee72c4e337fc4800840786fa50102ccb'
if request.method == 'POST':
query = request.form['query']
if len(query) == 0 or not query.isalnum():
return render_template('insights.html')
option = request.form['options']
print(option)
if option == "Medicine":
print(query)
r = requests.get(
"http://www.healthos.co/api/v1/autocomplete/medicines/brands/" + query,
headers={
'Authorization': 'Bearer ' + token})
if len(
r.content) > 2: # checking if the response has more than just two brackets []
parsed = json.loads(r.content.decode('utf-8'))
for element in parsed:
del element['medicine_id']
del element['id']
del element['search_score']
finaltable = json2html.convert(json=parsed).replace('>', '>\n')
f = open("templates/template.html", "r")
contents = f.readlines()
f.close()
cssname = """<link href="/static/assets/css/table.css" rel="stylesheet"/>"""
contents.insert(27, cssname)
contents.insert(39, finaltable)
contents.insert(40,
"""
<br><br>
<a href="/insights" class="button">Search again</a>
""")
f = open("templates/new.html", "wb")
contents = "".join(contents)
f.write(contents.encode('utf-8'))
f.close()
with open("templates/new.html", "r") as f:
for num, line in enumerate(f, 1):
if num == 39:
newline = """<table class="table" border="1">"""
line = newline
else:
f = open("templates/template.html", "r")
contents = f.readlines()
f.close()
contents.insert(39, "<b>Your query returned no results.</b>")
contents.insert(40,
"""
<br><br>
<a href="/insights" class="button">Search again</a>
""")
f = open("templates/new.html", "wb")
contents = "".join(contents)
f.write(contents.encode('utf-8'))
f.close()
return render_template('new.html')
elif option == "LabTest":
print(query)
r = requests.get(
"http://www.healthos.co/api/v1/autocomplete/lab_tests/" + query,
headers={
'Authorization': 'Bearer ' + token})
if len(
r.content) > 2: # checking if the response has more than just two brackets []
parsed = json.loads(r.content.decode('utf-8'))
for element in parsed:
del element['lab_test_id']
del element['id']
del element['search_score']
finaltable = json2html.convert(json=parsed).replace('>', '>\n')
f = open("templates/template.html", "r")
contents = f.readlines()
f.close()
cssname = """<link href="/static/assets/css/table.css" rel="stylesheet"/>"""
contents.insert(27, cssname)
contents.insert(39, finaltable)
contents.insert(40,
"""
<br><br>
<a href="/insights" class="button">Search again</a>
""")
f = open("templates/new.html", "wb")
contents = "".join(contents)
f.write(contents.encode('utf-8'))
f.close()
with open("templates/new.html", "r") as f:
for num, line in enumerate(f, 1):
if num == 39:
newline = """<table class="table" border="1">"""
line = newline
else:
f = open("templates/template.html", "r")
contents = f.readlines()
f.close()
contents.insert(39, "<b>Your query returned no results.</b>")
contents.insert(40,
"""
<br><br>
<a href="/insights" class="button">Search again</a>
""")
f = open("templates/new.html", "wb")
contents = "".join(contents)
f.write(contents.encode('utf-8'))
f.close()
return render_template('new.html')
else:
r = requests.get(
"http://www.healthos.co/api/v1/autocomplete/diseases/" + query,
headers={
'Authorization': 'Bearer ' + token})
if len(
r.content) > 2: # checking if the response has more than just two brackets []
parsed = json.loads(r.content.decode('utf-8'))
for element in parsed:
del element['disease_id']
del element['disease_cat']
del element['search_score']
finaltable = json2html.convert(json=parsed).replace('>', '>\n')
f = open("templates/template.html", "r")
contents = f.readlines()
f.close()
cssname = """<link href="/static/assets/css/table.css" rel="stylesheet"/>"""
contents.insert(27, cssname)
contents.insert(39, finaltable)
contents.insert(40,
"""
<br><br>
<a href="/insights" class="button">Search again</a>
""")
f = open("templates/new.html", "wb")
contents = "".join(contents)
f.write(contents.encode('utf-8'))
f.close()
with open("templates/new.html", "r") as f:
for num, line in enumerate(f, 1):
if num == 39:
newline = """<table class="table" border="1">"""
line = newline
else:
f = open("templates/template.html", "r")
contents = f.readlines()
f.close()
contents.insert(39, "<b>Your query returned no results.</b>")
contents.insert(40,
"""
<br><br>
<a href="/insights" class="button">Search again</a>
""")
f = open("templates/new.html", "wb")
contents = "".join(contents)
f.write(contents.encode('utf-8'))
f.close()
return render_template('new.html')
else:
return render_template('insights.html')
@app.route("/about")
def about():
return render_template('about.html')
@app.route("/feedback",methods=['GET', 'POST'])
def feedback():
if request.method == 'POST':
name = request.form['Name']
email = request.form['email']
idnum = request.form['idnum']
phone = request.form['phone']
feedbacktext = request.form['feedbacktext']
if not os.path.exists('feedbacks'):
os.makedirs('feedbacks')
f = open("feedbacks/"+name+".txt", "wb")
f.write(("Name ->"+name+'\n').encode('utf-8'))
f.write(("email ->"+email+'\n').encode('utf-8'))
f.write(("Id ->"+idnum+'\n').encode('utf-8'))
f.write(("phone ->"+phone+'\n').encode('utf-8'))
f.write(("Feedback ->"+feedbacktext+'\n').encode('utf-8'))
f.close()
f = open("templates/template.html", "r")
contents = f.readlines()
f.close()
contents.insert(39, "<h3>Your response was captured and saved to the Feedback folder.</h3>")
contents.insert(40,
"""
<br><br>
<a href="/" class="button">Return to home.</a>
""")
f = open("templates/feedbackdone.html", "wb")
contents = "".join(contents)
f.write(contents.encode('utf-8'))
f.close()
return render_template('feedbackdone.html')
else:
return render_template('feedback.html')
if __name__ == '__main__':
app.debug = True
port = 8000
url = "http://127.0.0.1:{0}".format(port)
print(""" * DigiCon v1.0 is running on port 8000\n
Please open {0} in your browser to use the software.
""".format(url))
if not app.debug:
threading.Timer(1.00, lambda: webbrowser.open(url)).start()
app.run(port=port)