graceful interrupt of while loop in ipython notebook

wenhoo picture wenhoo · May 17, 2016 · Viewed 8k times · Source

I'm running some data analysis in ipython notebook. A separate machine collects some data and saves them to a server folder, and my notebook scans this server periodically for new files, and analyzes them.

I do this in a while loop that checks every second for new files. Currently I have set it up to terminate when some number of new files are analyzed. However, I want to instead terminate upon a keypress.

I have tried try-catching a keyboard interrupt, as suggested here: How to kill a while loop with a keystroke?

but it doesn't seem to work with ipython notebook (I am using Windows).

Using openCV's keywait does work for me, but I was wondering if there are alternative methods without having to import opencv.

I have also tried implementing a button widget that interrupts the loop, as such:

from ipywidgets import widgets 
import time
%pylab inline

button = widgets.Button(description='Press to stop')
display(button)

class Mode():
    def __init__(self):
        self.value='running'

mode=Mode()

def on_button_clicked(b):
    mode.value='stopped'

button.on_click(on_button_clicked)

while True:
    time.sleep(1)
    if mode.value=='stopped':
        break

But I see that the loop basically ignores the button presses.

Answer

Mike Müller picture Mike Müller · May 17, 2016

You can trigger a KeyboardInterrupt in a Notebook via the menu "Kernel --> Interrupt".

So use this:

try:
    while True:
        do_something()
except KeyboardInterrupt:
    pass

as suggested here and click this menu entry.