Skip to content

Instantly share code, notes, and snippets.

@Carreau
Created November 4, 2012 19:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Carreau/4013237 to your computer and use it in GitHub Desktop.
Save Carreau/4013237 to your computer and use it in GitHub Desktop.
Headless run-all notebook
page = require('webpage').create();
var system = require('system');
var fs = require("fs");
// notebook uuid to load.
var uuid = system.args[1];
var noop = function(){}
// external action to map on console.log message of embeded
// browser
action_map = {
'execution_request.Kernel':noop,
'notebook_loaded.Notebook':noop,
'notebook_loading.Notebook':noop,
'notebook_save_failed.Notebook':noop,
// if notebook is saved, exit
'notebook_saved.Notebook':function(){phantom.exit()},
'notebook_saving.Notebook':noop,
'open_with_text.Pager':noop,
'select.Cell':noop,
'selected_cell_type_changed.Notebook':noop,
'set_dirty.Notebook':noop,
'set_next_input.Notebook':noop,
'!shell_reply.Kernel':function(){ fs.write("/dev/stdout", ".", "w");},
'shell_reply.Kernel':noop,
'status_busy.Kernel':noop,
'status_dead.Kernel':noop,
'status_idle.Kernel':noop,
'status_interrupting.Kernel':noop,
'status_restarting.Kernel':noop,
'status_started.Kernel':noop,
}
// if embedded browser log a message, worward it, and trigger corresponding
// action if exist
page.onConsoleMessage = function (msg) {
if(msg[0] != '!'){
console.log('==> ' + msg);
}
if (msg in action_map){
action_map[msg]();
}
};
// looping action_map key does not work,
// so re-bind all event by hand to trigger the same message as their name
// in the embeded console.
var all_Event = function(){
//$([IPython.events]).on('execution_request.Kernel', function(){console.log('execution_request.Kernel')});
$([IPython.events]).on('notebook_loaded.Notebook', function(){console.log('notebook_loaded.Notebook')});
$([IPython.events]).on('notebook_loading.Notebook', function(){console.log('notebook_loading.Notebook')});
$([IPython.events]).on('notebook_save_failed.Notebook', function(){console.log('notebook_save_failed.Notebook')});
$([IPython.events]).on('notebook_saved.Notebook', function(){console.log('notebook_saved.Notebook')});
$([IPython.events]).on('notebook_saving.Notebook', function(){console.log('notebook_saving.Notebook')});
$([IPython.events]).on('open_with_text.Pager', function(){console.log('open_with_text.Pager')});
$([IPython.events]).on('select.Cell', function(){console.log('select.Cell')});
//$([IPython.events]).on('selected_cell_type_changed.Notebook', function(){console.log('selected_cell_type_changed.Notebook')});
//$([IPython.events]).on('set_dirty.Notebook', function(){console.log('set_dirty.Notebook')});
$([IPython.events]).on('set_next_input.Notebook', function(){console.log('set_next_input.Notebook')});
//$([IPython.events]).on('shell_reply.Kernel', function(){console.log('shell_reply.Kernel')});
$([IPython.events]).on('shell_reply.Kernel', function(){console.log('!shell_reply.Kernel')});
//$([IPython.events]).on('status_busy.Kernel', function(){console.log('status_busy.Kernel')});
$([IPython.events]).on('status_dead.Kernel', function(){console.log('status_dead.Kernel')});
//$([IPython.events]).on('status_idle.Kernel', function(){console.log('status_idle.Kernel')});
$([IPython.events]).on('status_interrupting.Kernel', function(){console.log('status_interrupting.Kernel')});
$([IPython.events]).on('status_restarting.Kernel', function(){console.log('status_restarting.Kernel')});
$([IPython.events]).on('status_started.Kernel', function(){console.log('status_started.Kernel')});
}
// first we restart the kernel for it to be fresh.
restart_page_kernel = function(){
page.evaluate(function (){
IPython.notebook.kernel.restart();
});};
// next we run_all_cell, and once all cells are runned,
// save the notebook.
// I tried to bind the page.evaluate(xxxxxx) to console log event (to avoid setTimeout)
// but it fails with DOM error... I don't now why
run_all_and_save = function(){
page.evaluate(function (){
var execution_count=0;
$([IPython.events]).on('execution_request.Kernel',function(){execution_count=execution_count+1;})
$([IPython.events]).on('shell_reply.Kernel',function()
{execution_count=execution_count-1;
if (execution_count==0){
console.log('Done, saving');
IPython.notebook.save_notebook();
}
})
IPython.notebook.execute_all_cells();
}
);
}
once_open = function(){
page.evaluate(all_Event);
setTimeout(restart_page_kernel,1000);
setTimeout(run_all_and_save,2000);
}
page.open('http://127.0.0.1:8888/'+uuid,once_open);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment