Marc Liyanage
Marc Liyanage

Reputation: 4691

Is there an IPython notebook server status query?

I am embedding the notebook server in a standalone OS X app, where the server is launched in a subprocess (via NSTask) and the app connects to that process with HTTP and renders the web front end in a WebView.

I would like the OS X app part to be able to query the server subprocess for various bits of status information, the most important one being if there are any notebooks open with unsaved changes. I was wondering if there is some way to query this at a special URL, for example something like

http://127.0.0.1:8888/status.json

Another thing I'd want to do is to control the notebook server, for example to tell it to save any unsaved changes, before it gets shut down.

Upvotes: 1

Views: 382

Answers (1)

minrk
minrk

Reputation: 38648

There is not a general 'status' query, but you can view a JSON summary of existing notebooks with a GET request to

http://127.0.0.1:8888/notebooks

Which will give you a list of dicts of the form:

[
 {
  "kernel_id": null, 
  "name": "Animations Using clear_output", 
  "notebook_id": "49222a70-b746-4fb2-9b96-fe1a61e82979"
 }, 
 {
  "kernel_id": "2e8de018-c816-4222-82d2-4a35cfa95f1c", 
  "name": "Cell Magics", 
  "notebook_id": "38ce96ab-b456-4af1-b68d-44a1a3ce86ee"
 }
]

Where kernel_id=null means that the notebook is not running, and kernel_id=U-U-I-D means it is.

the most important one being if there are any notebooks open with unsaved changes

unsaved changes cannot be known, because this information only lives in the browser - the server doesn't even know that the document has been edited, so there's nothing to query.

Another thing I'd want to do is to control the notebook server, for example to tell it to save any unsaved changes, before it gets shut down.

This is not possible, because the live document does not actually live in the server. The edited document only lives browser-side in the browser window editing the notebook.

If you want to check unsaved changed and/or trigger saves, you must do this by interacting with the javascript in your WebViews - the notebook object in js has a 'dirty' flag, which indicates that there are unsaved changes. So 'save any unsaved changes' would be:

if (IPython.notebook.dirty) { IPython.notebook.save_notebook(); }

tested with IPython 1.0.dev

Upvotes: 4

Related Questions