Skip to content

Commit 9e39e2c

Browse files
author
Philip Guo
committed
some refactoring
1 parent 0d4acfe commit 9e39e2c

6 files changed

Lines changed: 294 additions & 165 deletions

File tree

‎edu-python-questions.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2222

2323
// UI for online problem sets
2424

25+
// Pre-req: edu-python.js should be imported BEFORE this file
26+
2527

‎edu-python-tutor.js‎

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
3+
Online Python Tutor
4+
Copyright (C) 2010 Philip J. Guo (philip@pgbovine.net)
5+
https://github.com/pgbovine/OnlinePythonTutor/
6+
7+
This program is free software: you can redistribute it and/or modify
8+
it under the terms of the GNU General Public License as published by
9+
the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
20+
*/
21+
22+
// The Online Python Tutor front-end, which calls the cgi-bin/web_exec.py
23+
// back-end with a string representing the user's script POST['user_script']
24+
// and receives a complete execution trace, which it parses and displays to HTML.
25+
26+
// Pre-req: edu-python.js should be imported BEFORE this file
27+
28+
29+
$(document).ready(function() {
30+
eduPythonCommonInit(); // must call this first!
31+
32+
$("#pyOutputPane").hide();
33+
34+
$("#executeBtn").attr('disabled', false);
35+
$("#executeBtn").click(function() {
36+
if (localTesting) {
37+
renderPyCodeOutput($("#pyInput").val());
38+
39+
processTrace(data_test_trace);
40+
41+
$("#pyInputPane").hide();
42+
$("#pyOutputPane").show();
43+
appMode = 'visualize';
44+
}
45+
else {
46+
$('#executeBtn').html("Please wait ... processing your code");
47+
$('#executeBtn').attr('disabled', true);
48+
$("#pyOutputPane").hide();
49+
50+
$.post("cgi-bin/web_exec.py",
51+
{user_script : $("#pyInput").val()},
52+
function(traceData) {
53+
renderPyCodeOutput($("#pyInput").val());
54+
processTrace(traceData);
55+
56+
$("#pyInputPane").hide();
57+
$("#pyOutputPane").show();
58+
appMode = 'visualize';
59+
60+
$('#executeBtn').html("Visualize execution");
61+
$('#executeBtn').attr('disabled', false);
62+
},
63+
"json");
64+
}
65+
});
66+
67+
68+
$("#editBtn").click(function() {
69+
$("#pyInputPane").show();
70+
$("#pyOutputPane").hide();
71+
appMode = 'edit';
72+
});
73+
74+
75+
// canned examples
76+
77+
$("#tutorialExampleLink").click(function() {
78+
$.get("example-code/py_tutorial.txt", function(dat) {$("#pyInput").val(dat);});
79+
return false;
80+
});
81+
82+
$("#strtokExampleLink").click(function() {
83+
$.get("example-code/strtok.txt", function(dat) {$("#pyInput").val(dat);});
84+
return false;
85+
});
86+
87+
$("#fibonacciExampleLink").click(function() {
88+
$.get("example-code/fib.txt", function(dat) {$("#pyInput").val(dat);});
89+
return false;
90+
});
91+
92+
$("#memoFibExampleLink").click(function() {
93+
$.get("example-code/memo_fib.txt", function(dat) {$("#pyInput").val(dat);});
94+
return false;
95+
});
96+
97+
$("#factExampleLink").click(function() {
98+
$.get("example-code/fact.txt", function(dat) {$("#pyInput").val(dat);});
99+
return false;
100+
});
101+
102+
$("#filterExampleLink").click(function() {
103+
$.get("example-code/filter.txt", function(dat) {$("#pyInput").val(dat);});
104+
return false;
105+
});
106+
107+
$("#insSortExampleLink").click(function() {
108+
$.get("example-code/ins_sort.txt", function(dat) {$("#pyInput").val(dat);});
109+
return false;
110+
});
111+
112+
$("#aliasExampleLink").click(function() {
113+
$.get("example-code/aliasing.txt", function(dat) {$("#pyInput").val(dat);});
114+
return false;
115+
});
116+
117+
$("#newtonExampleLink").click(function() {
118+
$.get("example-code/sqrt.txt", function(dat) {$("#pyInput").val(dat);});
119+
return false;
120+
});
121+
122+
$("#oopSmallExampleLink").click(function() {
123+
$.get("example-code/oop_small.txt", function(dat) {$("#pyInput").val(dat);});
124+
return false;
125+
});
126+
127+
$("#mapExampleLink").click(function() {
128+
$.get("example-code/map.txt", function(dat) {$("#pyInput").val(dat);});
129+
return false;
130+
});
131+
132+
$("#oop1ExampleLink").click(function() {
133+
$.get("example-code/oop_1.txt", function(dat) {$("#pyInput").val(dat);});
134+
return false;
135+
});
136+
137+
$("#oop2ExampleLink").click(function() {
138+
$.get("example-code/oop_2.txt", function(dat) {$("#pyInput").val(dat);});
139+
return false;
140+
});
141+
142+
$("#inheritanceExampleLink").click(function() {
143+
$.get("example-code/oop_inherit.txt", function(dat) {$("#pyInput").val(dat);});
144+
return false;
145+
});
146+
147+
$("#sumExampleLink").click(function() {
148+
$.get("example-code/sum.txt", function(dat) {$("#pyInput").val(dat);});
149+
return false;
150+
});
151+
152+
$("#pwGcdLink").click(function() {
153+
$.get("example-code/wentworth_gcd.txt", function(dat) {$("#pyInput").val(dat);});
154+
return false;
155+
});
156+
157+
$("#pwSumListLink").click(function() {
158+
$.get("example-code/wentworth_sumList.txt", function(dat) {$("#pyInput").val(dat);});
159+
return false;
160+
});
161+
162+
$("#towersOfHanoiLink").click(function() {
163+
$.get("example-code/towers_of_hanoi.txt", function(dat) {$("#pyInput").val(dat);});
164+
return false;
165+
});
166+
167+
$("#pwTryFinallyLink").click(function() {
168+
$.get("example-code/wentworth_try_finally.txt", function(dat) {$("#pyInput").val(dat);});
169+
return false;
170+
});
171+
172+
173+
// select an example on start-up:
174+
$("#aliasExampleLink").trigger('click');
175+
});
176+

‎edu-python.js‎

Lines changed: 13 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
2020
*/
2121

22-
// The Online Python Tutor front-end, which calls the cgi-bin/web_exec.py
23-
// back-end with a string representing the user's script POST['user_script']
24-
// and receives a complete execution trace, which it parses and displays to HTML.
22+
// code that is common to all Online Python Tutor pages
2523

2624
var appMode = 'edit'; // 'edit' or 'visualize'
2725

@@ -1093,57 +1091,10 @@ function renderPyCodeOutput(codeStr) {
10931091

10941092
}
10951093

1096-
$(document).ready(function() {
1097-
1098-
$("#pyOutputPane").hide();
1099-
1100-
$("#executeBtn").attr('disabled', false);
1101-
1094+
// initialization function that should be called when the page is loaded
1095+
function eduPythonCommonInit() {
11021096
$("#pyInput").tabby(); // recognize TAB and SHIFT-TAB
11031097

1104-
// disable autogrow for simplicity
1105-
//$("#pyInput").autogrow();
1106-
1107-
$("#executeBtn").click(function() {
1108-
if (localTesting) {
1109-
renderPyCodeOutput($("#pyInput").val());
1110-
1111-
processTrace(data_test_trace);
1112-
1113-
$("#pyInputPane").hide();
1114-
$("#pyOutputPane").show();
1115-
appMode = 'visualize';
1116-
}
1117-
else {
1118-
$('#executeBtn').html("Please wait ... processing your code");
1119-
$('#executeBtn').attr('disabled', true);
1120-
$("#pyOutputPane").hide();
1121-
1122-
$.post("cgi-bin/web_exec.py",
1123-
{user_script : $("#pyInput").val()},
1124-
function(traceData) {
1125-
renderPyCodeOutput($("#pyInput").val());
1126-
processTrace(traceData);
1127-
1128-
$("#pyInputPane").hide();
1129-
$("#pyOutputPane").show();
1130-
appMode = 'visualize';
1131-
1132-
$('#executeBtn').html("Visualize execution");
1133-
$('#executeBtn').attr('disabled', false);
1134-
},
1135-
"json");
1136-
}
1137-
});
1138-
1139-
1140-
$("#editBtn").click(function() {
1141-
$("#pyInputPane").show();
1142-
$("#pyOutputPane").hide();
1143-
appMode = 'edit';
1144-
});
1145-
1146-
11471098
$("#jmpFirstInstr").click(function() {
11481099
curInstr = 0;
11491100
updateOutput();
@@ -1168,103 +1119,11 @@ $(document).ready(function() {
11681119
}
11691120
});
11701121

1171-
1172-
// canned examples
1173-
1174-
$("#tutorialExampleLink").click(function() {
1175-
$.get("example-code/py_tutorial.txt", function(dat) {$("#pyInput").val(dat);});
1176-
return false;
1177-
});
1178-
1179-
$("#strtokExampleLink").click(function() {
1180-
$.get("example-code/strtok.txt", function(dat) {$("#pyInput").val(dat);});
1181-
return false;
1182-
});
1183-
1184-
$("#fibonacciExampleLink").click(function() {
1185-
$.get("example-code/fib.txt", function(dat) {$("#pyInput").val(dat);});
1186-
return false;
1187-
});
1188-
1189-
$("#memoFibExampleLink").click(function() {
1190-
$.get("example-code/memo_fib.txt", function(dat) {$("#pyInput").val(dat);});
1191-
return false;
1192-
});
1193-
1194-
$("#factExampleLink").click(function() {
1195-
$.get("example-code/fact.txt", function(dat) {$("#pyInput").val(dat);});
1196-
return false;
1197-
});
1198-
1199-
$("#filterExampleLink").click(function() {
1200-
$.get("example-code/filter.txt", function(dat) {$("#pyInput").val(dat);});
1201-
return false;
1202-
});
1203-
1204-
$("#insSortExampleLink").click(function() {
1205-
$.get("example-code/ins_sort.txt", function(dat) {$("#pyInput").val(dat);});
1206-
return false;
1207-
});
1208-
1209-
$("#aliasExampleLink").click(function() {
1210-
$.get("example-code/aliasing.txt", function(dat) {$("#pyInput").val(dat);});
1211-
return false;
1212-
});
1213-
1214-
$("#newtonExampleLink").click(function() {
1215-
$.get("example-code/sqrt.txt", function(dat) {$("#pyInput").val(dat);});
1216-
return false;
1217-
});
1218-
1219-
$("#oopSmallExampleLink").click(function() {
1220-
$.get("example-code/oop_small.txt", function(dat) {$("#pyInput").val(dat);});
1221-
return false;
1222-
});
1223-
1224-
$("#mapExampleLink").click(function() {
1225-
$.get("example-code/map.txt", function(dat) {$("#pyInput").val(dat);});
1226-
return false;
1227-
});
1228-
1229-
$("#oop1ExampleLink").click(function() {
1230-
$.get("example-code/oop_1.txt", function(dat) {$("#pyInput").val(dat);});
1231-
return false;
1232-
});
1233-
1234-
$("#oop2ExampleLink").click(function() {
1235-
$.get("example-code/oop_2.txt", function(dat) {$("#pyInput").val(dat);});
1236-
return false;
1237-
});
1238-
1239-
$("#inheritanceExampleLink").click(function() {
1240-
$.get("example-code/oop_inherit.txt", function(dat) {$("#pyInput").val(dat);});
1241-
return false;
1242-
});
1243-
1244-
$("#sumExampleLink").click(function() {
1245-
$.get("example-code/sum.txt", function(dat) {$("#pyInput").val(dat);});
1246-
return false;
1247-
});
1248-
1249-
$("#pwGcdLink").click(function() {
1250-
$.get("example-code/wentworth_gcd.txt", function(dat) {$("#pyInput").val(dat);});
1251-
return false;
1252-
});
1253-
1254-
$("#pwSumListLink").click(function() {
1255-
$.get("example-code/wentworth_sumList.txt", function(dat) {$("#pyInput").val(dat);});
1256-
return false;
1257-
});
1258-
1259-
$("#towersOfHanoiLink").click(function() {
1260-
$.get("example-code/towers_of_hanoi.txt", function(dat) {$("#pyInput").val(dat);});
1261-
return false;
1262-
});
1263-
1264-
$("#pwTryFinallyLink").click(function() {
1265-
$.get("example-code/wentworth_try_finally.txt", function(dat) {$("#pyInput").val(dat);});
1266-
return false;
1267-
});
1122+
// disable controls initially ...
1123+
$("#vcrControls #jmpFirstInstr").attr("disabled", true);
1124+
$("#vcrControls #jmpStepBack").attr("disabled", true);
1125+
$("#vcrControls #jmpStepFwd").attr("disabled", true);
1126+
$("#vcrControls #jmpLastInstr").attr("disabled", true);
12681127

12691128

12701129
// set some sensible jsPlumb defaults
@@ -1279,17 +1138,6 @@ $(document).ready(function() {
12791138
jsPlumb.Defaults.HoverPaintStyle = {lineWidth:2, strokeStyle: pinkish};
12801139

12811140

1282-
// select an example on start-up:
1283-
$("#aliasExampleLink").trigger('click');
1284-
1285-
1286-
// disable controls initially ...
1287-
$("#vcrControls #jmpFirstInstr").attr("disabled", true);
1288-
$("#vcrControls #jmpStepBack").attr("disabled", true);
1289-
$("#vcrControls #jmpStepFwd").attr("disabled", true);
1290-
$("#vcrControls #jmpLastInstr").attr("disabled", true);
1291-
1292-
12931141
// set keyboard event listeners ...
12941142
$(document).keydown(function(k) {
12951143
// ONLY capture keys if we're in 'visualize code' mode:
@@ -1311,9 +1159,11 @@ $(document).ready(function() {
13111159

13121160
// redraw everything on window resize so that connectors are in the
13131161
// right place
1162+
// TODO: can be SLOW on older browsers!!!
13141163
$(window).resize(function() {
1315-
updateOutput();
1164+
if (appMode == 'visualize') {
1165+
updateOutput();
1166+
}
13161167
});
1317-
1318-
});
1168+
}
13191169

0 commit comments

Comments
 (0)