6

How it works? I got some code running in an IPython Notebook. Some iterative work.

Accidentally I closed the browser with the running Notebook, but going back to the IPython Dashboard I see that this particular Notebook hasn't Shutdown, so if I open the Notebook again I see the [*] in front of my code that it was executing.

I even can hear my PC still running the code, but it doesn't return me any new output of the print statements.

Can I wait and eventually continue with the output, or my PC will still running my code, but it won't be accessible anymore?

Mattijn
  • 12,975
  • 15
  • 45
  • 68
  • Yes, once you close the browser it no longer displays the output. One solution is to re-route the STDout to file so you can access it later. See related post: http://stackoverflow.com/questions/29119657/ipython-notebook-keep-printing-to-notebook-output-after-closing-browser/29170902#29170902 – Peter Apr 22 '15 at 08:46

3 Answers3

4

When you start up ipython, it is essentially creating a web server that is running on a separate process. The code itself is running on the web server, or kernel. The web browser is simply one of several front-ends that can view and edit the code on the kernel.

This design allows ipython to separate the evaluation of code from the viewing and editing of code -- for example, I could access the same kernel via the web interface (ipython notebook), the console (ipython console), or the qt console interface (ipython qtconsole).

Your PC will continue to run the code, though I believe that the output requested by one frontend will not show on any other frontends using the same kernel (I'm not 100% certain about this though).

You can find more information here.

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
  • That's useful information, thanks, if I understand correctly the kernel stays active and the frontend is flexible. But that's suggesting that I could access to the kernel again with another frontend. But how to get the %connect_info if the Notebook stays in phase of [*]. – Mattijn Dec 20 '13 at 09:49
  • 4
    This is a limitation we have for now, when you close the html page, the javascript loose the information about which cell will received which output in the future(those a store in callbacks), we will fix that at some point in the future. One of the only thing you can do is re-access a previous output with `Out[]`. – Matt Dec 20 '13 at 13:27
  • When I close the browse the * goes away. How can I know if it's still running? – jwillis0720 Oct 16 '15 at 04:08
  • One possibility is to run a top on your machine to see if python code is still running. – Devil Nov 07 '15 at 17:12
  • How about when running the entire notebook via the menu command "Run all"? Does execution stop after one cell? – IanS May 16 '18 at 16:23
2

I am struggling with this issue as well for some time now. The kernel keeps running your job on the server, but there is no way to see the console output after closing the browser.

My workaround was to write all my logs to a file, so that when my browser closes (indeed when a lot of logs come through browser it hangs up too) I can see the kernel job process by opening the log file (the log file can be open using Jupyter too).

#!/usr/bin/python
import time
import datetime
import logging

logger = logging.getLogger()

def setup_file_logger(log_file):
    hdlr = logging.FileHandler(log_file)
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr) 
    logger.setLevel(logging.INFO)

def log(message):
    #outputs to Jupyter console
    print('{} {}'.format(datetime.datetime.now(), message))
    #outputs to file
    logger.info(message)

setup_file_logger('out.log')

for i in range(10000):
    log('Doing hard work here i=' + str(i))
    log('Taking a nap now...')
    time.sleep(1000)
0

use this code to save the output to file

import time
from threading import Thread
import sys
#write the stdout to file
def log():
    #for stop the thread
    global run
    while (run):
        try:
            global out
            text = str(sys.stdout.getvalue())
            with open("out.txt", 'w') as f:
                f.write(text)
        finally:
            time.sleep(1)
%%capture out
run = True
print("start")
process = Thread(target=log, args=[]).start()

# do some work
for i in range(10, 1000):
    print(i)
    time.sleep(1)
run= False
process.join()

It is useful to use a text editor that tracer changes the file and suggest reloading the file like notepad++

Raful Chizkiyahu
  • 364
  • 2
  • 15