|
| 1 | +/* |
| 2 | +
|
| 3 | +Online Python Tutor |
| 4 | +https://github.com/pgbovine/OnlinePythonTutor/ |
| 5 | +
|
| 6 | +Copyright (C) 2010-2012 Philip J. Guo (philip@pgbovine.net) |
| 7 | +
|
| 8 | +Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | +copy of this software and associated documentation files (the |
| 10 | +"Software"), to deal in the Software without restriction, including |
| 11 | +without limitation the rights to use, copy, modify, merge, publish, |
| 12 | +distribute, sublicense, and/or sell copies of the Software, and to |
| 13 | +permit persons to whom the Software is furnished to do so, subject to |
| 14 | +the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included |
| 17 | +in all copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 20 | +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 | +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 23 | +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 24 | +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 | +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | +
|
| 27 | +*/ |
| 28 | + |
| 29 | + |
| 30 | +// Pre-reqs: pytutor.js and jquery.ba-bbq.min.js should be imported BEFORE this file |
| 31 | + |
| 32 | + |
| 33 | +// backend scripts to execute (Python 2 and 3 variants, if available) |
| 34 | +//var python2_backend_script = 'web_exec_py2.py'; |
| 35 | +//var python3_backend_script = 'web_exec_py3.py'; |
| 36 | + |
| 37 | +// uncomment below if you're running on Google App Engine using the built-in app.yaml |
| 38 | +var python2_backend_script = 'exec'; |
| 39 | +var python3_backend_script = null; |
| 40 | + |
| 41 | + |
| 42 | +var myVisualizer = null; // singleton ExecutionVisualizer instance |
| 43 | + |
| 44 | + |
| 45 | +$(document).ready(function() { |
| 46 | + |
| 47 | + var preseededCode = $.bbq.getState('code'); |
| 48 | + var cumulativeState = $.bbq.getState('cumulative'); |
| 49 | + var pyState = $.bbq.getState('py'); |
| 50 | + |
| 51 | + var preseededCurInstr = Number($.bbq.getState('curInstr')); |
| 52 | + if (!preseededCurInstr) { |
| 53 | + preseededCurInstr = 0; |
| 54 | + } |
| 55 | + |
| 56 | + // TODO: add more options |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + var backend_script = null; |
| 61 | + if (pyState == '2') { |
| 62 | + backend_script = python2_backend_script; |
| 63 | + } |
| 64 | + else if (pyState == '3') { |
| 65 | + backend_script = python3_backend_script; |
| 66 | + } |
| 67 | + |
| 68 | + if (!backend_script) { |
| 69 | + alert('Error: This server is not configured to run Python ' + $('#pythonVersionSelector').val()); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + $.get(backend_script, |
| 75 | + {user_script : preseededCode, cumulative_mode: cumulativeState}, |
| 76 | + function(dataFromBackend) { |
| 77 | + var trace = dataFromBackend.trace; |
| 78 | + |
| 79 | + // don't enter visualize mode if there are killer errors: |
| 80 | + if (!trace || |
| 81 | + (trace.length == 0) || |
| 82 | + (trace[trace.length - 1].event == 'uncaught_exception')) { |
| 83 | + |
| 84 | + if (trace.length == 1) { |
| 85 | + alert(trace[0].exception_msg); |
| 86 | + } |
| 87 | + else if (trace[trace.length - 1].exception_msg) { |
| 88 | + alert(trace[trace.length - 1].exception_msg); |
| 89 | + } |
| 90 | + else { |
| 91 | + alert("Whoa, unknown error! Reload to try again, or report a bug to philip@pgbovine.net\n\n(Click the 'Generate URL' button to include a unique URL in your email bug report.)"); |
| 92 | + } |
| 93 | + } |
| 94 | + else { |
| 95 | + var startingInstruction = 0; |
| 96 | + |
| 97 | + // only do this at most ONCE, and then clear out preseededCurInstr |
| 98 | + if (preseededCurInstr && preseededCurInstr < trace.length) { // NOP anyways if preseededCurInstr is 0 |
| 99 | + startingInstruction = preseededCurInstr; |
| 100 | + } |
| 101 | + |
| 102 | + myVisualizer = new ExecutionVisualizer('vizDiv', |
| 103 | + dataFromBackend, |
| 104 | + {startingInstruction: preseededCurInstr, |
| 105 | + embeddedMode: true, |
| 106 | + editCodeBaseURL: 'http://pythontutor.com/visualize.html', |
| 107 | + }); |
| 108 | + } |
| 109 | + }, |
| 110 | + "json"); |
| 111 | + |
| 112 | + |
| 113 | + // log a generic AJAX error handler |
| 114 | + $(document).ajaxError(function() { |
| 115 | + alert("Online Python Tutor server error (possibly due to memory/resource overload)."); |
| 116 | + }); |
| 117 | + |
| 118 | + |
| 119 | + // redraw connector arrows on window resize |
| 120 | + $(window).resize(function() { |
| 121 | + if (appMode == 'display') { |
| 122 | + myVisualizer.redrawConnectors(); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | +}); |
| 127 | + |
0 commit comments