-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisualizer.py
More file actions
executable file
·198 lines (167 loc) · 6.12 KB
/
visualizer.py
File metadata and controls
executable file
·198 lines (167 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Copyright (C) 2011 Bradley N. Miller
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
__author__ = 'bmiller'
from docutils import nodes
from docutils.parsers.rst import directives
from docutils.parsers.rst import Directive
from pg_logger import exec_script_str_local
import json
def setup(app):
app.add_directive('codelens',Codelens)
app.add_stylesheet('codelens/v3/css/pytutor.css')
app.add_stylesheet('codelens/v3/css/basic.css')
app.add_stylesheet('codelens/v3/css/ui-lightness/jquery-ui-1.8.24.custom.css')
app.add_javascript('codelens/v3/js/jquery.simplemodal.js')
app.add_javascript('codelens/v3/js/d3.v2.min.js')
app.add_javascript('codelens/v3/js/jquery.ba-bbq.min.js')
app.add_javascript('codelens/v3/js/jquery.jsPlumb-1.3.10-all-min.js')
app.add_javascript('codelens/v3/js/jquery-ui-1.8.24.custom.min.js')
app.add_javascript('codelens/v3/js/pytutor.js')
VIS = '''
<div id="%(divid)s"></div>
<p class="cl_caption"><span class="cl_caption_text">%(caption)s (%(divid)s)</span> </p>'''
QUESTION = '''
<div id="%(divid)s_modal" class="basic-modal-content">
<h3>Check your understanding</h3>
%(question)s
<input id="%(divid)s_textbox" type="textbox" />
<button id="%(divid)s_tracecheck" onclick="traceQCheckMe('%(divid)s_textbox','%(divid)s','%(correct)s')">Check
Me</button>
<button onclick="closeModal('%(divid)s')">Continue...</button>
<p id="%(divid)s_feedbacktext" class="feedbacktext"></p>
</div>
'''
DATA = '''
<script type="text/javascript">
%(tracedata)s
var %(divid)s_vis;
$(document).ready(function() {
%(divid)s_vis = new ExecutionVisualizer('%(divid)s',%(divid)s_trace,
{embeddedMode: %(embedded)s,
verticalStack: true,
heightChangeCallback: redrawAllVisualizerArrows,
codeDivWidth: 500
});
attachLoggers(%(divid)s_vis,'%(divid)s');
allVisualizers.push(%(divid)s_vis);
});
$(document).ready(function() {
$("#%(divid)s_tracecheck").click(function() {
logBookEvent({'event':'codelens', 'act': 'check', 'div_id':'%(divid)s'});
});
});
if (allVisualizers === undefined) {
var allVisualizers = [];
}
$(window).resize(function() {
%(divid)s_vis.redrawConnectors();
});
</script>
'''
# Some documentation to help the author.
# Here's and example of a single stack frame.
# you might ask a qestion about the value of a global variable
# in which case the correct answer is expressed as:
#
# globals.a
#
# You could ask about a value on the heap
#
# heap.variable
#
# You could ask about a local variable -- not shown here.
#
# locals.variable
#
# You could even ask about what line is going to be executed next
#
# line
# {
# "ordered_globals": [
# "a",
# "b"
# ],
# "stdout": "1\n",
# "func_name": "<module>",
# "stack_to_render": [],
# "globals": {
# "a": 1,
# "b": 1
# },
# "heap": {},
# "line": 5,
# "event": "return"
# }
class Codelens(Directive):
required_arguments = 1
optional_arguments = 1
option_spec = {
'tracedata':directives.unchanged,
'caption':directives.unchanged,
'showoutput':directives.flag,
'question':directives.unchanged,
'correct':directives.unchanged,
'feedback':directives.unchanged,
'breakline':directives.nonnegative_int
}
has_content = True
def run(self):
self.JS_VARNAME = ""
self.JS_VARVAL = ""
def raw_dict(input_code, output_trace):
ret = dict(code=input_code, trace=output_trace)
return ret
def js_var_finalizer(input_code, output_trace):
global JS_VARNAME
ret = dict(code=input_code, trace=output_trace)
json_output = json.dumps(ret, indent=None)
return "var %s = %s;" % (self.JS_VARNAME, json_output)
self.options['divid'] = self.arguments[0]
if self.content:
source = "\n".join(self.content)
else:
source = '\n'
CUMULATIVE_MODE=False
self.JS_VARNAME = self.options['divid']+'_trace'
if 'showoutput' not in self.options:
self.options['embedded'] = 'true' # to set embeddedmode to true
else:
self.options['embedded'] = 'false'
if 'question' in self.options:
curTrace = exec_script_str_local(source, CUMULATIVE_MODE, raw_dict)
self.inject_questions(curTrace)
json_output = json.dumps(curTrace, indent=None)
self.options['tracedata'] = "var %s = %s;" % (self.JS_VARNAME, json_output)
else:
self.options['tracedata'] = exec_script_str_local(source, CUMULATIVE_MODE, js_var_finalizer)
res = VIS
if 'caption' not in self.options:
self.options['caption'] = ''
if 'question' in self.options:
res += QUESTION
if 'tracedata' in self.options:
res += DATA
return [nodes.raw('',res % self.options,format='html')]
def inject_questions(self,curTrace):
if 'breakline' not in self.options:
raise RuntimeError('Must have breakline option')
breakline = self.options['breakline']
for frame in curTrace['trace']:
if frame['line'] == breakline:
frame['question'] = dict(text=self.options['question'],
correct = self.options['correct'],
div = self.options['divid']+'_modal',
feedback = self.options['feedback'] )