forked from blockpy-edu/blockpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplay.html
382 lines (355 loc) · 17.6 KB
/
replay.html
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>BlockPy Replay</title>
<link rel="stylesheet" href="libs/bootstrap.min.css">
<link rel="stylesheet" href="libs/codemirror/codemirror.css">
<link rel="stylesheet" href="libs/font-awesome.min.css">
<link rel="stylesheet" href="libs/summernote/summernote.css">
<link rel="stylesheet" href="libs/multi-select.css">
<link rel="stylesheet" href="src/blockpy.css">
<!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css">-->
<!-- JQuery, D3, Math.js, Bootstrap -->
<script type="text/javascript" src="libs/jquery.js"></script>
<script type="text/javascript" src="libs/jquery-ui.min.js"></script>
<script type="text/javascript" src="libs/jquery.hotkeys.js"></script>
<script type="text/javascript" src="libs/jquery.multi-select.js"></script>
<script type="text/javascript" src="libs/d3.min.js"></script>
<script type="text/javascript" src="libs/math.0.19.0.min.js"></script>
<script type="text/javascript" src="libs/bootstrap.min.js"></script>
<script type="text/javascript" src="libs/bootstrap-wysiwyg.js"></script>
<script type="text/javascript" src="libs/mindmup-editabletable.js"></script>
<script type="text/javascript" src="libs/codemirror/codemirror.js"></script>
<script type="text/javascript" src="libs/codemirror/python.js"></script>
<script type="text/javascript" src="libs/codemirror/htmlmixed.js"></script>
<script type="text/javascript" src="libs/codemirror/xml.js"></script>
<script type="text/javascript" src="libs/knockout-3.4.0.js"></script>
<!-- Summernote, a rich text editor -->
<script type="text/javascript" src="libs/summernote/summernote.min.js"></script>
<script type="text/javascript" src="libs/summernote/summernote-ext-hint.js"></script>
<script type="text/javascript" src="libs/summernote/summernote-ext-video.js"></script>
<!-- Blockly -->
<script type="text/javascript" src="blockly/blockly_uncompressed.js"></script>
<script type="text/javascript" src="blockly/blocks_compressed.js"></script>
<script type="text/javascript" src="blockly/python_compressed.js"></script>
<script type="text/javascript" src="blockly/pseudo_compressed.js"></script>
<script type="text/javascript" src="blockly/javascript_compressed.js"></script>
<script type="text/javascript" src="blockly/msg/js/en.js"></script>
<!-- Skulpt -->
<script type="text/javascript" src="skulpt/dist/skulpt.min.js"></script>
<script type="text/javascript" src="skulpt/dist/skulpt-stdlib.js"></script>
<!-- Source Code -->
<script type="text/javascript" src="src/utilities.js"></script>
<script type="text/javascript" src="src/python_errors.js"></script>
<script type="text/javascript" src="src/ast_node_visitor.js"></script>
<script type="text/javascript" src="src/abstract_interpreter.js"></script>
<script type="text/javascript" src="src/abstract_interpreter_definitions.js"></script>
<script type="text/javascript" src="src/abstract_interpreter_tests.js"></script>
<script type="text/javascript" src="src/python_to_blockly.js"></script>
<script type="text/javascript" src="src/imported.js"></script>
<script type="text/javascript" src="src/blockly_blocks/class.js"></script>
<script type="text/javascript" src="src/blockly_blocks/comment.js"></script>
<script type="text/javascript" src="src/blockly_blocks/comprehensions.js"></script>
<script type="text/javascript" src="src/blockly_blocks/dict.js"></script>
<script type="text/javascript" src="src/blockly_blocks/if.js"></script>
<script type="text/javascript" src="src/blockly_blocks/io.js"></script>
<script type="text/javascript" src="src/blockly_blocks/lists.js"></script>
<script type="text/javascript" src="src/blockly_blocks/sets.js"></script>
<script type="text/javascript" src="src/blockly_blocks/loops.js"></script>
<script type="text/javascript" src="src/blockly_blocks/parking.js"></script>
<script type="text/javascript" src="src/blockly_blocks/tuple.js"></script>
<script type="text/javascript" src="src/blockly_blocks/turtles.js"></script>
<script type="text/javascript" src="src/blockly_blocks/text.js"></script>
<script type="text/javascript" src="src/dialog.js"></script>
<script type="text/javascript" src="src/storage.js"></script>
<script type="text/javascript" src="src/printer.js"></script>
<script type="text/javascript" src="src/interface.js"></script>
<script type="text/javascript" src="src/server.js"></script>
<script type="text/javascript" src="src/english.js"></script>
<script type="text/javascript" src="src/corgis.js"></script>
<script type="text/javascript" src="src/history.js"></script>
<script type="text/javascript" src="src/presentation.js"></script>
<script type="text/javascript" src="src/editor.js"></script>
<script type="text/javascript" src="src/feedback.js"></script>
<script type="text/javascript" src="src/toolbar.js"></script>
<script type="text/javascript" src="src/treeMatching.js"></script>
<script type="text/javascript" src="src/sk_mod_instructor_extended.js"></script>
<script type="text/javascript" src="src/sk_mod_instructor.js"></script>
<script type="text/javascript" src="src/engine.js"></script>
<script type="text/javascript" src="src/main.js"></script>
<!--<script type="text/javascript" src="test_corgis/blockpy/tate/tate_blockly.js"></script>
<script type="text/javascript" src="test_corgis/blockpy/tate/tate_skulpt.js"></script>
<script type="text/javascript" src="test_corgis/blockpy/tate/tate_dataset.js"></script>-->
<style>
</style>
<script>
$(document).ready(function() {
blockpy = new BlockPy({
'blocklyPath': "blockly/",
'attachmentPoint': document.getElementById('blockpy-div'),
'instructor': true,
'developer': true,
'editor': 'Text',
'urls': {
'import_datasets': 'https://think.cs.vt.edu/blockpy/load_corgis/',
//'walk_old_code': 'http://localhost:8000/walk_code/',
//'log_event': 'http://localhost:8000/save_events/'
/*'save_code': 'http://localhost:8000/save_code/',
'save_success': 'http://localhost:8000/submit_grade/',
'log_event': 'http://localhost:8000/save_events/'*/
}
});
blockpy.setAssignment(
// settings
{
'editor': 'Text',
'read_only': false,
'disable_semantic_errors': false,
},
// assignment
{
'introduction': "Welcome to BlockPy. Try running the code below.",
'name': "#1.2",
'give_feedback': 'import instructor_histogram as ins_hist\nimport instructor_append as ins_app\nimport instructor_iteration as ins_iter\nimport instructor_filter as ins_filt\nimport iteration_context as ins_cont\nins_app.missing_append_in_iteration()\nins_app.wrong_not_append_to_list()\nins_app.missing_append_list_initialization()\nins_app.wrong_append_list_initiatization()\nins_app.append_list_wrong_slot()\n',
'parsons': false,
'starting_code': '',
'importable': true,
'initial_view': 'Blocks',
'modules': {
'added': ['Functions', 'Data - Weather'],
'removed': []
}
},
// programs
{
'__main__': 'import matplotlib.pyplot as plt\nquakes = [1,2,3,4]\nquakes_in_miles = []\nfor quake in quakes:\n quakes_in_miles.append(quake * 0.62)\nplt.hist(quakes_in_miles)\nplt.xlabel("Depth in Miles")\nplt.ylabel("Number of Earthquakes")\nplt.title("Distribution of Depth in Miles of Earthquakes")\nplt.show()\n'
}
)
function inIframe () {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
if (inIframe()) {
$("#blockpy-container").width("100%");
}
function updatePauseButton(){
if(global_isPaused){
document.getElementById("pause-button").innerHTML = "Play";
}else{
document.getElementById("pause-button").innerHTML = "Pause";
}
};
var global_isPaused = false;
const FORWARD_SCRUB = "forward";
const BACKWARD_SCRUB = "backward";
const STOP_SCRUB = "stop"
var global_scrubing = STOP_SCRUB;
var speed = 50;
document.onkeydown = navigationStroke;
function pauseEvent(){
global_isPaused = !global_isPaused;
updatePauseButton();
}
function forwardEvent(){
global_isPaused = true;
global_scrubing = FORWARD_SCRUB;
updatePauseButton();
}
function backEvent(){
global_isPaused = true;
global_scrubing = BACKWARD_SCRUB;
updatePauseButton();
}
function navigationStroke(key){
var KeyNavigationEnabled = document.getElementById("myCheck").checked;
if(KeyNavigationEnabled == true){
key = key || window.event;
if (key.keyCode == '80') {
// p is pressed
pauseEvent();
}
else if (key.keyCode == '37') {
// left arrow
backEvent();
}
else if (key.keyCode == '39') {
// right arrow
forwardEvent();
}
}
}
$('#speed-slider').change(function() {
var sliderValue = document.getElementById("speed-slider").value;
speed = 100 - document.getElementById("speed-slider").value;
document.getElementById("sliderValue").innerHTML = sliderValue;
});
$('#pause-button').click(function() {
global_isPaused = !global_isPaused;
updatePauseButton();
});
$('#forward-button').click(function() {
forwardEvent();
});
$('#backward-button').click(function() {
backEvent();
});
function handleCodeEvents(record){
switch (record.action.toLowerCase()) {
case "set":
blockpy.model.programs['__main__'](record.body);
blockpy.components.editor.updateBlocks();
break;
}
}
function executeLastCodeEvent(pastRecords){
var codeEvent = null;
for(i = pastRecords.length-1; i >= 0; i--){
var rawRecord = pastRecords[i];
record = JSON.parse(rawRecord);
if(record.event.toLowerCase() == "code"){
codeEvent = record;
break;
}
}
if(codeEvent != null){
handleCodeEvents(codeEvent);
}
}
function handleFeedbackEvents(record){
var messages = record.body.split("\n");
var detailError = messages[0];
var bodyMessage = "";
var regex = /line (\d{1,})/i;
for(i = 1; i < messages.length; i++){
bodyMessage += messages[i];
}
var results = regex.exec(bodyMessage);
var line = results != null && results.length > 1 ? results[1] : null;
try{
switch (record.action.toLowerCase()) {
case "success":
blockpy.components.feedback.complete(detailError, bodyMessage, line);
break;
case "editor error":
blockpy.components.feedback.editorError(detailError, bodyMessage, line);
break;
case "no errors":
blockpy.components.feedback.noErrors(detailError, bodyMessage, line);
break;
case "syntax error":
blockpy.components.feedback.syntaxError(detailError, bodyMessage, line);
break;
case "semantic error":
blockpy.components.feedback.semanticError(detailError, bodyMessage, line);
break;
case "internal error":
blockpy.components.feedback.internalError(detailError, bodyMessage, line);
break;
case "instructor feedback":
blockpy.components.feedback.instructorFeedback(detailError, bodyMessage, line);
break;
case "empty program":
blockpy.components.feedback.emptyProgram(detailError, bodyMessage, line);
break;
default:
console.log(record.action.toLowerCase());
break;
// ...
}
}catch(err){
console.error(err);
}
};
$('#replay-button').click(function() {
var records = $('#replay-data').val().split(/\r?\n/);
var previousTime = null;
var pastRecords = [];
var goingBackwards = false;
var clearRunFlag = false;
function processAnother(recordsLeft) {
var gap = 100;
var moveForward = true;
if(global_isPaused){
switch(global_scrubing){
case BACKWARD_SCRUB:
if(pastRecords.length > 1){
blockpy.components.feedback.clearEditorErrors();
blockpy.components.feedback.clear();
recordsLeft.unshift(pastRecords.pop());
recordsLeft.unshift(pastRecords.pop());
goingBackwards = true;
//TODO: Handle when going backwards to a feedback event
}else{
moveForward = false;
}
//NOTE: This cascades into FORWARD_SCRUB
case FORWARD_SCRUB:
global_scrubing = STOP_SCRUB;
previousTime = null;
break;
default:
moveForward = false;
break;
};
}
if(moveForward && recordsLeft.length > 0){
blockpy.components.feedback.clearEditorErrors();
blockpy.components.feedback.clear();
var rawRecord = recordsLeft.shift();
var record = JSON.parse(rawRecord);
pastRecords.push(rawRecord);
switch (record.event.toLowerCase()) {
case "code":
handleCodeEvents(record);
break;
case "feedback":
if(goingBackwards){
executeLastCodeEvent(pastRecords);
}
handleFeedbackEvents(record);
break;
}
var cur_time = new Date(record.time*1000);
if(!global_isPaused){
gap = previousTime != null ? record.time - previousTime : 0;
gap = (gap > 2000 ? 2000 : gap)*speed;
previousTime = record.time;
//var cur_time = new Date(record.time*1000);
//var cur_time = record.time;
//cur_time = cur_time.getHours();
console.log(gap + ": " + cur_time.getHours() + ":" + cur_time.getMinutes() + ":" + cur_time.getSeconds() + ":" + cur_time.getMilliseconds() + "/" + record.time + ", ass: " + record.assignment + ", user: " + record.user);
}else{
console.log("freeze frame" + ": " + cur_time.getHours() + ":" + cur_time.getMinutes() + ":" + cur_time.getSeconds() + ":" + cur_time.getMilliseconds() + "/" + record.time + ", ass:" + record.assignment + ", user: " + record.user);
}
//blockpy.components.editor.getPngFromBlocks
}
if(!clearRunFlag){
setTimeout(function() {processAnother(recordsLeft)}, gap);
}
}
processAnother(records);
});
});
</script>
</head>
<body>
<div style="width:900px; margin:0 auto;" id='blockpy-container'> <!-- 900px -->
<!-- Replay stuff -->
<textarea class="form-control" rows="3" id='replay-data'></textarea>
<button type='button' class='btn btn-submit' id='replay-button'>Replay</button>
<button type='button' class='btn btn-submit' id='pause-button'>Pause</button>
<button type='button' class='btn btn-submit' id='backward-button'>Backward</button>
<button type='button' class='btn btn-submit' id='forward-button'>Forward</button>
KeyNavigation: <input type="checkbox" id="myCheck" checked="check">
<input type="range" id="speed-slider" value="50" min="1" max="100">
<p id="sliderValue" value="50"></p>
<div id="blockpy-div" style='height:100%'></div>
</div>
<script>
</script>
</body>
</html>