takluyver / nbopen

Open a Jupyter notebook in the best available server
BSD 3-Clause "New" or "Revised" License
301 stars 58 forks source link

Open without Browser #59

Closed teddyrendahl closed 5 years ago

teddyrendahl commented 5 years ago

Hello,

I'm trying to modify your code a little bit to launch a notebook kernel without the browser. It appears that just setting cfg.NotebookApp.open_browser = False doesn't do the trick. Could you point me in the right direction on how to launch one of these kernels sans web browser?

takluyver commented 5 years ago

Nbopen is really about getting you to the interface, so I'm not sure how useful it will be if you don't want to do that.

There are two layers at which you could launch a kernel, depending on your needs:

  1. You can launch a kernel directly from your own code, using the jupyter_client Python API, so your process is the parent process. E.g. nbconvert does this when you run a notebook with nbconvert --execute. Here's the code for that: https://github.com/jupyter/nbconvert/blob/5.4/nbconvert/preprocessors/execute.py
  2. If you know there is a notebook server running, you can make an HTTP request to ask it for a kernel, passing the path from the notebook server's root directory to the notebook file. Here's the API call you'll want. nbopen might help you figure out the path, but it then opens the interface, which makes the relevant API calls from Javascript. You'll want to make the API call directly.
teddyrendahl commented 5 years ago

Thank you for following up @takluyver. Much appreciated.

teddyrendahl commented 5 years ago

You seem to have sent the same link twice. Would you mind pointing me towards the HTTP request API?

takluyver commented 5 years ago

Certainly, here it is: https://github.com/jupyter/jupyter/wiki/Jupyter-Notebook-Server-API#sessions-api

It's somewhat brief, so you might need to look at some code to understand how to pass the parameters. Here's the JS code which makes an AJAX request to start a session using the REST API:

https://github.com/jupyter/notebook/blob/7ded6383572b7bb4ae095c9bbeb3a6a0e0e060fd/notebook/static/services/sessions/session.js#L124-L133

(A session associates a notebook path with a kernel. Starting a session will automatically start a kernel for you if necessary)