JupyterLab 3: how to get the list of running servers

1.1k Views Asked by At

Since JupyterLab 3.x jupyter-server is used instead of the classic notebook server, and the following code does not list servers served with jupyter_server:

from notebook import notebookapp 
notebookapp.list_running_servers()
None

What still works for the file/notebook name is:

from time import sleep
from IPython.display import display, Javascript
import subprocess
import os
import uuid

def get_notebook_path_and_save():
    magic = str(uuid.uuid1()).replace('-', '')
    print(magic)
    # saves it (ctrl+S)
    # display(Javascript('IPython.notebook.save_checkpoint();')) # Javascript Error: IPython is not defined
    nb_name = None
    while nb_name is None:
        try:
            sleep(0.1)
            nb_name = subprocess.check_output(f'grep -l {magic} *.ipynb', shell=True).decode().strip()
        except:
            pass
    return os.path.join(os.getcwd(), nb_name)

But it's not pythonic nor fast


How to get the current running server instances - and so e.g. the current notebook file?

1

There are 1 best solutions below

0
On BEST ANSWER

Migration to jupyter_server should be as easy as changing notebook to jupyter_server, notebookapp to serverapp and changing the appropriate configuration files - the server-related codebase is largely unchanged. In the case of listing servers simply use:

from jupyter_server import serverapp
serverapp.list_running_servers()