Skip to content

Commit a9e561b

Browse files
committed
added two entry points: opt-frontend and opt-live
1 parent a50df37 commit a9e561b

6 files changed

Lines changed: 206 additions & 49 deletions

File tree

‎v5-unity/js/opt-frontend-common.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = {
3535
getBaseBackendOptionsObj: getBaseBackendOptionsObj,
3636
getBaseFrontendOptionsObj: getBaseFrontendOptionsObj,
3737
getVisualizer: getVisualizer,
38+
setVisualizer: setVisualizer,
3839
getRawInputLst: getRawInputLst,
3940
setRawInputLst: setRawInputLst,
4041
}
@@ -762,6 +763,7 @@ function populateTogetherJsShareUrl() {
762763

763764
var myVisualizer = null; // singleton ExecutionVisualizer instance
764765
function getVisualizer() {return myVisualizer;}
766+
function setVisualizer(v) {myVisualizer = v;}
765767

766768
var rawInputLst = []; // a list of strings inputted by the user in response to raw_input or mouse_input events
767769
function getRawInputLst() {return rawInputLst;}

‎v5-unity/js/opt-live.js‎

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
1-
/*
1+
// Python Tutor: https://github.com/pgbovine/OnlinePythonTutor/
2+
// Copyright (C) Philip Guo (philip@pgbovine.net)
3+
// LICENSE: https://github.com/pgbovine/OnlinePythonTutor/blob/master/LICENSE.txt
24

3-
Online Python Tutor
4-
https://github.com/pgbovine/OnlinePythonTutor/
5+
// TODO: using myVisualizer here borrowed from opt-frontend-common.js is
6+
// very dicey and dangerous due to scoping issues!
57

6-
Copyright (C) Philip J. Guo (philip@pgbovine.net)
8+
// use Webpack to automatically package up these dependencies
79

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:
10+
require('../css/opt-frontend.css');
11+
require('../css/opt-live.css');
1512

16-
The above copyright notice and this permission notice shall be included
17-
in all copies or substantial portions of the Software.
13+
var optCommon = require('./opt-frontend-common.js');
14+
var pytutor = require('./pytutor.js');
15+
var assert = pytutor.assert;
1816

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.
17+
// just punt and use global script dependencies
18+
require("script!./ace/src-min-noconflict/ace.js");
19+
require('script!./ace/src-min-noconflict/mode-python.js');
20+
require('script!./ace/src-min-noconflict/mode-javascript.js');
21+
require('script!./ace/src-min-noconflict/mode-typescript.js');
22+
require('script!./ace/src-min-noconflict/mode-c_cpp.js');
23+
require('script!./ace/src-min-noconflict/mode-java.js');
24+
require('script!./ace/src-min-noconflict/mode-ruby.js');
2625

27-
*/
2826

2927
// OPT live programming prototype started on 2016-05-30
3028
// first launched as a "Live Programming Mode" button on main OPT site
@@ -57,14 +55,6 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5755
*/
5856

5957

60-
// Pre-reqs:
61-
// - pytutor.js
62-
// - jquery.ba-bbq.min.js
63-
// - jquery.ba-dotimeout.min.js // for event debouncing: http://benalman.com/code/projects/jquery-dotimeout/examples/debouncing/
64-
// - opt-frontend-common.js
65-
// should all be imported BEFORE this file
66-
67-
6858
// these scripts override the versions defined in opt-frontend-common.js
6959

7060
// backend scripts to execute (Python 2 and 3 variants, if available)
@@ -128,6 +118,7 @@ function removeAllGutterDecorations() {
128118
}
129119

130120
function updateStepLabels() {
121+
var myVisualizer = optCommon.getVisualizer();
131122
assert(myVisualizer);
132123
myVisualizer.updateCurPrevLines(); // do this first to update the right fields
133124

@@ -167,6 +158,7 @@ function updateStepLabels() {
167158
.unbind('click')
168159
.click(function() {
169160
var userInput = ruiDiv.find('#raw_input_textbox').val();
161+
var myVisualizer = optCommon.getVisualizer();
170162
// advance instruction count by 1 to get to the NEXT instruction
171163
myVisualizer.executeCodeWithRawInputFunc(userInput, myVisualizer.curInstr + 1);
172164
});
@@ -236,6 +228,7 @@ function updateStepLabels() {
236228
}
237229

238230
function optliveFinishSuccessfulExecution() {
231+
var myVisualizer = optCommon.getVisualizer();
239232
assert(myVisualizer);
240233
$("#pyOutputPane,#vcrControls").show();
241234
doneExecutingCode();
@@ -259,6 +252,7 @@ function optliveFinishSuccessfulExecution() {
259252
// was changed by a user-initiated event, then this code should be
260253
// executed ...
261254
if (evt.originalEvent) {
255+
var myVisualizer = optCommon.getVisualizer();
262256
myVisualizer.renderStep(ui.value);
263257
}
264258
//updateStepLabels(); // I don't think we need this anymore
@@ -331,6 +325,7 @@ function optliveFinishSuccessfulExecution() {
331325
uh.push([args.myViz.curInstr, curTs]);
332326
}
333327

328+
var myVisualizer = optCommon.getVisualizer();
334329
$('#executionSlider').slider('value', myVisualizer.curInstr); // update slider
335330
updateStepLabels();
336331

@@ -348,6 +343,7 @@ function optliveHandleUncaughtExceptionFunc(trace) {
348343
if (errorLineNo !== undefined && errorLineNo != NaN) {
349344
removeAllGutterDecorations();
350345

346+
var myVisualizer = optCommon.getVisualizer();
351347
if (myVisualizer) {
352348
toggleSyntaxError(true);
353349
myVisualizer.redrawConnectors();
@@ -390,6 +386,7 @@ function initAceEditor(height) {
390386
resize: function(evt, ui) {
391387
pyInputAceEditor.resize(); // to keep Ace internals happy
392388
$("#pyInputPane").width($("#codeInputPane").width()); // to keep parent happy
389+
var myVisualizer = optCommon.getVisualizer();
393390
if (myVisualizer) {
394391
myVisualizer.redrawConnectors(); // to keep visualizations happy
395392
}
@@ -451,8 +448,10 @@ function optliveExecuteCodeAndCreateViz(codeToExec,
451448
}
452449
}
453450
else {
451+
var myVisualizer = optCommon.getVisualizer();
454452
prevVisualizer = myVisualizer;
455-
myVisualizer = new ExecutionVisualizer(outputDiv, dataFromBackend, frontendOptionsObj);
453+
var v = new ExecutionVisualizer(outputDiv, dataFromBackend, frontendOptionsObj);
454+
optCommon.setVisualizer(v);
456455
handleSuccessFunc();
457456
}
458457

@@ -496,6 +495,7 @@ function optliveExecuteCodeAndCreateViz(codeToExec,
496495
assert(false);
497496
}
498497

498+
var myVisualizer = optCommon.getVisualizer();
499499
// submit update history of the "previous" visualizer whenever you
500500
// run the code and hopefully get a new visualizer back
501501
//
@@ -602,45 +602,49 @@ function optliveExecuteCodeFromScratch() {
602602
}
603603

604604
$(document).ready(function() {
605-
genericOptFrontendReady();
605+
optCommon.genericOptFrontendReady();
606606

607607
$('#legendDiv')
608608
.append('<svg id="prevLegendArrowSVG"/> line that has just executed')
609609
.append('<p style="margin-top: 4px"><svg id="curLegendArrowSVG"/> next line to execute</p>');
610610

611611
d3.select('svg#prevLegendArrowSVG')
612612
.append('polygon')
613-
.attr('points', SVG_ARROW_POLYGON)
614-
.attr('fill', lightArrowColor);
613+
.attr('points', pytutor.SVG_ARROW_POLYGON)
614+
.attr('fill', pytutor.lightArrowColor);
615615

616616
d3.select('svg#curLegendArrowSVG')
617617
.append('polygon')
618-
.attr('points', SVG_ARROW_POLYGON)
619-
.attr('fill', darkArrowColor);
618+
.attr('points', pytutor.SVG_ARROW_POLYGON)
619+
.attr('fill', pytutor.darkArrowColor);
620620

621621
$('#cumulativeModeSelector,#heapPrimitivesSelector,#textualMemoryLabelsSelector,#pythonVersionSelector').change(function() {
622-
setAceMode();
622+
optCommon.setAceMode();
623623
// force a re-execute on a toggle switch
624624
optliveExecuteCodeFromScratch();
625625
});
626626

627-
setAceMode(); // set syntax highlighting at the end
627+
optCommon.setAceMode(); // set syntax highlighting at the end
628628
$("#pyOutputPane").show();
629629

630630

631631
$("#jmpFirstInstr").click(function() {
632+
var myVisualizer = optCommon.getVisualizer();
632633
if (myVisualizer) {myVisualizer.renderStep(0);}
633634
});
634635

635636
$("#jmpLastInstr").click(function() {
637+
var myVisualizer = optCommon.getVisualizer();
636638
if (myVisualizer) {myVisualizer.renderStep(myVisualizer.curTrace.length - 1);}
637639
});
638640

639641
$("#jmpStepBack").click(function() {
642+
var myVisualizer = optCommon.getVisualizer();
640643
if (myVisualizer) {myVisualizer.stepBack();}
641644
});
642645

643646
$("#jmpStepFwd").click(function() {
647+
var myVisualizer = optCommon.getVisualizer();
644648
if (myVisualizer) {myVisualizer.stepForward();}
645649
});
646650
});

‎v5-unity/js/pytutor.js‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ require('./jquery.qtip.min.js');
1414
require('../css/jquery.qtip.css');
1515
require('../css/pytutor.css');
1616

17-
// TODO: abstract this better
18-
// only export methods and NOT objects, since they're copied by value
19-
module.exports = {
20-
assert: assert,
21-
htmlspecialchars: htmlspecialchars,
22-
ExecutionVisualizer: ExecutionVisualizer,
23-
}
24-
2517
/* Coding gotchas:
2618
2719
- NEVER use raw $(__) or d3.select(__) statements to select DOM elements.
@@ -4820,3 +4812,17 @@ ExecutionVisualizer.prototype.activateJavaFrontend = function() {
48204812
};
48214813

48224814
}
4815+
4816+
4817+
// TODO: abstract this better
4818+
// only export methods and NOT objects, since they're copied by value
4819+
module.exports = {
4820+
assert: assert,
4821+
htmlspecialchars: htmlspecialchars,
4822+
ExecutionVisualizer: ExecutionVisualizer,
4823+
// OK since these are constants, but make sure they're defined first
4824+
// (still hacky, though!)
4825+
SVG_ARROW_POLYGON: SVG_ARROW_POLYGON,
4826+
darkArrowColor: darkArrowColor,
4827+
lightArrowColor: lightArrowColor,
4828+
}

‎v5-unity/live.html‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml">
3+
4+
<!--
5+
Python Tutor: https://github.com/pgbovine/OnlinePythonTutor/
6+
Copyright (C) Philip Guo (philip@pgbovine.net)
7+
LICENSE: https://github.com/pgbovine/OnlinePythonTutor/blob/master/LICENSE.txt
8+
-->
9+
10+
<!-- OPT live programming prototype started on 2016-05-30 -->
11+
12+
<head>
13+
<title>Live Programming Mode - Python Tutor - Visualize Python and JavaScript code</title>
14+
<meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
15+
16+
<!-- let Webpack take care of everything. Use the [hash] feature to
17+
create unique filenames for releases:
18+
https://webpack.github.io/docs/long-term-caching.html -->
19+
<script type="text/javascript" src="opt-live.bundle.js" charset="utf-8"></script>
20+
21+
<!-- insert google-analytics.txt contents here -->
22+
23+
</head>
24+
25+
<body>
26+
27+
<div id="liveModeHeader">
28+
29+
This is the <b>live programming mode</b> of Python Tutor, which
30+
continually runs and visualizes your code as you type. Help
31+
us improve this mode by filling out this <a target="_blank"
32+
href="https://docs.google.com/forms/d/1XT-rPaWxDRNOPAfBxIqzCXs7nYGUgOjNofmUnuQZYV8/viewform">two-question
33+
survey</a>. Or return to the <a
34+
href="visualize.html" target="_blank">regular
35+
Python Tutor visualizer</a>.
36+
37+
</div>
38+
39+
<table>
40+
<tr>
41+
<td valign="top">
42+
<div id="pyInputPane">
43+
<div id="codeInputWarnings">Write code in
44+
<select id="pythonVersionSelector">
45+
<option value="2">Python 2.7</option>
46+
<option value="3">Python 3.3</option>
47+
<option value="js">JavaScript ES6</option>
48+
</select>
49+
50+
<span style="color: #888; font-size: 8pt; float: right;">(drag lower right corner to resize code editor)</span>
51+
</div>
52+
53+
<div id="codeInputPane"></div>
54+
<div id="legendDiv"></div>
55+
<div id="executionSlider" style="margin-top: 10px;"></div>
56+
57+
<!-- copied and pasted from pytutor.js
58+
TODO: integrate all this together and modularize -->
59+
60+
<div id="vcrControls" style="display: none;">
61+
<button id="jmpFirstInstr", type="button">&lt;&lt; First</button>
62+
<button id="jmpStepBack", type="button">&lt; Back</button>
63+
<span id="curInstr"></span>
64+
<button id="jmpStepFwd", type="button">Forward &gt;</button>
65+
<button id="jmpLastInstr", type="button">Last &gt;&gt;</button>
66+
</div>
67+
68+
<div id="rawUserInputDiv" style="display: none;">
69+
<span id="userInputPromptStr"></span>
70+
<input type="text" id="raw_input_textbox" size="30"/>
71+
<button id="raw_input_submit_btn">Submit</button>
72+
</div>
73+
74+
<div id="frontendErrorOutput"></div>
75+
</div>
76+
</td>
77+
<td valign="top">
78+
<div id="pyOutputPane"/>
79+
</td>
80+
</tr>
81+
</table>
82+
83+
<div id="footer">
84+
85+
<p style="margin-top: 10px; line-height: 175%; color: black; font-size: 10pt;">
86+
If you are <b>at least 60 years old</b>, please support our research by <a href="https://docs.google.com/forms/d/1lrXsE04ghfX9wNzTVwm1Wc6gQ5I-B4uw91ACrbDhJs8/viewform" target="_blank">filling out this short survey</a>.
87+
</p>
88+
89+
<p>Visualizer options:</p>
90+
91+
<div id="optionsPane" style="margin-top: 10px; padding-bottom: 15px; line-height: 150%;">
92+
<select id="cumulativeModeSelector">
93+
<option value="false">hide exited frames [default]</option>
94+
<option value="true">show exited frames (Python)</option>
95+
<!-- <option value="holistic">holistic mode (experimental)</option> -->
96+
</select>
97+
<select id="heapPrimitivesSelector">
98+
<option value="false">inline primitives &amp; nested objects [default]</option>
99+
<option value="true">render all objects on the heap (Python)</option>
100+
</select>
101+
<select id="textualMemoryLabelsSelector">
102+
<option value="false">draw pointers as arrows [default]</option>
103+
<option value="true">use text labels for pointers</option>
104+
</select>
105+
</div>
106+
107+
<p>
108+
<button id="genUrlBtn" class="smallBtn" type="button">Generate permanent link</button> <input type="text" id="urlOutput" size="70"/>
109+
</p>
110+
111+
<p>Click the button above to create a permanent link to your
112+
visualization. To report a bug, paste the link along with a brief error
113+
description in an email addressed to philip@pgbovine.net</p>
114+
115+
<p style="margin-top: 35px;">
116+
This live programming mode of
117+
<a href="http://pythontutor.com/">Python Tutor</a> (<a href="https://github.com/pgbovine/OnlinePythonTutor">code on GitHub</a>) supports three
118+
languages: Python <a href="http://www.python.org/doc/2.7/">2.7</a> and <a
119+
href="http://www.python.org/doc/3.3.0/">3.3</a> with limited module
120+
imports and no file I/O, and
121+
JavaScript running in Node.js v6.0.0 with limited support for ES6.
122+
123+
Try the regular
124+
<a href="http://pythontutor.com/visualize.html">Python Tutor</a>
125+
visualizer for additional language support.</p>
126+
127+
<p style="margin-top: 30px;">Privacy Policy: By using Online Python
128+
Tutor, your visualized code, options, user interactions, text chats, and
129+
IP address are logged on our server and may be analyzed for research
130+
purposes. Nearly all Web services collect this basic information from
131+
users. However, the Online Python Tutor website (pythontutor.com) does
132+
not collect any personal information or session state from users, nor
133+
does it issue any cookies.</p>
134+
135+
<p style="margin-top: 25px;">
136+
Copyright &copy; <a href="http://www.pgbovine.net/">Philip Guo</a>. All rights reserved.
137+
</p>
138+
139+
</div>
140+
141+
</body>
142+
</html>

‎v5-unity/visualize.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<!-- let Webpack take care of everything. Use the [hash] feature to
1515
create unique filenames for releases:
1616
https://webpack.github.io/docs/long-term-caching.html -->
17-
<script type="text/javascript" src="bundle.js" charset="utf-8"></script>
17+
<script type="text/javascript" src="opt-frontend.bundle.js" charset="utf-8"></script>
1818

1919
<!-- include this AFTER opt-frontend.js so that configuration
2020
global variables are already defined at load time.

0 commit comments

Comments
 (0)