-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTeacherHandler.py
More file actions
210 lines (157 loc) · 5.59 KB
/
TeacherHandler.py
File metadata and controls
210 lines (157 loc) · 5.59 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import urllib2
import json
# import numpy as np
import cStringIO
# import webapp2
# import jinja2
# import os
# os.environ["MATPLOTLIBDATA"] = os.getcwdu()
# os.environ["MPLCONFIGDIR"] = os.getcwdu()
# import subprocess
# def no_popen(*args, **kwargs): raise OSError("forbjudet")
# subprocess.Popen = no_popen
# subprocess.PIPE = None
# subprocess.STDOUT = None
# try:
# import matplotlib.pyplot as plt
# except:
# print "trouble"
import logging
try:
import webapp2
except:
logging.exception("no webapp")
import pprint
import os
import StringIO
os.environ["MATPLOTLIBDATA"] = os.getcwdu()
os.environ["MPLCONFIGDIR"] = os.getcwdu()
import subprocess
def no_popen(*args, **kwargs): raise OSError("forbjudet")
subprocess.Popen = no_popen
subprocess.PIPE = None
subprocess.STDOUT = None
logging.warn("E: %s" % pprint.pformat(os.environ))
try:
import numpy, matplotlib, matplotlib.pyplot as plt
except:
logging.exception("trouble")
print "trouble"
from google.appengine.ext.webapp import template
STATUS_KEY = 'status'
DESC_KEY = 'description'
STATUS_ERROR = 'error'
STATUS_OK = 'ok'
REPLY_DICT = {STATUS_KEY: "", DESC_KEY: ""}
path = os.path.join(os.path.dirname(__file__), 'templates/submitStudent.html')
def make_plot(data, labels, title):
assert(len(data) == len(labels))
fig, ax = plt.subplots()
ind = numpy.arange(1, len(data)+1)
# show the figure, but do not block
# plt.show(block=False)
# q1, q2, q3, q4 = plt.bar(ind, data)
# q1.set_facecolor('r')
# q2.set_facecolor('r')
# q3.set_facecolor('r')
# q4.set_facecolor('r')
plt.bar(ind, data)
ax.set_xticks(ind)
ax.set_xticklabels(labels)
# ax.set_ylabel('Percent usage')
ax.set_title(title)
sio = cStringIO.StringIO()
plt.savefig(sio, format="png")
# plt.show()
return sio
# print make_plot([2,4,6,3], ['Q1','Q2','Q3', 'Q4'], 'quarters')
class MainForm(webapp2.RequestHandler):
def get(self):
self.response.out.write(template.render(path, {}))
class DisplayQuaterAnalysis(webapp2.RequestHandler):
SUBJECT_LIST = ['mathematics', 'computer', 'litrature']
def get(self, student_id):
url = "http://" + str(self.request.host) + "/statistics/studentperquarter/" + str(student_id)
print "URL: " + url
response = urllib2.urlopen(url, timeout=100)
data = json.load(response)
if data['status'] == 'ok':
desc = data['description']
self.response.write("""<html><body>""")
vals = []
for q in desc.keys():
vals.append(desc[q])
Q1P = make_plot(vals, desc.keys(), "Quater Analysis for Student: " + student_id)
img_b64 = Q1P.getvalue().encode("base64").strip()
self.response.write("<img src='data:image/png;base64,%s'/><br>" % img_b64)
self.response.write("""</body> </html>""")
else:
REPLY_DICT[STATUS_KEY] = STATUS_ERROR
REPLY_DICT[DESC_KEY] = data['description']
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(REPLY_DICT))
class DisplaySubjectAnalysis(webapp2.RequestHandler):
SUBJECT_LIST = ['mathematics', 'computer', 'litrature']
def get(self, subject):
url = "http://" + str(self.request.host) + "/statistics/quarter/" + str(subject)
print "URL: " + url
response = urllib2.urlopen(url, timeout=100)
data = json.load(response)
if data['status'] == 'ok':
desc = data['description']
self.response.write("""<html><body>""")
vals = []
for q in desc.keys():
vals.append(desc[q])
Q1P = make_plot(vals, desc.keys(), self.SUBJECT_LIST[int(subject)])
img_b64 = Q1P.getvalue().encode("base64").strip()
self.response.write("<img src='data:image/png;base64,%s'/><br>" % img_b64)
self.response.write("""</body> </html>""")
else:
REPLY_DICT[STATUS_KEY] = STATUS_ERROR
REPLY_DICT[DESC_KEY] = data['description']
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(REPLY_DICT))
class DisplaySubjectQuaterAnalysis(webapp2.RequestHandler):
def get(self):
url = "http://" + str(self.request.host) + "/statistics/quarter"
print "URL: " + url
response = urllib2.urlopen(url, timeout=100)
data = json.load(response)
if data['status'] == 'ok':
desc = data['description']
self.response.write("""<html><body>""")
vals = []
Q1 = desc['Q1']
for q in Q1.keys():
vals.append(Q1[q])
Q1P = make_plot(vals, Q1.keys(), "Quater1")
img_b64 = Q1P.getvalue().encode("base64").strip()
self.response.write("<img src='data:image/png;base64,%s'/><br>" % img_b64)
vals = []
Q2 = desc['Q2']
for q in Q2.keys():
vals.append(Q2[q])
Q2P = make_plot(vals, Q2.keys(), "Quater2")
img_b64 = Q2P.getvalue().encode("base64").strip()
self.response.write("<img src='data:image/png;base64,%s'/><br>" % img_b64)
vals = []
Q3 = desc['Q3']
for q in Q3.keys():
vals.append(Q3[q])
Q3P = make_plot(vals, Q3.keys(), "Quater3")
img_b64 = Q3P.getvalue().encode("base64").strip()
self.response.write("<img src='data:image/png;base64,%s'/><br>" % img_b64)
vals = []
Q4 = desc['Q4']
for q in Q4.keys():
vals.append(Q4[q])
Q4P = make_plot(vals, Q4.keys(), "Quater4")
img_b64 = Q4P.getvalue().encode("base64").strip()
self.response.write("<img src='data:image/png;base64,%s'/><br>" % img_b64)
self.response.write("""</body> </html>""")
else:
REPLY_DICT[STATUS_KEY] = STATUS_ERROR
REPLY_DICT[DESC_KEY] = data['description']
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(REPLY_DICT))