forked from halloTheCoder/web-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
94 lines (71 loc) · 3.46 KB
/
app.py
File metadata and controls
94 lines (71 loc) · 3.46 KB
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
import os
import json
import urllib
import h5py
import pickle as pk
import numpy as np
from os.path import join, dirname, realpath
from flask import Flask, request, redirect, url_for, send_from_directory, render_template, flash, Response
from werkzeug.utils import secure_filename
import engine # remember to reinclude this
# A <form> tag is marked with enctype=multipart/form-data and an <input type=file> is placed in that form.
# The application accesses the file from the files dictionary on the request object.
# use the save() method of the file to save the file permanently somewhere on the filesystem.
UPLOAD_FOLDER = join(dirname(realpath(__file__)), 'static/uploads/') # where uploaded files are stored
ALLOWED_EXTENSIONS = set(['png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG', 'gif', 'GIF']) # models support png and gif as well
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024 # max upload - 10MB
app.secret_key = 'secret'
# check if an extension is valid and that uploads the file and redirects the user to the URL for the uploaded file
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/')
def home():
return render_template('index.html', result=None)
@app.route('/<a>')
def available(a):
flash('{} coming soon!'.format(a))
return render_template('index.html', result=None, scroll='third')
@app.route('/assessment')
def assess():
return render_template('index.html', result=None, scroll='third')
@app.route('/assessment', methods=['GET', 'POST'])
def upload_and_classify():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(url_for('assess'))
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(url_for('assess'))
if file and allowed_file(file.filename):
filename = secure_filename(file.filename) # used to secure a filename before storing it directly on the filesystem
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# return redirect(url_for('uploaded_file',
# filename=filename))
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
model_results = engine.engine(filepath)
return render_template('results.html', result=model_results, scroll='third', filename=filename)
flash('Invalid file format - please try your upload again.')
return redirect(url_for('assess'))
# @app.route('/show/<filename>')
# def uploaded_file(filename):
# return render_template('template.html', filename=filename)
@app.route('/uploads/<filename>')
def send_file(filename):
return send_from_directory(UPLOAD_FOLDER, filename)
# Now one last thing is missing: the serving of the uploaded files.
# In the upload_file() we redirect the user to url_for('uploaded_file', filename=filename),
# that is, /uploads/filename. So we write the uploaded_file() function to return the file of that name.
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True, use_reloader=False) # remember to set back to False