[docs]defcurrentNBpath():"""Returns the absolute path of the Notebook or None if it cannot be determined NOTE: works only for *Jupyter Notebook* (not Jupyter Lab) and when the security is token-based or there is also no password. """connection_file=os.path.basename(ipykernel.get_connection_file())kernel_id=connection_file.split("-",1)[1].split(".")[0]forsrvinnotebookapp.list_running_servers():try:ifsrv["token"]==""andnotsrv["password"]:# No token and no password, ahem...req=urllib.request.urlopen(srv["url"]+"api/sessions")else:req=urllib.request.urlopen(srv["url"]+"api/sessions?token="+srv["token"])sessions=json.load(req)forsessinsessions:ifsess["kernel"]["id"]==kernel_id:returnos.path.join(srv["notebook_dir"],sess["notebook"]["path"])exceptOSError:pass# There may be stale entries in the runtime directoryreturnNone