Re: [jupyter] Auto-running notebook and hooking an event on completion

190 views
Skip to first unread message

MinRK

unread,
Oct 9, 2015, 5:54:23 AM10/9/15
to jup...@googlegroups.com

The kernel should know that it has outstanding requests, so you ought to be able to query that, to determine whether.

There isn’t a public API for this information, but this should work for now:

finished = (Object.keys(IPython.notebook.kernel._msg_callbacks).length == 0);

If you check that value when idle events arrive, it should tell you when the notebook is actually done or not.

-MinRK


On Thu, Oct 8, 2015 at 9:18 PM, Graham Wheeler <gr...@geekraver.com> wrote:
Hi all

I'm trying to have a custom.js that will automatically run a notebook when it gets opened or reloaded, and then hook an event handler when done.

I'm trying this to start:

require(['base/js/namespace', 'base/js/events', 'base/js/dialog', 'base/js/utils', 'base/js/security'], function() {
  $([IPython.events]).on('kernel_ready.Kernel kernel_created.Session notebook_loaded.Notebook', run_notebook);
});

That seems to work okay.

Getting completion event handling seems more tricky. I have:

function run_notebook() {
  var pageClass = document.body.className;
  if (pageClass.indexOf('notebook_app') >= 0) {
    IPython.notebook.clear_all_output();
    IPython.notebook.execute_all_cells();

   // My problem is here - how to tell when done.

   // One attempt - this seems to run over and over
    $([IPython.events]).on('kernel_idle.Kernel', function() {
        checkIfDone();
    });

    // Another attempt - this doesn't seem to work at all
    setTimeout(5000, function() {
      checkIfDone();
    });
  }
}

function checkIfDone() {
  if (IPython.notebook.kernel_busy) {
    setTimeout(5000, function() {
      checkIfDone();
    });
  } else {
    console.log('Finished execution');
  }
}

Is there a better way? Some other event to hook into?

Thanks!

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/0982776e-ea36-447c-a45c-d6c75775cb3f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pouya Ghadimi Karahrodi

unread,
Dec 19, 2017, 9:30:48 PM12/19/17
to Project Jupyter
Is this resolved ? I am trying to automate a notebook run and conversion to pdf.
The missing step is the save after the notebook run completion.
I am managing this through commands:
 %run ./notebook.ipynb --inputParameters
and then 
subprocess.call(["jupyter", "nbconvert" ,"--to=pdf","--template=template.tplx", "notebook.ipynb"]) 

Having this setup it misses the save and doesn't have the updated fields.

Any suggestion to fix this ?

Thomas Kluyver

unread,
Dec 20, 2017, 5:30:01 AM12/20/17
to Project Jupyter
You could use the --execute option when you call nbconvert.

To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.

To post to this group, send email to jup...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages