|
| 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 | + |
0 commit comments