unsorted
unsorted

Reputation: 3274

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?

Upvotes: 11

Views: 1692

Answers (3)

Oleg Polosin
Oleg Polosin

Reputation: 101

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'

Upvotes: 2

Piotr Kamoda
Piotr Kamoda

Reputation: 1006

If I understood correctly you wish to open a Python console, and connect Jupyter notebook to that kernel instance?

Perhaps your solution would be to edit jupyter scripts itself and run the server in separate thread/background task implementing some sort of connection between threads and work in the jupyter console? Currently it's impossible because main thread is running the server.

This would require some work and I don't have any solution as-is, but I will look into that and maybe edit this answer if I can make it work.

But it seems that the easiest solution is to simply add another field in the notebook and do whatever you wish to do there. Is there a reason for not doing that?

Upvotes: 1

Haipeng Su
Haipeng Su

Reputation: 2541

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.

Upvotes: 1

Related Questions