jupyter / jupyter_client

Jupyter protocol client APIs
https://jupyter-client.readthedocs.io
BSD 3-Clause "New" or "Revised" License
383 stars 283 forks source link

Documentation is not so clear, I don't know how to proceed #900

Closed santiagobasulto closed 1 year ago

santiagobasulto commented 1 year ago

I'm sorry in advance if this is a terribly n00b question. I have a jupyter server running, with a specific Notebook that has processed, and I'm trying to programmatically connect to it and execute some arbitrary code [0].

I know that somehow I have to get a BlockingKernelClient instance (and then use the execute or execute_interactive) potentially using a KernelManager but I just don't know how to do it.

I see everywhere that these managers receive a config, but I don't see much about it in the documentation.

If you could point me in the right direction I'd be glad to contribute some more documentation for more n00bs like me :)

[0] There is a dataframe in memory which has some partial state that I want to log/process. I need to get that intermediate state in memory of the dataframe that is always in the df variable.

So, I just need to somehow do (in pseudocode):

# using Jupyter Server API
kernel_id = find_kernel_id_for_notebook("MyNotebookRunning.ipynb")

# using jupyter-client
client = get_kernel(kernel_id=kernel_id)
result = client.execute("""
df.describe() # whatever
""")

# log the result
davidbrochart commented 1 year ago

811 could help.

santiagobasulto commented 1 year ago

That was useful, thanks very much! After digging with Jupyter runtimes and the issue you linked I was able to instantiate a BlockingKernelClient and execute code in an arbitrary Kernel of my choice.

Is the BlockingKernelClient.execute() the preferred method to execute some code on that kernel and get the result?[0] Or should I use execute_interactive and capture stdout to get what I need.

Thanks!

[0] I see I receive a message ID of some sort, I assume I can then check the stdout/stderr of that message?

davidbrochart commented 1 year ago

Maybe take inspiration from nbclient?

santiagobasulto commented 1 year ago

Thanks, will do that, I think I can contribute some docs about runtimes now if that's ok with the project. I'll go ahead and close this issue. Thank you again @davidbrochart !