IPython notebook - configure closing the kernel when closing the tab

The official website says :

When the laptop opens, its "computing engine" (called the kernel) starts automatically. Closing the laptop browser tab will not disable the kernel; instead, the kernel will continue to work until it is explicitly disabled. "

Is it possible to configure the iPython server so that the kernel is killed along with the associated tab?

+8
python ipython-notebook
source share
3 answers

After some research and numerous attempts, the solution is extremely simple. Add the following code to the end of the profile/static/custom/custom.js :

 $( window ).unload(function() { IPython.notebook.kernel.kill(); }); 

Works great for me! (tested on Chrome)

+6
source share

You can write the extension in jupyter ( more about jupyter extensions for laptops ). I cannot find information on laptop extensions in earlier versions of IPython. However, laptop extensions are customizable and much prettier than custom.js

Be careful with unload . My chrome 43.0.2357.130 does not start unload when redirecting.

I wanted to write a comment for Nikolai, but I do not have enough points ...

+2
source share

I think IPython.notebook.session.delete() better than IPython.notebook.kernel.kill() .

 window.addEventListener('unload', function() { // For Firefox IPython.notebook.session.delete(); }); window.onbeforeunload = function () { // For Chrome IPython.notebook.session.delete(); }; 

Cause

Because kernel.kill() will just kill the kernel. This will not tell the laptop session manager about the kill. Therefore, after killing, sessionmanager raises a KernelError when you open the same ipynb file again that you closed on kernel.kill() (closed tabs or windows).

About unloading

In GoogleChrome, unloading events will not be triggered on moving pages. So, I think you should also catch the beforeunload event

But intercepting addEventListener('beforeunload', ...) did not work. So I tried window.onbeforeunload = and it works fine.

0
source share

All Articles