def find_best_server(filename, profile='default'): servers = [si for si in notebookapp.list_running_servers(profile=profile) \ if filename.startswith(si['notebook_dir'])] try: return max(servers, key=lambda si: len(si['notebook_dir'])) except ValueError: return None
def get_notebook_name(): """ Fetch current notebook's name through IPython Parameters ---------- None Return ------ notebook_name : string name of currently running notebook Notes ----- The python in this function could be replaced with Javascript """ def sess_open(url): try: urllib2.urlopen(url + 'api/sessions') return True except: return False load = lambda url: json.loads( urllib2.urlopen(url + 'api/sessions').fp.read()) base = lambda path: os.path.basename(path) nbpath = lambda session: base(session['notebook']['path']) nbid = lambda session: session['kernel']['id'] connection_file_path = kernel.get_connection_file() connection_file = base(connection_file_path) kernel_id = connection_file.split('-', 1)[1].split('.')[0] sessions = [ load(data['url']) for data in list_running_servers() if sess_open(data['url']) ] sessions = reduce(list.__add__, sessions) if isinstance(sessions[0], list) else sessions notebook_name = [ nbpath(sess) for sess in sessions if nbid(sess) == kernel_id ] if notebook_name: return notebook_name[0] else: sys.stderr.write('No notebook name was found. Export manually.') sys.stderr.flush()
def get_notebook_name(): """ Fetch current notebook's name through IPython Parameters ---------- None Return ------ notebook_name : string name of currently running notebook Notes ----- The python in this function could be replaced with Javascript """ def sess_open(url): try: urllib2.urlopen(url+'api/sessions') return True except: return False load = lambda url: json.loads(urllib2.urlopen(url+'api/sessions').fp.read()) base = lambda path: os.path.basename(path) nbpath = lambda session: base(session['notebook']['path']) nbid = lambda session: session['kernel']['id'] connection_file_path = kernel.get_connection_file() connection_file = base(connection_file_path) kernel_id = connection_file.split('-', 1)[1].split('.')[0] sessions = [load(data['url']) for data in list_running_servers() if sess_open(data['url'])] sessions = reduce(list.__add__, sessions) if isinstance(sessions[0], list) else sessions notebook_name = [nbpath(sess) for sess in sessions if nbid(sess) == kernel_id] if notebook_name: return notebook_name[0] else: sys.stderr.write('No notebook name was found. Export manually.') sys.stderr.flush()
def find_best_server(filename, profile='default'): return max([si for si in notebookapp.list_running_servers(profile=profile) \ if filename.startswith(si['notebook_dir'])], key=lambda si: len(si['notebook_dir']), default=None)
def get_servers(): return list(notebookapp.list_running_servers(profile='nbserver_file_test'))
def get_servers(): return list( notebookapp.list_running_servers(profile='nbserver_file_test'))
def findall(cls): return [cls(info) for info in list_running_servers()]