Reputation: 2297
My notebook is located on a server, which means that the kernel will still run even though I close the notebook tab. I was thus wondering if it was possible to let the cell running by itself while closing the window? As the notebook is located on a server the kernel will not stop running...
I tried to read previous questions but could not find an answer. Any idea on how to proceed? Thanks!
Upvotes: 1
Views: 904
Reputation: 1
Jupyter has cell magic that can solve this
%%capture foobar
allows you to capture the cell's stdout into a variable, and you can print it with foobar.show()
in the same cell.
This does however stop you from seeing the output of the cell while it's running, so there is a solution to this by making a custom magic startup script.
How to simultaneously capture and display ipython (jupyter notebook) cell output?
I made a file in ~/.ipython/profile_default/startup/
and named it 00-tee.py
with the content being the code at the bottom. Give it executable permissions, then when you load a kernel, it automatically executes this code on startup.
Now you can use %%tee foobar
and show the output by using foobar()
so that the output will still be displayed on the screen while running, and you can recall it after reopening the session.
from IPython import get_ipython
from IPython.core import magic_arguments
from IPython.core.magic import register_cell_magic
from IPython.utils.capture import capture_output
@magic_arguments.magic_arguments()
@magic_arguments.argument('output', type=str, default='', nargs='?',
help="""The name of the variable in which to store output.
This is a utils.io.CapturedIO object with stdout/err attributes
for the text of the captured output.
CapturedOutput also has a show() method for displaying the output,
and __call__ as well, so you can use that to quickly display the
output.
If unspecified, captured output is discarded.
"""
)
@magic_arguments.argument('--no-stderr', action="store_true",
help="""Don't capture stderr."""
)
@magic_arguments.argument('--no-stdout', action="store_true",
help="""Don't capture stdout."""
)
@magic_arguments.argument('--no-display', action="store_true",
help="""Don't capture IPython's rich display."""
)
@register_cell_magic
def tee(line, cell):
args = magic_arguments.parse_argstring(tee, line)
out = not args.no_stdout
err = not args.no_stderr
disp = not args.no_display
with capture_output(out, err, disp) as io:
get_ipython().run_cell(cell)
if args.output:
get_ipython().user_ns[args.output] = io
io()
Upvotes: 0
Reputation: 66
You can make open a new file and write outputs to it. I think that's the best that you can do.
Upvotes: 1
Reputation: 157
If you run the cell before closing the tab it will continue to run once the tab has been closed. However, the output will be lost (anything using print functions to stdout or plots which display inline) unless it is written to file.
Upvotes: 1