-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
177 lines (146 loc) · 7.02 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
import json
import time
import glob
import os
from tsne_lib import tsne_script
from shutil import copyfile
from flask import Flask, request, redirect, url_for, render_template
from werkzeug.utils import secure_filename
import uuid
from IPython import embed
from flask_thumbnails import Thumbnail
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = Flask(__name__)
thumb = Thumbnail(app)
app.config['THUMBNAIL_MEDIA_ROOT'] = os.getcwd() + ''
app.config['THUMBNAIL_MEDIA_URL'] = ''
app.config['THUMBNAIL_MEDIA_THUMBNAIL_ROOT'] = os.getcwd() + ''
app.config['THUMBNAIL_MEDIA_THUMBNAIL_URL'] = ''
app.config['THUMBNAIL_STORAGE_BACKEND'] = 'flask_thumbnails.storage_backends.FilesystemStorageBackend'
app.config['THUMBNAIL_DEFAUL_FORMAT'] = 'JPEG'
app.config['UPLOAD_FOLDER'] = 'static/uploads'
app.config['MAX_CONTENT_LENGTH'] = 300 * 1024 * 1024 # Allow 300 MiB maximum upload for all images at once
FILE_SIZE_LIMIT = 3 * 1024 * 1024 # 3 MiB
NUM_IMAGES_LIMIT = 60
@app.route("/")
def main():
# Set a session ID
session_id=request.args.get('session')
if not session_id:
session_id = uuid.uuid4()
return redirect('/?session=%s' % session_id)
# Create folders for this session ID
if not os.path.exists('static/output/%s/' % session_id):
os.makedirs('static/output/%s/' % session_id)
if not os.path.exists('static/uploads/%s/' % session_id):
os.makedirs('static/uploads/%s/' % session_id)
# Check if plot exists, the front-end will display different things
# plot_exists = os.path.exists('static/output/%s/*.png' % session_id)
pathlength=len("static/output/"+session_id)
plot_exists = False
plot_name=""
plot_name_no_dir=""
# files = list(glob.glob('static/output/%s/*.png' % session_id).encode('ascii','ignore') )
files = list(glob.glob('static/output/%s/*.png' % session_id))
for x in range(0,len(files)):
files[x]=files[x].encode('ascii','ignore');
if (len(files)!=0):
plot_exists=True
plot_name= max(files, key=os.path.getctime).encode('ascii','ignore') #getting the name of the newest file
plot_name_no_dir=plot_name[pathlength:]
csv_exists = False
csv_name=""
csv_name_no_dir=""
files = list(glob.glob('static/output/%s/*features*.csv' % session_id))
for x in range(0,len(files)):
files[x]=files[x].encode('ascii','ignore');
if (len(files)!=0):
csv_exists=True
csv_name= max(files, key=os.path.getctime).encode('ascii','ignore') #getting the name of the newest file
csv_name_no_dir=csv_name[pathlength:]
# Create list of files organized by colors
colors = []
color_names=list(glob.glob('static/uploads/%s/*' % session_id))
for color_name in color_names:
color_name = os.path.basename(color_name) # get just the color name, not a long folder path including static/uploads
images=list(glob.glob('static/uploads/%s/%s/*' % (session_id,color_name)))
images=[ x for x in images if "100x100" not in x ] # remove thumbnails from list, they will be generated
colors.append({'hex':color_name,'images':images})
image_exists = len(colors)
return render_template('main.html', files=files, colors=colors, session_id=session_id, plot_exists=plot_exists, csv_exists=csv_exists, image_exists=image_exists, timestamp=str(time.time()), plot_name=plot_name, plot_name_no_dir=plot_name_no_dir, csv_name=csv_name_no_dir)
@app.route("/tsne", methods=['POST', 'GET'])
def tsne():
session_id=request.args.get('session')
if not session_id:
session_id = uuid.uuid4()
return redirect('/?session=%s' % session_id)
perplexity = int (request.args.get('perplexity'))
early_exaggeration = int (request.args.get('early_exaggeration'))
learning_rate = int (request.args.get('learning_rate'))
# resolution = int( request.args.get('resolution'))
resolution = 200 # hard coded this to simplify front-end
CanvasSize = int (request.args.get('CanvasSize'))
DotsPerInchs = int (request.args.get('DotsPerInchs'))
model_name = request.args.get('model_name')
# Get colors and images information
colors_text = request.args.get('colors')
# Convert from badly formatted json to dict
colors_text = colors_text.replace("'",'"')
colors_text = colors_text.replace('u"','"')
colors_dict = json.loads(colors_text)
colors = []
hex_number = ""
for section in range(len(colors_dict)):
for hex1 in colors_dict[section]['hex']:
hex_number+= hex1
for files in colors_dict[section]['images']:
colors.append('#'+hex_number.encode('ascii','ignore'))
hex_number = ""
print 'creating tsne plot'
tsne_data = tsne_script.tsne_images(session_id, colors_dict, resolution, perplexity,early_exaggeration, learning_rate, DotsPerInchs,CanvasSize, colors, model_name)
return redirect('/?session=%s#try' % session_id)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['POST'])
def upload_file():
session_id=request.args.get('session')
hexcolor=request.form.get('hexcolor')
hexcolor=hexcolor[1:] # remove # from #FFFFFF
if not session_id:
session_id = uuid.uuid4()
return redirect('/?session=%s' % session_id)
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
print('No file part')
return redirect(request.url)
files = request.files.getlist("file")
# Create folder if necessary
folder = os.path.join(app.config['UPLOAD_FOLDER'], session_id, hexcolor)
if not os.path.exists(folder):
os.makedirs(folder)
# Check if number of images exceeds the limit
new_num_images = len(files)
existing_num_images = len(list(glob.glob('static/uploads/%s/*' % session_id)))
if new_num_images + existing_num_images > NUM_IMAGES_LIMIT:
return redirect(request.url) # TODO: Warn user that this has happened
for file in files:
# if user does not select file, browser also submit a empty part without filename
if file.filename == '':
print('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
# Skip file if too large (>5MB)
file.seek(0, os.SEEK_END)
file_size = file.tell()
if file_size > FILE_SIZE_LIMIT:
continue # Skip this file it is too big # TODO: Warn user that this has happened
file.seek(0) # File is allowable size seek back to the beginning of the file. Without this no data would be read because we already seeked to the end of the file to read it's length.
# Save file
filename = secure_filename(file.filename)
path = os.path.join(folder, filename)
file.save(path)
return redirect(request.url)
if __name__ == "__main__":
app.run(debug = True, port=3000)