Hi Brendan,regarding your fallback solution: the notebook server is Python code. You can import the packages and call the functions to start a notebook server in the current process. The 'jupyter' command itself is a Python script, so just follow the code path to see what it does when invoked with certain options. Once you get to the point where the notebook server is started, you'll know how to do that directly from your code. You'll probably have to follow the code path a bit further to see where the token is generated and stored.
What I would like is the ability to start a notebook server that would automatically shut down once the associated browser is closed. Looking at old messages on the list, I get the impression this isn't possible. Is that correct?
It seems like it would be possible to implement this by having JavaScript in the notebook page send keepalive messages back to the server. These would serve as a sort of "dead man's switch" --- as long as the page remained open, the server would remain alive, but when the browser (or tab in a multi-tab browser) was closed, the cessation of keepalive messages would tell the server to shut itself down. Is this feasible?
Failing that, I've also been looking for documentation on how to programmatically start the notebook server. That is, supposing I'm running some Python code, how can I, from within that code, do the equivalent of "jupyter notebook --no-browser", and get the login token returned as a string so that I can redirect my wrapper to the appropriate URL? Right now the only way I can see is to actually run jupyter using subprocess and parse the output, but that seems rather awkward. Is there a part of the notebook that I can import as a module and do something like "server = notebook.start_server()" and then "token = server.token"?
On 8 June 2017 at 04:04, Brendan Barnwell <bren...@gmail.com> wrote:What I would like is the ability to start a notebook server that would automatically shut down once the associated browser is closed. Looking at old messages on the list, I get the impression this isn't possible. Is that correct?With a special-purpose browser that knows about the notebook server, yes - the browser can either use SIGTERM when it closes to shut down the notebook server, or when notebook 5.1 is released, it will be able to POST to /api/shutdown
https://github.com/jupyter/notebook/pull/2507
It seems like it would be possible to implement this by having JavaScript in the notebook page send keepalive messages back to the server. These would serve as a sort of "dead man's switch" --- as long as the page remained open, the server would remain alive, but when the browser (or tab in a multi-tab browser) was closed, the cessation of keepalive messages would tell the server to shut itself down. Is this feasible?Feasible, and we already monitor connectivity to the kernel, but I don't think we'd ever do that by default - we don't want to kill the notebook server if your connection to it goes down. Even locally, 'keep alive' messages can be tricky to get right when computers go into standby.
Plus, of course, many people expect their notebook server to stay running without an open browser tab on it.
Failing that, I've also been looking for documentation on how to programmatically start the notebook server. That is, supposing I'm running some Python code, how can I, from within that code, do the equivalent of "jupyter notebook --no-browser", and get the login token returned as a string so that I can redirect my wrapper to the appropriate URL? Right now the only way I can see is to actually run jupyter using subprocess and parse the output, but that seems rather awkward. Is there a part of the notebook that I can import as a module and do something like "server = notebook.start_server()" and then "token = server.token"?Not quite as neat as that, but you can get info on the current user's running notebook servers by calling:notebook.notebookapp.list_running_servers()You should be able to find the one you've just started as a subprocess by checking the PIDs. That's easier than parsing info from stdout/stderr.
One thing I'm wondering is whether there's a way to get any of this information by communicating with the notebook server itself.
It would be nice if I could write my wrapper so it really acted as a browser from Jupyter's perspective, with the notebook starting it rather than the other way around. The reason I'd feel more comfortable with this is that it would make the wrapper more independent of Jupyter, and thus (I hope) more robust to a situation where the wrapper is, for instance, running on a different version of Python than the notebook itself.
For instance, I'd like it if I could have my wrapper installed in just one Python environment (an Anaconda environment, say), but be able to run notebooks from arbitrary Python versions. If I call list_running_servers from one Python process, is it guaranteed to show me all servers, even if they're running on different versions of Python?
On 8 June 2017 at 18:58, Brendan Barnwell <bren...@gmail.com> wrote:One thing I'm wondering is whether there's a way to get any of this information by communicating with the notebook server itself.Most stuff can be done by communicating with the server. Finding out how to communicate with it, such as what port it's running on, obviously can't. ;-)
It would be nice if I could write my wrapper so it really acted as a browser from Jupyter's perspective, with the notebook starting it rather than the other way around. The reason I'd feel more comfortable with this is that it would make the wrapper more independent of Jupyter, and thus (I hope) more robust to a situation where the wrapper is, for instance, running on a different version of Python than the notebook itself.This is a sensible rationale. At the moment, I don't think that a browser launched by the notebook server has any specific knowledge of that, except for the URL it's asked to load.If it's useful, I think we could add an environment variable when we launch the browser, like JUPYTER_NOTEBOOK_PORT, so that a special-purpose browser like yours could use that. You can probably get that information from the URL, though.
On 8 June 2017 at 04:04, Brendan Barnwell <bren...@gmail.com> wrote:What I would like is the ability to start a notebook server that would automatically shut down once the associated browser is closed. Looking at old messages on the list, I get the impression this isn't possible. Is that correct?With a special-purpose browser that knows about the notebook server, yes - the browser can either use SIGTERM when it closes to shut down the notebook server, or when notebook 5.1 is released, it will be able to POST to /api/shutdown
https://github.com/jupyter/notebook/pull/2507
Anyway I guess I'll just wait with bated breath for the 5.1 release.