Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep Jupyter notebook running after closing browser tab

I use Jupyter Notebook to run a series of experiments that take some time. Certain cells take way too much time to execute so it's normal that I'd like to close the browser tab and come back later. But when I do the kernel interrupts running.

I guess there is a workaround for this but I can't find it

like image 798
Flo Avatar asked Sep 12 '15 14:09

Flo


People also ask

Can I run jupyter notebook without browser?

Step 1: Run Jupyter Notebook from remote machine Log-in to your remote machine the usual way you do. In most cases, this is simply done via an ssh command. Once the console shows, type the following: remoteuser@remotehost: jupyter notebook --no-browser --port=XXXX # Note: Change XXXX to the port of your choice.

How do you make Jupyter notebooks run automatically?

Open a Jupyter notebook from the left sidebar. Click on the Scheduler icon either from the left sidebar tab or from the top toolbar of the Jupyter notebook. The left sidebar displays the Schedule(s) and Run History tabs as shown below. To view the active schedules, click Schedule(s) tab.

How do I save my jupyter notebook browser?

Saving a Jupter notebookThere is a disk icon in the upper left of the Jupyter tool bar. Click the save icon and your notebook edits are saved. It's important to realize that you will only be saving edits you've made to the text sections and to the coding windows. You will NOT be saving the results of running the code.


2 Answers

The simplest workaround to this seems to be the built-in cell magic %%capture:

%%capture output # Time-consuming code here 

Save, close tab, come back later. The output is now stored in the output variable:

output.show() 

This will show all interim print results as well as the plain or rich output cell.

like image 113
Seb Avatar answered Sep 26 '22 00:09

Seb


TL;DR:

Code doesn't stop on tab closes, but the output can no longer find the current browser session and loses data on how it's supposed to be displayed, causing it to throw out all new output received until the code finishes that was running when the tab closed.

Long Version:

Unfortunately, this isn't implemented (Nov 24th). If there's a workaround, I can't find it either. (Still looking, will update with news.) There is a workaround that saves output then reprints it, but won't work if code is still running in that notebook. An alternative would be to have a second notebook that you can get the output in.

I also need this functionality, and for the same reason. The kernel doesn't shut down or interrupt on tab closes. And the code doesn't stop running when you close a tab. The warning given is exactly correct, "The kernel is busy, outputs may be lost."

Running

import time a = 0 while a < 100:     a+=1     print(a)     time.sleep(1) 

in one box, then closing the tab, opening it up again, and then running

print(a) 

from another box will cause it to hang until the 100 seconds have finished and the code completes, then it will print 100.

When a tab is closed, when you return, the python process will be in the same state you left it (when the last save completed). That was their intended behavior, and what they should have been more clear about in their documentation. The output from the run code actually gets sent to the browser upon reopening it, (lost the reference that explains this,) so hacks like the one in this comment will work as it can receive those and just throw them into some cell.

Output is kind of only saved in an accessible way through the endpoint connection. They've been working on this for a while (before Jupyter), although I cannot find the current bug in the Jupyter repository (this one references it, but is not it).

The only general workaround seems to be finding a computer you can always leave on, and leaving that on the page while it runs, then remote in or rely on autosave to be able to access it elsewhere. This is a bad way to do it, but unfortunately, the way I have to for now.

Related questions:

  • Closed IPython Notebook that was running code
    • Confirms that output will not be updated, but does not mention the interrupt functionality.
  • IPython Notebook - Keep printing to notebook output after closing browser
    • Offers a workaround in a link. Referenced above
like image 41
Poik Avatar answered Sep 27 '22 00:09

Poik