-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
98 lines (77 loc) · 2.89 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
from flask import *
import os
from werkzeug.utils import secure_filename
from preprocess import processData
app = Flask(__name__)
app.secret_key = "secret key"
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
# Get current path
path = os.getcwd()
# file Upload
UPLOAD_FOLDER = os.path.join(path, 'uploads')
# Make directory if uploads is not exists
if not os.path.isdir(UPLOAD_FOLDER):
os.mkdir(UPLOAD_FOLDER)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Allowed extension you can set your own
ALLOWED_EXTENSIONS = set(['pkl'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def hello():
return render_template('index.html')
@app.route('/help')
def help():
return render_template('tutorial.html')
@app.route('/contact')
def contact():
return render_template('contact.html')
@app.route('/success', methods = ['GET','POST'])
def success():
if request.method == 'POST':
filenamesArray=[]
if 'files[]' not in request.files:
flash('No file part')
return redirect(request.url)
files = request.files.getlist('files[]')
for file in files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
# print(filename)
filenamesArray.append(filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
flash('File(s) successfully uploaded')
print(app.config['UPLOAD_FOLDER'], filenamesArray)
testResult = processData(app.config['UPLOAD_FOLDER'], filenamesArray)
# print(testResult)
# return redirect('/')
return jsonify(testResult)
# return "Hello lulu"
# return make_response(jsonify(testResult), 200)
else:
print("The request method is not POST")
print(request.method)
print(request)
filenamesArray=[]
if 'files[]' not in request.files:
flash('No file part')
return redirect(request.url)
files = request.files.getlist('files[]')
for file in files:
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
# print(filename)
filenamesArray.append(filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
flash('File(s) successfully uploaded')
print(app.config['UPLOAD_FOLDER'], filenamesArray)
testResult = processData(app.config['UPLOAD_FOLDER'], filenamesArray)
# print(testResult)
# return redirect('/')
@after_this_request
def cleanup():
for f in filenamesArray:
os.remove(os.path.join(app.config['UPLOAD_FOLDER'], f))
return testResult
if __name__ == '__main__':
app.run(debug=True)