Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython Notebook - early exit from cell

I'd like to programmatically exit a cell early in IPython Notebook. exit(0), however, kills the kernel.

Whats the proper way to do this? I'd prefer not to split the cell or manually halt execution.

like image 999
watsonic Avatar asked Jun 03 '14 00:06

watsonic


People also ask

How do you exit a cell in Jupyter Notebook?

Just import 'exit' from the code beneath into your jupyter notebook (IPython notebook) and calling 'exit()' should work. It will exit and letting you know that...

What does %% capture do?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.

Does exit work in Jupyter Notebook?

IPython recognizes objects of this type, so when a line containing just exit or quit is executed, IPython actually exits. A Jupyter notebook's IPython kernel has exit and quit set to instances of the very closely related IPython.

How do you jump to the next cell in Jupyter Notebook?

Shift + Enter run the current cell, select below. Ctrl + Enter run selected cells. Alt + Enter run the current cell, insert below. Ctrl + S save and checkpoint.


1 Answers

Slightly more "proper" options:

This will get you out of all but the worst try/except blocks.

raise KeyboardInterrupt 

A little cleaner version of yours:

assert(False) 

or simply:

raise 

if you want to save a couple keystrokes.

like image 63
Paul Avatar answered Sep 20 '22 15:09

Paul