-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app_crowd.py
188 lines (127 loc) · 5.07 KB
/
flask_app_crowd.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
from flask import Flask, jsonify, request, render_template, redirect, url_for, session
from sqlalchemy.orm.session import sessionmaker, make_transient
from sqlalchemy import create_engine
from database import db
from page_model_oct import PageModel
import glob
import os
import random
import numpy as np
from cython_tste.tste_next_point import *
import time
import hashlib
print "Hello"
# Choose a random triplet and set the page's triplet accordingly
def update_page_with_random():
page_ims = random.sample(range(len(image_list)), 3)
page_model_dict[session['name']].main_img = page_ims[0]
page_model_dict[session['name']].compare_img_1 = page_ims[1]
page_model_dict[session['name']].compare_img_2 = page_ims[2]
page_model_dict[session['name']].main_path = image_list[page_ims[0]]
page_model_dict[session['name']].compare_1_path = image_list[page_ims[1]]
page_model_dict[session['name']].compare_2_path = image_list[page_ims[2]]
# Make Flask app
app = Flask(__name__)
app.config["DEBUG"] = True
# Set up database
SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
username="cs101teaching",
password="gogo_teaching",
hostname="cs101teaching.mysql.pythonanywhere-services.com",
databasename="cs101teaching$teaching",
)
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_POOL_RECYCLE"] = 200
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
db.init_app(app)
db.app = app
engine = create_engine(SQLALCHEMY_DATABASE_URI, pool_recycle=200)
Session_sql = sessionmaker(bind=engine, expire_on_commit=False)
session_sql = Session_sql()
# Make image list
# image_list = glob.glob("/home/cs101teaching/MachineTeaching/static/oct/data_resized/*/*")
image_list = glob.glob("/home/cs101teaching/MachineTeaching/static/birds/images/*/*")
image_list.sort()
image_list = [img.replace("/home/cs101teaching/MachineTeaching", "") for img in image_list]
# Make dictionary to keep track of page models
page_model_dict = {}
# Make dictionary to keep track of how many jobs each user has done
user_nclicks_dict = {}
user_id_dict = {}
user_time_dict = {}
user_code_dict = {}
# Set triplet
random.seed()
counter = 0
max_clicks = 50
# Redirect user to login page
@app.route("/")
def to_login():
return redirect(url_for('login'))
# Return image list in JSON format
@app.route("/get_imgs")
def get_imgs():
return jsonify(page_model_dict[session['name']].get_imgs_list() + [str(user_nclicks_dict[session['name']])])
@app.route("/end/")
def logout():
if ('name' in session and session['name'] in user_nclicks_dict and
user_nclicks_dict[session['name']] == max_clicks):
# end_id = session['name']
end_id = hashlib.md5(str(session['name'])).hexdigest()
# print end_id
return render_template('end.html', end_id=end_id)
else:
return redirect(url_for('login'))
# THESE DO LOTS
#
# - Gets the response from the user
# - Updates the user's kernel
@app.route("/kernel/get_response", methods = ['POST'])
def get_response_kernel():
if not 'name' in session or not session['name'] in user_nclicks_dict:
return jsonify([url_for('login'), 0])
if request.method == 'POST':
data = request.get_data()
if data == "0":
page_model_dict[session['name']].set_chosen(page_model_dict[session['name']].compare_img_1)
elif data == "1":
page_model_dict[session['name']].set_chosen(page_model_dict[session['name']].compare_img_2)
user_nclicks_dict[session['name']] += 1
user_time_dict[session['name']][1] = time.time()
if user_nclicks_dict[session['name']] == max_clicks:
return jsonify([url_for('logout'), 0])
make_transient(page_model_dict[session['name']])
page_model_dict[session['name']].id = None
session_sql.add(page_model_dict[session['name']])
session_sql.commit()
update_page_with_random()
return jsonify(page_model_dict[session['name']].get_imgs_list() + [str(user_nclicks_dict[session['name']])])
# Render main page
@app.route("/kernel/")
def kernel_index():
if not 'name' in session or not session['name'] in user_nclicks_dict:
return redirect(url_for('login'))
return render_template('kernel.html')
# Create new user
#
# - Store given user name
# - Create initial kernel for new user
@app.route('/login', methods=['GET', 'POST'])
def login():
global counter
error = None
if request.method == 'POST' and request.form['cont'] == "Continue":
np.save('nclicks_dict.npy', user_nclicks_dict)
np.save('time_dict.npy', user_time_dict)
np.save('code_dict.npy', user_code_dict)
session['name'] = counter
counter += 1
page_model_dict[session['name']] = PageModel()
update_page_with_random()
user_nclicks_dict[session['name']] = 0
user_time_dict[session['name']] = [time.time(), 0]
return redirect(url_for('kernel_index'))
return render_template('login_rand.html', error=error)
# Run
if __name__ == "__main__":
app.run()