Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can a Jupyter notebook programmatically halt itself? #1880

Closed
jeremydouglass opened this issue Nov 8, 2016 · 6 comments

Comments

Projects
None yet
4 participants
@jeremydouglass
Copy link

commented Nov 8, 2016

Can a Jupyter notebook programmatically halt itself, such that the notebook no longer appears in the "Running" tab?

At the end of a notebook "Run All", I would like the last notebook cell to contain code, and for that code to trigger the equivalent of:

  • the "Running tab > Shutdown notebook" action
  • ...or, failing that, the "Close and Halt" menu item

Can this be done with javascript, or with %magic, or something else?

I am running the latest Docker image Jupyer Datascience Notebook, not JupyterHub.


forum cross-posted

@takluyver

This comment has been minimized.

Copy link
Member

commented Nov 9, 2016

(Responded on the mailing list)

@jeremydouglass

This comment has been minimized.

Copy link
Author

commented Nov 10, 2016

Yes, a Jupyter notebook can programmatically halt itself.

Use a cell at the end of an iPython notebook like this:

%%javascript
Jupyter.notebook.session.delete();
  • On "Run All" the final %%javascript cell "Jupyter.notebook.session.delete();" correctly stops the notebook.
  • The notebook interface shows "No Kernel", the Running tab no longer lists the notebook.
  • The previous cell incorrectly still shows a running status [ * ], however the output from that cell has already returned. This can be misleading, but seems minor -- I can report it as a separately issue if anyone cares.

Restart the stopped notebook with:

  • Menu > Kernel > Restart ... "Dead Kernel" warning > Try restarting now
  • ...or refresh the browser tab.

A demo ipynb file is posted on the forum thread

@takluyver

This comment has been minimized.

Copy link
Member

commented Nov 12, 2016

This led to a question: should it be possible to use exit() to stop the kernel in a notebook? At present, using exit() will make the notebook think that the kernel has died, and it will automatically start a new kernel. I think that exit() should probably put the notebook interface into the state it arrives in if it gives up on trying to start a kernel.

@takluyver takluyver added this to the 5.0 milestone Nov 12, 2016

@jeremydouglass

This comment has been minimized.

Copy link
Author

commented Nov 16, 2016

As a current workaround for those interested in notebook halting, I developed this %%javascript cell for use at the end of a Run All sequence. It uses a modal dialog to halt the notebook kernal (and optionally restart it on user click).


Notebook Halt / Restart cell

When included at the bottom of a notebook, it will stop the notebook at the end of Run All, and pop up a modal dialog letting the user know that the notebook was auto-stopped at the end of the run. The dialog offers a "Kernel Restart" button which restarts the notebook.

%%javascript
require(
    ["base/js/dialog"], 
    function(dialog) {
        dialog.modal({

                title: 'Notebook Halted',
                body: 'This notebook is no longer running; the kernel has been halted. Close the browser tab, or, to continue working, restart the kernel.',
                buttons: {
                    'Kernel restart': { click: function(){ Jupyter.notebook.session.restart(); } }
                }
        });
    }
);
Jupyter.notebook.session.delete();

https://groups.google.com/forum/#!topic/jupyter/3zAVHofQykg

@minrk minrk modified the milestones: 5.1, 5.0 Jan 25, 2017

@ivanov

This comment has been minimized.

Copy link
Member

commented Jan 27, 2017

Related to this, in #2070, which was recently merged, I added a "Shutdown" menu item to the Kernel menu.

@ivanov

This comment has been minimized.

Copy link
Member

commented Mar 2, 2017

Hey there, I'm going through old issues and it seems to me that it makes sense to close this one.

@jeremydouglass figured out a workaround in the forum link posted two comments above this one, and I added "Shutdown" item to the Kernel menu.

Thanks everyone and happy hacking! :bowtie:

@ivanov ivanov closed this Mar 2, 2017

@minrk minrk modified the milestones: No Action, 5.1 Mar 3, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.