Skip to content

Commit f023d4d

Browse files
pgbovinemmmicedcoffee
authored andcommitted
added static screenshot renderer
1 parent 9d9329a commit f023d4d

7 files changed

Lines changed: 104 additions & 1 deletion

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../images/
1+
../images

‎v3/screenshot-renderer/README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Render static screenshots of Online Python Tutor visualizations
2+
3+
I've written a script that uses [PhantomJS](http://phantomjs.org/) to
4+
create screenshots of Online Python Tutor data structure visualizations
5+
as `.png` files.
6+
7+
For example, first [install PhantomJS](http://phantomjs.org/download.html) and then run:
8+
9+
phantomjs render-opt-screenshots.js test.py
10+
11+
This will send `test.py` to Online Python Tutor and visualize all steps
12+
as a set of `test.py.step.*.png` files.
13+
14+
Here are some example files:
15+
16+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
var page = require('webpage').create();
2+
var system = require('system');
3+
var fs = require('fs');
4+
5+
if (system.args.length < 2) {
6+
console.log('\nUsage:\n phantomjs render-opt-screenshots.js <filename>');
7+
console.log('\nVisualizes execution of a Python file at pythontutor.com and renders');
8+
console.log('the state diagram at each step i as <filename>.step.$i.png');
9+
phantom.exit(1);
10+
}
11+
12+
fn = system.args[1];
13+
14+
var pythonScript = fs.open(fn, 'r').read();
15+
console.log('Visualizing ...\n');
16+
console.log(pythonScript);
17+
console.log('--- please wait ---');
18+
19+
var scriptEncoded = encodeURIComponent(pythonScript);
20+
21+
// construct a URL with the script and options:
22+
// (for instance, to run with Python 3, change the 'py=2' string to 'py=3')
23+
var url = 'http://www.pythontutor.com/visualize.html#code=' +
24+
scriptEncoded +
25+
'&mode=display' +
26+
'&cumulative=false' +
27+
'&heapPrimitives=false' +
28+
'&drawParentPointers=false' +
29+
'&textReferences=false' +
30+
'&showOnlyOutputs=false' +
31+
'&py=2' +
32+
'&curInstr=0';
33+
34+
page.open(url, function () {
35+
// impose a slight delay to make sure the page loads completely before working
36+
window.setTimeout(function () {
37+
38+
// hide extraneous components and resize
39+
page.evaluate(function() {
40+
$("#footer").hide();
41+
$("#vizLayoutTdFirst").hide();
42+
$('#pyOutputPane').width($('table.visualizer').width());
43+
});
44+
45+
// grab maximum instruction from the page itself
46+
var maxInstr = page.evaluate(function() {
47+
return myVisualizer.curTrace.length - 1;
48+
});
49+
50+
for (var i=0; i <= maxInstr; i++) {
51+
page.evaluate(function(i) {
52+
myVisualizer.curInstr = i;
53+
myVisualizer.updateOutput();
54+
}, i /* pass i in here */);
55+
var outfn = fn + '.step.' + (i+1) + '.png';
56+
page.render(outfn);
57+
console.log('Rendered step ' + (i+1) + ' of ' + (maxInstr+1));
58+
}
59+
60+
phantom.exit()
61+
62+
}, 1000);
63+
});
64+
12.1 KB
Loading
12.6 KB
Loading
12.5 KB
Loading

‎v3/screenshot-renderer/test.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
x = [1, 2, 3]
2+
y = [4, 5, 6]
3+
z = y
4+
y = x
5+
x = z
6+
7+
x = [1, 2, 3] # a different [1, 2, 3] list!
8+
y = x
9+
x.append(4)
10+
y.append(5)
11+
z = [1, 2, 3, 4, 5] # a different list!
12+
x.append(6)
13+
y = "hello"
14+
15+
16+
def foo(lst):
17+
lst.append("hello")
18+
bar(lst)
19+
20+
def bar(myLst):
21+
print(myLst)
22+
23+
foo(x)

0 commit comments

Comments
 (0)