Skip to content

Commit f7dd82f

Browse files
author
Philip Guo
committed
moar MAJOR refactoringz
1 parent 3f91dea commit f7dd82f

5 files changed

Lines changed: 42 additions & 43 deletions

File tree

‎PyTutorGAE/backend_test.py‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎PyTutorGAE/js/opt-frontend.js‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,16 @@ $(document).ready(function() {
136136

137137
$.get("exec",
138138
{user_script : pyInputCodeMirror.getValue()},
139-
function(traceData) {
139+
function(dataFromBackend) {
140+
var trace = dataFromBackend.trace;
141+
140142
// don't enter visualize mode if there are killer errors:
141-
if (!traceData ||
142-
(traceData.length == 0) ||
143-
((traceData.length == 1) && traceData[0].event == 'uncaught_exception')) {
143+
if (!trace ||
144+
(trace.length == 0) ||
145+
((trace.length == 1) && trace[0].event == 'uncaught_exception')) {
144146

145-
if (traceData.length > 0) {
146-
var errorLineNo = traceData[0].line - 1; /* CodeMirror lines are zero-indexed */
147+
if (trace.length > 0) {
148+
var errorLineNo = trace[0].line - 1; /* CodeMirror lines are zero-indexed */
147149
if (errorLineNo !== undefined) {
148150
// highlight the faulting line in pyInputCodeMirror
149151
pyInputCodeMirror.focus();
@@ -156,7 +158,7 @@ $(document).ready(function() {
156158
});
157159
}
158160

159-
alert(traceData[0].exception_msg);
161+
alert(trace[0].exception_msg);
160162
}
161163
else {
162164
alert("Whoa, unknown error! Please reload and try again.");
@@ -169,12 +171,12 @@ $(document).ready(function() {
169171
var startingInstruction = 0;
170172

171173
// only do this at most ONCE, and then clear out preseededCurInstr
172-
if (preseededCurInstr && preseededCurInstr < traceData.length) { // NOP anyways if preseededCurInstr is 0
174+
if (preseededCurInstr && preseededCurInstr < trace.length) { // NOP anyways if preseededCurInstr is 0
173175
startingInstruction = preseededCurInstr;
174176
preseededCurInstr = null;
175177
}
176178

177-
myVisualizer = new ExecutionVisualizer(pyInputCodeMirror.getValue(), traceData, startingInstruction, 'pyOutputPane');
179+
myVisualizer = new ExecutionVisualizer(dataFromBackend, startingInstruction, 'pyOutputPane');
178180

179181
$.bbq.pushState({ mode: 'visualize' }, 2 /* completely override other hash strings to keep URL clean */);
180182
}

‎PyTutorGAE/js/pytutor.js‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,20 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2929

3030
var curVisualizerID = 1; // global to uniquely identify each ExecutionVisualizer instance
3131

32-
function ExecutionVisualizer(inputCode, traceData, startingInstruction, domRootID) {
33-
this.curInputCode = inputCode;
34-
this.curTrace = traceData;
35-
this.curInstr = startingInstruction;
32+
// domRootID is the string ID of the root element where to render this instance
33+
// dat is data returned by the Python Tutor backend consisting of two fields:
34+
// code - string of executed code
35+
// trace - a full execution trace
36+
// startingInstruction is (optional) the execution point to display upon rendering (one-indexed)
37+
function ExecutionVisualizer(domRootID, dat, startingInstruction /* optional */) {
38+
this.curInputCode = dat.code;
39+
this.curTrace = dat.trace;
40+
41+
this.curInstr = 0;
42+
43+
if (startingInstruction) {
44+
this.curInstr = (startingInstruction - 1); // zero-indexed
45+
}
3646

3747
this.visualizerID = curVisualizerID;
3848
curVisualizerID++;
@@ -1103,9 +1113,7 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
11031113
// (the format is: '<this.visualizerID>__heap_object_<id>')
11041114
//
11051115
// The reason we need to prepend this.visualizerID is because jsPlumb needs
1106-
// GLOBALLY UNIQUE IDs for use as connector endpoints. Actually this is no
1107-
// longer true since we can use this.jsPlumbInstance, but still it's nice
1108-
// to have unique div IDs
1116+
// GLOBALLY UNIQUE IDs for use as connector endpoints.
11091117
var connectionEndpointIDs = d3.map();
11101118
var heapConnectionEndpointIDs = d3.map(); // subset of connectionEndpointIDs for heap->heap connections
11111119

@@ -1427,10 +1435,10 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
14271435
// render all global variables IN THE ORDER they were created by the program,
14281436
// in order to ensure continuity:
14291437
if (curEntry.ordered_globals.length > 0) {
1430-
this.domRoot.find("#stack").append('<div class="stackFrame" id="globals"><div id="globals_header" class="stackFrameHeader">Global variables</div></div>');
1431-
this.domRoot.find("#stack #globals").append('<table class="stackFrameVarTable" id="global_table"></table>');
1438+
this.domRoot.find("#stack").append('<div class="stackFrame" id="' + myViz.visualizerID + '__globals"><div id="' + myViz.visualizerID + '__globals_header" class="stackFrameHeader">Global variables</div></div>');
1439+
this.domRoot.find('#stack #' + myViz.visualizerID + '__globals').append('<table class="stackFrameVarTable" id="' + myViz.visualizerID + '__global_table"></table>');
14321440

1433-
var tbl = this.domRoot.find("#global_table");
1441+
var tbl = this.domRoot.find('#' + myViz.visualizerID + '__global_table');
14341442

14351443
$.each(curEntry.ordered_globals, function(i, varname) {
14361444
var val = curEntry.globals[varname];
@@ -1449,7 +1457,7 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
14491457

14501458
// make sure varname doesn't contain any weird
14511459
// characters that are illegal for CSS ID's ...
1452-
var varDivID = 'global__' + varnameToCssID(varname);
1460+
var varDivID = myViz.visualizerID + '__global__' + varnameToCssID(varname);
14531461
curTr.find("td.stackFrameValue").append('<div id="' + varDivID + '">&nbsp;</div>');
14541462

14551463
assert(!connectionEndpointIDs.has(varDivID));
@@ -1483,12 +1491,12 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
14831491
var divClass, divID, headerDivID;
14841492
if (is_zombie) {
14851493
divClass = 'zombieStackFrame';
1486-
divID = "zombie_stack" + ind;
1494+
divID = myViz.visualizerID + "__zombie_stack" + ind;
14871495
headerDivID = "zombie_stack_header" + ind;
14881496
}
14891497
else {
14901498
divClass = 'stackFrame';
1491-
divID = "stack" + ind;
1499+
divID = myViz.visualizerID + "__stack" + ind;
14921500
headerDivID = "stack_header" + ind;
14931501
}
14941502

@@ -1605,13 +1613,13 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
16051613
var frame_already_highlighted = false;
16061614
$.each(curEntry.stack_to_render, function(i, e) {
16071615
if (e.is_highlighted) {
1608-
highlight_frame('stack' + i);
1616+
highlight_frame(myViz.visualizerID + '__stack' + i);
16091617
frame_already_highlighted = true;
16101618
}
16111619
});
16121620

16131621
if (!frame_already_highlighted) {
1614-
highlight_frame('globals');
1622+
highlight_frame(myViz.visualizerID + '__globals');
16151623
}
16161624

16171625
}

‎PyTutorGAE/pg_logger.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ def __init__(self, finalizer_func):
110110
# execution, or else canonical small IDs won't be consistent.
111111
self.encoder = pg_encoder.ObjectEncoder()
112112

113+
self.executed_script = None # Python script to be executed!
114+
113115

114116
# Returns the (lexical) parent frame of the function that was called
115117
# to create the stack frame 'frame'.
@@ -452,6 +454,8 @@ def create_encoded_stack_entry(cur_frame):
452454

453455

454456
def _runscript(self, script_str):
457+
self.executed_script = script_str
458+
455459
# When bdb sets tracing, a number of call and line events happens
456460
# BEFORE debugger even reaches user's code (and the exact sequence of
457461
# events depends on python version). So we take special measures to
@@ -542,7 +546,7 @@ def finalize(self):
542546

543547
#for e in self.trace: print >> sys.stderr, e
544548

545-
self.finalizer_func(self.trace)
549+
self.finalizer_func(self.executed_script, self.trace)
546550

547551

548552

‎PyTutorGAE/pythontutor.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ def get(self):
5050

5151
class ExecScript(webapp2.RequestHandler):
5252

53-
def json_finalizer(self, output_lst):
54-
json_output = json.dumps(output_lst, indent=None) # use indent=None for most compact repr
53+
def json_finalizer(self, input_code, output_trace):
54+
ret = dict(code=input_code, trace=output_trace)
55+
json_output = json.dumps(ret, indent=None) # use indent=None for most compact repr
5556
self.response.out.write(json_output)
5657

5758
def get(self):
5859
self.response.headers['Content-Type'] = 'application/json'
5960
self.response.headers['Cache-Control'] = 'no-cache'
6061
pg_logger.exec_script_str(self.request.get('user_script'), self.json_finalizer)
61-
#pg_logger.exec_script_str(TEST_STR, self.json_finalizer)
6262

6363

6464
app = webapp2.WSGIApplication([('/', TutorPage),

0 commit comments

Comments
 (0)