-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
executable file
·128 lines (98 loc) · 3.24 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
# sasha iatsenia 2019 for glint
import os
from werkzeug.utils import secure_filename
import pandas as pd
import uuid
import redis
import pickle
from job import validate_extension, create_job
from flask_cors import CORS
import sys
from flask import (
Flask, request, redirect, jsonify, render_template,
send_from_directory
)
# flask config, load react frontend
app = Flask(__name__,
static_folder = './public',
template_folder="./static")
# enable CORS on app
CORS(app)
# redis config
r = redis.from_url(os.environ.get("REDIS_URL"))
# upload file config
UPLOAD_FOLDER = '/data/'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/upload', methods=['POST'])
def upload_file():
"""Process file uploaded from frontend"""
if 'file' not in request.files:
return 'No file present in headers'
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
return 'No file uploaded'
if file:
# clean file name for safety
file_name = secure_filename(file.filename)
# validate the file to make sure the filetype is supported
print(file_name)
validated, file_type = validate_extension(file_name)
# create job if validated
if validated == True:
# save file
file.save(os.path.join(app.config['UPLOAD_FOLDER'], file_name))
# create a job
file_name, job_id = create_job(file_name, file_type)
print(f'Created {file_name} job with id {job_id}')
else:
return 'Invalid filetype'
# get the origin for a proper redirect
origin = request.environ.get('HTTP_ORIGIN')
return jsonify({'error': False,
'jobId': job_id})
@app.route('/data')
def get_data():
"""Return data to user with corresponding job ID"""
# get job id from url
job_id = request.args.get('jobId')
# check that we were able to get a url param
if job_id == None:
return jsonify({'error': True,
'error_description': 'Missing job id'})
# validate job id
try:
val = uuid.UUID(job_id, version=4)
except ValueError:
return jsonify({'error': True,
'error_description': 'Invalid job id'})
# pull data from redis
try:
p_data = r.get(str(job_id))
data = pickle.loads(p_data)
return jsonify(data)
except:
return jsonify({'error': True,
'error_description': 'Unable to pull data from queue'})
@app.route('/insights/<insight>')
def get_insights(insight):
"""Returns static insights after analysis has been completed"""
# send static file from insights directory
return send_from_directory('insights', insight)
@app.route('/test')
def test():
return 'Hello docker'
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def react(path):
"""Serve the user the frontend react code"""
return render_template('index.html')
if __name__ == "__main__":
# test if redis is running
try:
r.ping()
except redis.ConnectionError:
sys.exit('Error connecting to redis')
# start app normally
app.run(debug=False, host='0.0.0.0', port=os.environ["PORT"])