|
| 1 | +/* |
| 2 | +
|
| 3 | +Online Python Tutor |
| 4 | +https://github.com/pgbovine/OnlinePythonTutor/ |
| 5 | +
|
| 6 | +Copyright (C) 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 | +// OPT live programming prototype started on 2016-05-30 |
| 30 | + |
| 31 | +/* TODOs: |
| 32 | +
|
| 33 | +- display run-time exceptions properly -- right now display-mode code is |
| 34 | +hidden, so no exceptions displayed either |
| 35 | +
|
| 36 | +- when there's a syntax error, the ace code editor snaps cursor back to |
| 37 | + beginning of column at the marked line, which is annoying |
| 38 | +
|
| 39 | +*/ |
| 40 | + |
| 41 | +// Pre-reqs: |
| 42 | +// - pytutor.js |
| 43 | +// - jquery.ba-bbq.min.js |
| 44 | +// - jquery.ba-dotimeout.min.js // for event debouncing: http://benalman.com/code/projects/jquery-dotimeout/examples/debouncing/ |
| 45 | +// - opt-frontend-common.js |
| 46 | +// should all be imported BEFORE this file |
| 47 | + |
| 48 | +var originFrontendJsFile = 'opt-live.js'; |
| 49 | + |
| 50 | +function optliveFinishSuccessfulExecution() { |
| 51 | + assert(myVisualizer); |
| 52 | + $("#pyOutputPane").show(); |
| 53 | + |
| 54 | + doneExecutingCode(); |
| 55 | + |
| 56 | + // do this AFTER making #pyOutputPane visible, or else |
| 57 | + // jsPlumb connectors won't render properly |
| 58 | + myVisualizer.updateOutput(); |
| 59 | +} |
| 60 | + |
| 61 | +// override the one in opt-frontend-common.js |
| 62 | +function initAceEditor(height) { |
| 63 | + pyInputAceEditor = ace.edit('codeInputPane'); |
| 64 | + |
| 65 | + var s = pyInputAceEditor.getSession(); |
| 66 | + s.setMode("ace/mode/python"); |
| 67 | + // tab -> 4 spaces |
| 68 | + s.setTabSize(4); |
| 69 | + s.setUseSoftTabs(true); |
| 70 | + |
| 71 | + // disable extraneous indicators: |
| 72 | + s.setFoldStyle('manual'); // no code folding indicators |
| 73 | + pyInputAceEditor.setHighlightActiveLine(false); |
| 74 | + pyInputAceEditor.setShowPrintMargin(false); |
| 75 | + pyInputAceEditor.setBehavioursEnabled(false); |
| 76 | + pyInputAceEditor.$blockScrolling = Infinity; // kludgy to shut up weird warnings |
| 77 | + |
| 78 | + // auto-grow height as fit |
| 79 | + pyInputAceEditor.setOptions({minLines: 18, maxLines: 1000}); |
| 80 | + |
| 81 | + $('#codeInputPane').css('width', '500px'); |
| 82 | + $('#codeInputPane').css('height', height + 'px'); // VERY IMPORTANT so that it works on I.E., ugh! |
| 83 | + |
| 84 | + pyInputAceEditor.on('change', function(e) { |
| 85 | + $.doTimeout('pyInputAceEditorChange', 500, function() { |
| 86 | + executeCodeFromScratch(); |
| 87 | + }); // debounce |
| 88 | + clearFrontendError(); |
| 89 | + s.clearAnnotations(); |
| 90 | + }); |
| 91 | + |
| 92 | + // don't do real-time syntax checks: |
| 93 | + // https://github.com/ajaxorg/ace/wiki/Syntax-validation |
| 94 | + s.setOption("useWorker", false); |
| 95 | + pyInputAceEditor.focus(); |
| 96 | +} |
| 97 | + |
| 98 | +// based on executeCodeAndCreateViz |
| 99 | +function optliveExecuteCodeAndCreateViz(codeToExec, |
| 100 | + backendScript, backendOptionsObj, |
| 101 | + frontendOptionsObj, |
| 102 | + outputDiv, |
| 103 | + handleSuccessFunc, handleUncaughtExceptionFunc) { |
| 104 | + |
| 105 | + function execCallback(dataFromBackend) { |
| 106 | + var trace = dataFromBackend.trace; |
| 107 | + |
| 108 | + // don't enter visualize mode if there are killer errors: |
| 109 | + if (!trace || |
| 110 | + (trace.length == 0) || |
| 111 | + (trace[trace.length - 1].event == 'uncaught_exception')) { |
| 112 | + |
| 113 | + handleUncaughtExceptionFunc(trace); |
| 114 | + |
| 115 | + if (trace.length == 1) { |
| 116 | + setFronendError([trace[0].exception_msg]); |
| 117 | + } |
| 118 | + else if (trace.length > 0 && trace[trace.length - 1].exception_msg) { |
| 119 | + setFronendError([trace[trace.length - 1].exception_msg]); |
| 120 | + } |
| 121 | + else { |
| 122 | + setFronendError(["Unknown error. Reload the page and try again. Or report a bug to", |
| 123 | + "philip@pgbovine.net by clicking on the 'Generate permanent link'", |
| 124 | + "button at the bottom and including a URL in your email."]); |
| 125 | + } |
| 126 | + } |
| 127 | + else { |
| 128 | + myVisualizer = new ExecutionVisualizer(outputDiv, dataFromBackend, frontendOptionsObj); |
| 129 | + handleSuccessFunc(); |
| 130 | + } |
| 131 | + |
| 132 | + doneExecutingCode(); // rain or shine, we're done executing! |
| 133 | + // run this at the VERY END after all the dust has settled |
| 134 | + } |
| 135 | + |
| 136 | + clearFrontendError(); |
| 137 | + startExecutingCode(); |
| 138 | + |
| 139 | + if (backendScript === python2_backend_script) { |
| 140 | + frontendOptionsObj.lang = 'py2'; |
| 141 | + } else if (backendScript === python3_backend_script) { |
| 142 | + frontendOptionsObj.lang = 'py3'; |
| 143 | + } else { |
| 144 | + assert(false); |
| 145 | + } |
| 146 | + |
| 147 | + $.get(backendScript, |
| 148 | + {user_script : codeToExec, |
| 149 | + raw_input_json: rawInputLst.length > 0 ? JSON.stringify(rawInputLst) : '', |
| 150 | + options_json: JSON.stringify(backendOptionsObj), |
| 151 | + user_uuid: supports_html5_storage() ? localStorage.getItem('opt_uuid') : undefined, |
| 152 | + session_uuid: sessionUUID}, |
| 153 | + execCallback, "json"); |
| 154 | +} |
| 155 | + |
| 156 | +function executeCode(forceStartingInstr, forceRawInputLst) { |
| 157 | + if (forceRawInputLst !== undefined) { |
| 158 | + rawInputLst = forceRawInputLst; // UGLY global across modules, FIXME |
| 159 | + } |
| 160 | + |
| 161 | + backend_script = python2_backend_script; |
| 162 | + |
| 163 | + var backendOptionsObj = {cumulative_mode: false, |
| 164 | + heap_primitives: false, |
| 165 | + show_only_outputs: false, |
| 166 | + py_crazy_mode: false, |
| 167 | + origin: originFrontendJsFile}; |
| 168 | + |
| 169 | + var startingInstruction = forceStartingInstr ? forceStartingInstr : 0; |
| 170 | + var frontendOptionsObj = {startingInstruction: startingInstruction, |
| 171 | + executeCodeWithRawInputFunc: executeCodeWithRawInput, |
| 172 | + updateOutputCallback: function() {$('#urlOutput,#embedCodeOutput').val('');}, |
| 173 | + hideCode: true, |
| 174 | + jumpToEnd: true, |
| 175 | + } |
| 176 | + |
| 177 | + optliveExecuteCodeAndCreateViz(pyInputGetValue(), |
| 178 | + backend_script, backendOptionsObj, |
| 179 | + frontendOptionsObj, |
| 180 | + 'pyOutputPane', |
| 181 | + optliveFinishSuccessfulExecution, |
| 182 | + handleUncaughtExceptionFunc); |
| 183 | +} |
| 184 | + |
| 185 | +$(document).ready(function() { |
| 186 | + genericOptFrontendReady(); // initialize at the very end |
| 187 | + $("#pyOutputPane").show(); |
| 188 | +}); |
0 commit comments