Skip to content

Commit 9928282

Browse files
committed
support input/raw_input in live mode
1 parent dee5628 commit 9928282

4 files changed

Lines changed: 45 additions & 8 deletions

File tree

‎v3/css/opt-live.css‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
padding-top: 3px;
2323
}
2424

25-
/* copied from pytutor.css */
25+
/* copied from pytutor.css - TODO: integrate and merge together */
2626
/* VCR control buttons for stepping through execution */
2727
#vcrControls {
2828
margin: 15px auto;
@@ -40,6 +40,15 @@
4040
margin-right: 4px;
4141
}
4242

43+
div#rawUserInputDiv {
44+
padding: 5px;
45+
width: 95%;
46+
margin: 5px auto;
47+
text-align: center;
48+
border: 1px #e93f34 solid;
49+
}
50+
51+
4352
/* keep it a fixed min height so that footer below it doesn't jiggle up
4453
and down */
4554
#frontendErrorOutput {

‎v3/js/opt-live.js‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4343
- if these Ace enhancements look good, then I can also use them for
4444
Codeopticon as well!
4545
46-
- get input/raw_input working
47-
4846
- [later] add a codeopticon-style history slider of the user's past
4947
edits (but that might be confusing)
5048
@@ -143,7 +141,7 @@ function updateStepLabels() {
143141
var isLastInstr = myVisualizer.curInstr === (totalInstrs-1);
144142
if (isLastInstr) {
145143
if (myVisualizer.promptForUserInput || myVisualizer.promptForMouseInput) {
146-
$("#curInstr").html('<font color="' + brightRed + '">input()/raw_input() not supported in live mode; use the <a href="visualize.html" target="_blank">regular visualizer</a></font>');
144+
$("#curInstr").html('<b><font color="' + brightRed + '">Enter user input below:</font></b>');
147145
} else if (myVisualizer.instrLimitReached) {
148146
$("#curInstr").html("Instruction limit reached (" + String(totalInstrs-1) + " steps)");
149147
} else {
@@ -153,6 +151,27 @@ function updateStepLabels() {
153151
$("#curInstr").html("Step " + String(myVisualizer.curInstr + 1) + " of " + String(totalInstrs-1));
154152
}
155153

154+
// handle raw user input
155+
// copied from pytutor.js -- TODO: integrate this code better
156+
var ruiDiv = $('#rawUserInputDiv');
157+
if (isLastInstr && myVisualizer.executeCodeWithRawInputFunc &&
158+
myVisualizer.promptForUserInput) {
159+
ruiDiv.show();
160+
ruiDiv.find('#userInputPromptStr').html(myVisualizer.userInputPromptStr);
161+
ruiDiv.find('#raw_input_textbox').val('');
162+
163+
// first UNBIND handler so that we don't build up multiple click events
164+
ruiDiv.find('#raw_input_submit_btn')
165+
.unbind('click')
166+
.click(function() {
167+
var userInput = ruiDiv.find('#raw_input_textbox').val();
168+
// advance instruction count by 1 to get to the NEXT instruction
169+
myVisualizer.executeCodeWithRawInputFunc(userInput, myVisualizer.curInstr + 1);
170+
});
171+
} else {
172+
ruiDiv.hide(); // hide by default
173+
}
174+
156175
// render error (if applicable):
157176
var curEntry = myVisualizer.curTrace[myVisualizer.curInstr];
158177
if (curEntry.event == 'exception' ||
@@ -345,7 +364,7 @@ function initAceEditor(height) {
345364
$.doTimeout('pyInputAceEditorChange',
346365
500, /* go a bit faster than CODE_SNAPSHOT_DEBOUNCE_MS to feel more snappy */
347366
//CODE_SNAPSHOT_DEBOUNCE_MS /* match the value in opt-frontend-common.js for consistency and easy apples-to-apples comparisons later on */,
348-
executeCode); // debounce
367+
executeCodeFromScratch); // debounce
349368
clearFrontendError();
350369
s.clearAnnotations();
351370
});
@@ -559,8 +578,8 @@ $(document).ready(function() {
559578

560579
$('#cumulativeModeSelector,#heapPrimitivesSelector,#textualMemoryLabelsSelector,#pythonVersionSelector').change(function() {
561580
setAceMode();
562-
// force a recompile on a toggle switch
563-
executeCode();
581+
// force a re-execute on a toggle switch
582+
executeCodeFromScratch();
564583
});
565584

566585
setAceMode(); // set syntax highlighting at the end

‎v3/js/pytutor.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,7 @@ ExecutionVisualizer.prototype.updateOutputFull = function(smoothTransition) {
16621662

16631663
if (isLastInstr) {
16641664
if (this.promptForUserInput || this.promptForMouseInput) {
1665-
vcrControls.find("#curInstr").html('<b><font color="' + brightRed + '">Enter user input:</font></b>');
1665+
vcrControls.find("#curInstr").html('<b><font color="' + brightRed + '">Enter user input below:</font></b>');
16661666
}
16671667
else if (this.instrLimitReached) {
16681668
vcrControls.find("#curInstr").html("Instruction limit reached");

‎v3/live.html‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@
9090
<div id="legendDiv"></div>
9191
<div id="executionSlider" style="margin-top: 10px;"></div>
9292

93+
<!-- copied and pasted from pytutor.js
94+
TODO: integrate all this together and modularize -->
95+
9396
<div id="vcrControls" style="display: none;">
9497
<button id="jmpFirstInstr", type="button">&lt;&lt; First</button>
9598
<button id="jmpStepBack", type="button">&lt; Back</button>
@@ -98,6 +101,12 @@
98101
<button id="jmpLastInstr", type="button">Last &gt;&gt;</button>
99102
</div>
100103

104+
<div id="rawUserInputDiv" style="display: none;">
105+
<span id="userInputPromptStr"></span>
106+
<input type="text" id="raw_input_textbox" size="30"/>
107+
<button id="raw_input_submit_btn">Submit</button>
108+
</div>
109+
101110
<div id="frontendErrorOutput"></div>
102111
</div>
103112
</td>

0 commit comments

Comments
 (0)