There are a couple of apps that can do that (vs code and pycharm pro), I’m trying to reproduce the same functionality.
Say, jupyter is running on a server, and localhost:8889 is forwarding to jupyter.
I’d like to have a way to connect to jupyter (prefer from python), start a kernel, send requests and get output back.
I think jupyter_client is the package I should use, but I did not find any confirmation to that. Any working example (connect, send ‘print(1)’, get response) is highly appreciated.
jupyter_client is the library for providing that capability. It’s not documented super well for new users of jupyter systems, so what I’d recommend is to read nbclient’s source code which manages executing a notebook using jupyter_client. This is the library papermill is usually using to handle the low level details of managing cell executions. It has the best working examples of how to communicate with a jupyter_client. You may want to just use that library directly if you’re doing notebook-like execution patterns rather than interfacing with jupyter_client as it handles many of the message protocol concerns above the launching and message propegation mechanisms. An example of doing this can be seen in testbook.
@MSeal thanks a lot for you response.
I’ve jumped over the links you provided and I think it exhaustively answers a question how to make queries to a local jupyter.
Additional restriction in my case is ability to use remote jupyter that’s available on localhost with port-forwading.
I can miss something very simple here, but I don’t see such an example in nbclient and testbook.
(Thing I’m asking about is certainly possible as vscode and pycharm pro can do this)
You’re right that there isn’t a good RemoteKernelManager class to extend or pull as an example.
To achieve what you’re aiming for you need to define a KernelManager class that either doesn’t start / stop clients or calls some other API to launch a kernel. You’ll want that kernel manager to pass the ports needed to the KernelClient. Then either set the kernel_manager_class of the NotebookClient or pass an instance of your new KernelManager as the km arg to the NotebookClient init.