Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute jupyter notebook and keep attached to existing kernel

I know about nbconvert and use it to generate static html or ipynb files with the results output. However, I want to be able to generate a notebook that stays attached to a kernel that I already have running, so that I can do further data exploration after all of the template cells have been run. Is there a way to do that?

like image 204
unsorted Avatar asked Nov 02 '16 01:11

unsorted


People also ask

How do you keep a running Jupyter Notebook?

This can be done by typing jupyter notebook in the terminal, which will open a browser. Then, navigate to the respective jupyter notebook file in the browser and open it. Click Cell > Run All on the toolbar. All done!

How do you open a notebook in the same kernel Jupyter?

From the left Sidebar, select and right-click on the Jupyter notebook that has to be run from another notebook. From the context menu, select Copy Path. Open the Jupyter notebook from which you want to run another notebook. Click Run.

Can you embed a Jupyter Notebook?

Embedding a notebook is very straightforward. Just grab the URL of the hosted notebook and put it as an iframe on your website. The below code is an example. Unfortunately, most third-party sites don't support such embeds.

Why do we need to install a kernel before running Jupyter Notebook?

The IPython kernel is the Python execution backend for Jupyter. The Jupyter Notebook and other frontends automatically ensure that the IPython kernel is available. However, if you want to use a kernel with a different version of Python, or in a virtualenv or conda environment, you'll need to install that manually.


2 Answers

Apparently, you can do this through the Python API. I didn't try it myself, but for someone who will be looking for a solution, this PR has an example in the comments:

from nbconvert.preprocessors.execute import executenb, ExecutePreprocessor
from nbformat import read as nbread
from jupyter_client.manager import start_new_kernel

nb = nbread('parsee.ipynb', as_version=4)
kernel_name = nb.metadata.get('kernelspec', {}).get('name', 'python')
km, kc = start_new_kernel(kernel_name=kernel_name)
executenb(nb, kernel=(km, kc))
kc.execute_interactive('a')  # a is a variable defined in parsee.ipynb with 'a = 1'
like image 163
Oleg Polosin Avatar answered Oct 13 '22 13:10

Oleg Polosin


Not quite sure about your purpose. But my general solutions are,

  1. to execute the notebook in command line and see the execution at the same time,

    jupyter nbconvert --debug --allow-errors --stdout --execute test.ipynb

this will show the execute through all cells in debug mode even exception happens. but I can't see the result until the end of the execution.

  1. to output the result to a html file, and then open the html file to see the results. I found this is more convenient.

    jupyter nbconvert --execute --allow-errors --stdout test.ipynb >> result.html 2>&1

if you open result.html, it will be, enter image description here

and all the errors and results will be shown on the page.

I would like to learn other answers/solutions from you all. thank you.

like image 40
Haipeng Su Avatar answered Oct 13 '22 12:10

Haipeng Su