def stop(): subprocess.call(["killall global_scheduler plasma_store plasma_manager " "local_scheduler"], shell=True) # Find the PID of the monitor process and kill it. subprocess.call(["kill $(ps aux | grep monitor.py | grep -v grep | " "awk '{ print $2 }') 2> /dev/null"], shell=True) # Find the PID of the Redis process and kill it. subprocess.call(["kill $(ps aux | grep redis-server | grep -v grep | " "awk '{ print $2 }') 2> /dev/null"], shell=True) # Find the PIDs of the worker processes and kill them. subprocess.call(["kill -9 $(ps aux | grep default_worker.py | " "grep -v grep | awk '{ print $2 }') 2> /dev/null"], shell=True) # Find the PID of the Ray log monitor process and kill it. subprocess.call(["kill $(ps aux | grep log_monitor.py | grep -v grep | " "awk '{ print $2 }') 2> /dev/null"], shell=True) # Find the PID of the jupyter process and kill it. try: from notebook.notebookapp import list_running_servers pids = [str(server["pid"]) for server in list_running_servers() if "/tmp/raylogs" in server["notebook_dir"]] subprocess.call(["kill {} 2> /dev/null".format( " ".join(pids))], shell=True) except ImportError: pass
def get_server_info (): servercwd = get_server_cwd () for info in notebookapp.list_running_servers (): if Path (info['notebook_dir']) == servercwd: return info return None
def find_best_server(filename, profile='default'): kwargs = {} if profile != 'default': warnings.warn("Jupyter doesn't have profiles") kwargs['profile'] = profile servers = [si for si in notebookapp.list_running_servers(**kwargs) \ 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(): """ Return the full path of the jupyter notebook. """ kernel_id = re.search('kernel-(.*).json', ipykernel.connect.get_connection_file()).group(1) servers = list_running_servers() for ss in servers: response = requests.get(urljoin(ss['url'], 'api/sessions'), params={'token': ss.get('token', '')}) for nn in json.loads(response.text): if nn['kernel']['id'] == kernel_id: full_path = nn['notebook']['path'] return os.path.basename(full_path) return f
def get_notebook_name(): """ Return the full path of the jupyter notebook. """ kernel_id = re.search( "kernel-(.*).json", ipykernel.connect.get_connection_file() ).group(1) servers = list_running_servers() for ss in servers: response = requests.get( urljoin(ss["url"], "api/sessions"), params={"token": ss.get("token", "")} ) for nn in json.loads(response.text): if nn["kernel"]["id"] == kernel_id: relative_path = nn["notebook"]["path"] return os.path.join(ss["notebook_dir"], relative_path)
def get_env_port(enviroment): servers = list(notebookapp.list_running_servers()) ports = [x['port'] for x in servers] open_servers = os.listdir('/Users/tboudreaux/.jupyter_logs/ports') if enviroment in open_servers: with open( '/Users/tboudreaux/.jupyter_logs/ports/{}'.format(enviroment), 'r') as f: port = f.readline() port = int(port.lstrip().rstrip()) if port in ports: return port else: return -2 else: return -1
def _get_jupyter_notebook_filename(cls): if not sys.argv[0].endswith( os.path.sep + 'ipykernel_launcher.py') or len( sys.argv) < 3 or not sys.argv[2].endswith('.json'): return None # we can safely assume that we can import the notebook package here # noinspection PyBroadException try: from notebook.notebookapp import list_running_servers import requests current_kernel = sys.argv[2].split(os.path.sep)[-1].replace( 'kernel-', '').replace('.json', '') server_info = next(list_running_servers()) r = requests.get(url=server_info['url'] + 'api/sessions', headers={ 'Authorization': 'token {}'.format(server_info.get( 'token', '')), }) r.raise_for_status() notebooks = r.json() cur_notebook = None for n in notebooks: if n['kernel']['id'] == current_kernel: cur_notebook = n break notebook_path = cur_notebook['notebook']['path'] entry_point_filename = notebook_path.split(os.path.sep)[-1] # now we should try to find the actual file entry_point = (Path.cwd() / entry_point_filename).absolute() if not entry_point.is_file(): entry_point = (Path.cwd() / notebook_path).absolute() # install the post store hook, so always have a synced file in the system cls._jupyter_install_post_store_hook(entry_point.as_posix()) # now replace the .ipynb with .py # we assume we will have that file available with the Jupyter notebook plugin entry_point = entry_point.with_suffix('.py') return entry_point.as_posix() except Exception: return None
def invoke(self, args, **kwargs): import os if len(args): raise multitool.UsageError('mynotebook takes no arguments') # See if there's a running server that we can suggest. servercwd = get_server_cwd() for info in notebookapp.list_running_servers(): if Path(info['notebook_dir']) == servercwd: print(info['url']) return # OK, need to start a server info = cli.fork_detached_process() if info.whoami == 'original': url = info.pipe.readline().strip() if not len(url): cli.die('notebook server (PID %d) appears to have crashed', info.forkedpid) print(url.decode('ascii')) else: # We're the child. Set up to run as a background daemon as much as # possible, then indicate to the parent that things look OK. NOTE: # notebook's `argv` should not include what's traditionally called # `argv[0]`. os.chdir(str(servercwd)) app = BgNotebookApp.instance() app.initialize(argv=[]) info.pipe.write(app.display_url.encode('ascii')) info.pipe.write(b'\n') info.pipe.close() with open(os.devnull, 'rb') as devnull: os.dup2(devnull.fileno(), 0) with open(os.devnull, 'wb') as devnull: for fd in 1, 2: os.dup2(devnull.fileno(), fd) # Enter the main loop, never to leave again. app.start()
def invoke (self, args, **kwargs): import os if len (args): raise multitool.UsageError ('mynotebook takes no arguments') # See if there's a running server that we can suggest. servercwd = get_server_cwd () for info in notebookapp.list_running_servers (): if Path (info['notebook_dir']) == servercwd: print (info['url']) return # OK, need to start a server info = cli.fork_detached_process () if info.whoami == 'original': url = info.pipe.readline ().strip () if not len (url): cli.die ('notebook server (PID %d) appears to have crashed', info.forkedpid) print (url.decode ('ascii')) else: # We're the child. Set up to run as a background daemon as much as # possible, then indicate to the parent that things look OK. NOTE: # notebook's `argv` should not include what's traditionally called # `argv[0]`. os.chdir (str (servercwd)) app = BgNotebookApp.instance () app.initialize (argv=[]) info.pipe.write (app.display_url.encode ('ascii')) info.pipe.write (b'\n') info.pipe.close () with open (os.devnull, 'rb') as devnull: os.dup2 (devnull.fileno (), 0) with open (os.devnull, 'wb') as devnull: for fd in 1, 2: os.dup2 (devnull.fileno (), fd) # Enter the main loop, never to leave again. app.start ()
def __init__(self, **kwargs): super().__init__(**kwargs) # Set flag to check for first call to 'do_execute' self.is_first_call = True # Source: https://stackoverflow.com/a/13055551/2926226 # Fetch kernel id connection_file_path = kernel.get_connection_file() connection_file = os.path.basename(connection_file_path) self.kernel_id = connection_file.split('-', 1)[1].split('.')[0] # Fetch and create sessions url for server # Used to fetch the notebook name server = next(notebookapp.list_running_servers( )) # WARNING: Uses the first server found in the generator self.sessions_url = f"{server['url']}api/sessions?token={server['token']}"
def find_best_server(filename, profile="default"): """ find existing running server @param filename notebook @param profile profile to use @return a running server or None if not found """ kwargs = {} if profile != "default": warnings.warn("Jupyter doesn't have profiles") kwargs["profile"] = profile servers = [si for si in notebookapp.list_running_servers(**kwargs) if filename.startswith(si["notebook_dir"])] try: return max(servers, key=lambda si: len(si["notebook_dir"])) except ValueError: return None
def stop(): subprocess.call([ "killall global_scheduler plasma_store plasma_manager " "local_scheduler raylet raylet_monitor" ], shell=True) # Find the PID of the monitor process and kill it. subprocess.call([ "kill $(ps aux | grep monitor.py | grep -v grep | " "awk '{ print $2 }') 2> /dev/null" ], shell=True) # Find the PID of the Redis process and kill it. subprocess.call([ "kill $(ps aux | grep redis-server | grep -v grep | " "awk '{ print $2 }') 2> /dev/null" ], shell=True) # Find the PIDs of the worker processes and kill them. subprocess.call([ "kill -9 $(ps aux | grep default_worker.py | " "grep -v grep | awk '{ print $2 }') 2> /dev/null" ], shell=True) # Find the PID of the Ray log monitor process and kill it. subprocess.call([ "kill $(ps aux | grep log_monitor.py | grep -v grep | " "awk '{ print $2 }') 2> /dev/null" ], shell=True) # Find the PID of the jupyter process and kill it. try: from notebook.notebookapp import list_running_servers pids = [ str(server["pid"]) for server in list_running_servers() if "/tmp/raylogs" in server["notebook_dir"] ] subprocess.call(["kill {} 2> /dev/null".format(" ".join(pids))], shell=True) except ImportError: pass
def get_notebook_path(): """ Return the full path of the jupyter notebook. Reference: https://github.com/jupyter/notebook/issues/1000#issuecomment-359875246 """ kernel_id = re.search('kernel-(.*).json', ipykernel.connect.get_connection_file()).group(1) servers = list_running_servers() for ss in servers: response = requests.get(urljoin(ss['url'], 'api/sessions'), params={'token': ss.get('token', '')}) info = json.loads(response.text) if isinstance(info, dict) and info['message'] == 'Forbidden': continue for nn in info: if nn['kernel']['id'] == kernel_id: relative_path = nn['notebook']['path'] return os.path.join(ss['notebook_dir'], relative_path)
def get_notebook_path() -> Optional[str]: """ Return the full path of the jupyter notebook. From https://github.com/jupyter/notebook/issues/1000#issuecomment-359875246. :returns: the full path to the notebook or None if the path couldn't be found (the latter shouldn't happen) """ kernel_id = re.search("kernel-(.*).json", ipykernel.connect.get_connection_file()).group(1) servers = list_running_servers() for server in servers: response = requests.get(urljoin(server["url"], "api/sessions"), params={"token": server.get("token", "")}) for notebook in json.loads(response.text): if notebook["kernel"]["id"] == kernel_id: relative_path = notebook["notebook"]["path"] return os.path.join(server["notebook_dir"], relative_path)
class NotebookClientCollection: """EXPERIMENTAL SUPPORT: Representation of a collection of notebook clients""" # TODO: refactor from lambda to a def nb_client_gen = lambda: (NotebookClient(x) for x in list_running_servers()) sessions = {x.url: x.sessions for x in nb_client_gen()} @classmethod def current_server(cls): """class method for current notebook server""" current_kernel_id = extract_kernel_id( get_ipython().parent.parent.connection_file) for server_url, session_dict in cls.sessions.items(): for session_id, session in session_dict.items(): if session.kernel.id == current_kernel_id: return next(client for client in cls.nb_client_gen() if client.url == server_url)
def my_jupyter_server(verbose=False, jupyter_parent_pid=None): servers = [] imported_notebookapp = imported_serverapp = False try: from jupyter_server import serverapp servers += serverapp.list_running_servers() imported_serverapp = True except ImportError: pass try: from notebook import notebookapp imported_notebookapp = True servers += notebookapp.list_running_servers() except ImportError: pass if not len(servers): if verbose: import warnings warnings.warn( f"no running jupyter server found - imported jupyter_server: {imported_serverapp} notebook: {imported_notebookapp}" ) return None server_pid = os.getenv('JPY_PARENT_PID', jupyter_parent_pid) if server_pid is None: if len(servers) > 1: pass jpy = servers[0] else: for s in servers: if str(s['pid']) == server_pid: jpy = s break else: # no matching pid found... if verbose: print('no matching jupyter server found!') jpy = servers[0] if jpy is None: return None return dict( url=jpy['url'], params="token=" + jpy['token'], headers={'Authorization': 'token ' + jpy['token']}, )
def get_notebook_path(): """ Return the full path of the jupyter notebook. !!! Doesn't work from VS code. """ kernel_id = re.search('kernel-(.*).json', ipykernel.connect.get_connection_file()).group(1) servers = list_running_servers() for ss in servers: try: response = requests.get(urljoin(ss['url'], 'api/sessions'), params={'token': ss.get('token', '')}, timeout=1) for nn in json.loads(response.text): if nn['kernel']['id'] == kernel_id: relative_path = nn['notebook']['path'] return os.path.join(ss['notebook_dir'], relative_path) except Exception as e: pass
def find_best_server(filename, profile='default'): """ Finds existing running server. @param filename notebook @param profile profile to use @return a running server or None if not found """ from notebook import notebookapp kwargs = {} if profile != 'default': warnings.warn("Jupyter doesn't have profiles") kwargs['profile'] = profile servers = [si for si in notebookapp.list_running_servers(**kwargs) 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() -> str: """Return the full path of the jupyter notebook. References ---------- https://github.com/jupyter/notebook/issues/1000#issuecomment-359875246 """ kernel_id = re.search( # type: ignore 'kernel-(.*).json', ipykernel.connect.get_connection_file()).group(1) servers = list_running_servers() for server in servers: response = requests.get(urljoin(server['url'], 'api/sessions'), params={'token': server.get('token', '')}) for session in json.loads(response.text): if session['kernel']['id'] == kernel_id: relative_path = session['notebook']['path'] return pjoin(server['notebook_dir'], relative_path) raise Exception('Noteboook not found.')
def get_notebook_name() -> str: """ Return the full path and filename of the current jupyter notebook """ try: import ipykernel from notebook.notebookapp import list_running_servers except ImportError as e: #log.exception('ImportError : This only runs in a Jupyter Notebook environment ' + str(e)) return #raise Exception() from e import re import requests from urllib.parse import urljoin # Can also get the token this way #from traitlets import HasTraits, Unicode, getmembers #from jupyterhub.services.auth import HubAuth as auth #token = auth.api_token #print(token.default_value) # This is the same as the os.env JUPYTERHUB_API_TOKEN try: kernel_id = re.search('kernel-(.*).json', ipykernel.connect.get_connection_file()).group(1) servers = list_running_servers() except RuntimeError as e: #log.exception('RuntimeError : This only runs in a Jupyter Notebook environment ' + str(e)) return token = os.getenv('JUPYTERHUB_API_TOKEN') for ss in servers: response = requests.get(urljoin(ss['url'], 'api/sessions'), params={'token': token}) response.raise_for_status() for nn in json.loads(response.text): if nn['kernel']['id'] == kernel_id: relative_path = nn['notebook']['path'] return os.path.join(ss['notebook_dir'], relative_path)
def notebook_metadata(silent): """Attempts to query jupyter for the path and name of the notebook file""" error_message = ( "Failed to query for notebook name, you can set it manually with " "the WANDB_NOTEBOOK_NAME environment variable") try: import ipykernel from notebook.notebookapp import list_running_servers kernel_id = re.search("kernel-(.*).json", ipykernel.connect.get_connection_file()).group(1) servers = list(list_running_servers( )) # TODO: sometimes there are invalid JSON files and this blows up except Exception: # TODO: Fix issue this is not the logger initialized in in wandb.init() # since logger is not attached, outputs to notebook if not silent: logger.error(error_message) return {} for s in servers: try: if s["password"]: raise ValueError("Can't query password protected kernel") res = requests.get(urljoin(s["url"], "api/sessions"), params={ "token": s.get("token", "") }).json() except (requests.RequestException, ValueError): if not silent: logger.error(error_message) return {} for nn in res: # TODO: wandb/client#400 found a case where res returned an array of # strings... if isinstance(nn, dict) and nn.get("kernel") and "notebook" in nn: if nn["kernel"]["id"] == kernel_id: return { "root": s["notebook_dir"], "path": nn["notebook"]["path"], "name": nn["notebook"]["name"], } return {}
def _get_filename(f=None): """ Return the full path of the jupyter notebook. """ # when running not in and interactive shell, # just get the filename of the main script #raise ValueError('filename: {}'.format(f)) # default filename (injected) try: os.stat(f) return os.path.abspath(f) except: pass # fallback 1: main file name (python files) try: import __main__ as main return os.path.abspath(os.path.basename(main.__file__)) except: pass #fallback 2: interactive shell try: kernel_filename = ipykernel.connect.get_connection_file() kernel_id = re.search('kernel-(.*).json', kernel_filename).group(1) for s in list_running_servers(): url = urljoin(s['url'], 'api/sessions') params = {'token': s.get('token', '')} response = requests.get(url, params) for nn in json.loads(response.text): if nn['kernel']['id'] == kernel_id: relative_path = nn['notebook']['path'] f = os.path.join(s['notebook_dir'], relative_path) return os.path.abspath(f) except: pass #nothing found. Stop raise ValueError('could not infer the filename: {}'.format(f))
def notebook_path(): connection_file = os.path.basename(ipykernel.get_connection_file()) kernel_id = connection_file.split('-', 1)[1].split('.')[0] for srv in notebookapp.list_running_servers(): try: if srv['token'] == '' and not srv[ '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) for sess in sessions: if sess['kernel']['id'] == kernel_id: return os.path.join(srv['notebook_dir'], sess['notebook']['path']) except: pass return None
def get_notebook_name(): """ Get name of current Colab notebook. """ import re import ipykernel import requests from requests.compat import urljoin from notebook.notebookapp import list_running_servers kernel_id = re.search('kernel-(.*).json', ipykernel.connect.get_connection_file()).group(1) servers = list_running_servers() for ss in servers: response = requests.get(urljoin(ss['url'], 'api/sessions'), params={'token': ss.get('token', '')}) for nn in response.json(): if nn['kernel']['id'] == kernel_id: notebook_name = nn['notebook']['name'] return notebook_name
def notebook_path(): """Returns the absolute path of the Notebook or None if it cannot be determined NOTE: works only 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] for srv in notebookapp.list_running_servers(): try: if srv['token']=='' and not srv['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) for sess in sessions: if sess['kernel']['id'] == kernel_id: return os.path.join(srv['notebook_dir'],sess['notebook']['path']) except: pass # There may be stale entries in the runtime directory return None
def get_notebook_name(): """ Return the full path of the jupyter notebook. """ try: # Python 3 from notebook.notebookapp import list_running_servers except ImportError: # Python 2 try: import warnings from IPython.utils.shimmodule import ShimWarning with warnings.catch_warnings(): warnings.simplefilter("ignore", category=ShimWarning) from IPython.html.notebookapp import list_running_servers except ImportError: # Probably pyspark script is run without IPython/Jupyter if not explicit_process_name: print( 'WARN Unable to automatically extract Jupyter/pyspark notebook name (did you run it without jupyter?)' ) return [ '', explicit_process_name or 'Unknown pyspark filename' ] try: import ipykernel kernel_id = re.search( 'kernel-(.*).json', ipykernel.connect.get_connection_file()).group(1) servers = list_running_servers() for ss in servers: response = requests.get(urljoin(ss['url'], 'api/sessions'), params={'token': ss.get('token', '')}) for nn in json.loads(response.text): if nn['kernel']['id'] == kernel_id: server = ss notebooks_path = server['notebook_dir'] return [notebooks_path, nn['notebook']['path']] except Exception as e: if not explicit_process_name: print( 'WARN Unable to automatically extract pyspark notebook name' ) return ['', explicit_process_name or 'Unknown pyspark filename']
def _err_on_running(skip_running_check=False, runtime_dir=None): if skip_running_check: return try: srv = next(list_running_servers(runtime_dir=runtime_dir)) except StopIteration: return raise NotebookRunningError(""" Will not configure while a Jupyter notebook server appears to be running. At least this server appears to be running: {} Note that the json file indicating that this server is running may be stale, see https://github.com/jupyter/notebook/issues/2829 for further details. """.format(srv))
def get_current_notebook_filename(): """Return the file containing the current notebook. Returns: notebook_path, relative to start dir of jupyter, example 'deliver/180501-NN-name.ipynb Notes: - 180510: Only tested on jupyterlab - Don't use this to copy the notebook file, unless you figure out a way to make sure it's saved first """ for s0 in list_running_servers(): r = requests.get( url=s0['url'] + 'api/sessions', headers={'Authorization': 'token {}'.format(s0['token']),}) r.raise_for_status() response = r.json() kernel_id = re.search('kernel-(.*).json', ipykernel.connect.get_connection_file()).group(1) notebook_paths = {r['kernel']['id']: r['notebook']['path'] for r in response} if kernel_id in notebook_paths: return notebook_paths[kernel_id] else: continue # not found for this server
def get_notebook_name(): """ Return the full path of the jupyter notebook. """ try: kernel_id = re.search('kernel-(.*).json', ipykernel.connect.get_connection_file()).group(1) servers = list_running_servers() for ss in servers: response = requests.get(urljoin(ss['url'], 'api/sessions'), params={'token': ss.get('token', '')}) for nn in json.loads(response.text): if nn['kernel']['id'] == kernel_id: relative_path = nn['notebook']['path'] return os.path.basename(relative_path) #return os.path.join(ss['notebook_dir'], relative_path) warnings.warn( 'Unexpected Error: no kernel corresponds to this call was found.') return None except: return None
def get_notebook_server_path(): try: # Python 3 from notebook.notebookapp import list_running_servers except ImportError: # Python 2 import warnings from IPython.utils.shimmodule import ShimWarning with warnings.catch_warnings(): warnings.simplefilter("ignore", category=ShimWarning) from IPython.html.notebookapp import list_running_servers """Get the path of the notebook relative to the Jupyter server""" import ipykernel kernel_id = re.search('kernel-(.*).json', ipykernel.connect.get_connection_file()).group(1) servers = list_running_servers() for ss in servers: response = requests.get(urljoin(ss['url'], 'api/sessions'), params={'token': ss.get('token', '')}) for nn in json.loads(response.text): if nn['kernel']['id'] == kernel_id: relative_path = nn['notebook']['path'] return relative_path
def get_notebook_name() -> str: """Return the full path of the jupyter notebook. References ---------- https://github.com/jupyter/notebook/issues/1000#issuecomment-359875246 """ kernel_id = re.search( # type: ignore 'kernel-(.*).json', ipykernel.connect.get_connection_file() ).group(1) servers = list_running_servers() for server in servers: response = requests.get(urljoin(server['url'], 'api/sessions'), params={'token': server.get('token', '')}) for session in json.loads(response.text): if session['kernel']['id'] == kernel_id: relative_path = session['notebook']['path'] return pjoin(server['notebook_dir'], relative_path) raise Exception('Noteboook not found.')
def test_zip(): run_and_log("jupyter serverextension enable --py nbzip") run_and_log("jupyter nbextension enable --py nbzip") source_directory = 'testenv' create_test_files(source_directory) os.system("jupyter-notebook --port={} --no-browser &".format(PORT)) if (not wait_for_notebook_to_start()): assert False, "Notebook server failed to start" server = next(list_running_servers()) token = server['token'] try: for fmt in ('zip', 'tar.gz'): for path in ('Home', 'dir1/', 'dir1/dir2', 'dir1/dir3', 'dir4'): check_zipped_file_contents(source_directory, path, token, fmt) finally: logging.info("Shutting down notebook server...") os.system("jupyter notebook stop {}".format(PORT))
def form_url(): # form the url to access jupyter try: my_ip = get_ip() except: try: my_ip = get_local_ip() except: return "You did not have internet. If you still get this telegram message that means something is weird with your ip configuration" servers = list(notebookapp.list_running_servers()) if len(servers) > 0: urls = [] for idx, server, in enumerate(servers): urltosend = "".join([ "http://", my_ip, ":", str(server["port"]), "/?token=", str(server["token"]) ]) urls.append(urltosend) else: return "Something went wrong." return "Your jupyter instances are at " + "\n ".join(urls)
def create_notebook(): ''' Returns a new IQ# notebook in a Firefox web driver ''' driver = None max_retries = 5 for _ in range(max_retries): try: driver = Firefox() break except: print( f"Exception creating Firefox driver, retrying. Exception info: {sys.exc_info()}" ) if not driver: raise Exception( f"Failed to create Firefox driver in {max_retries} tries") server = list(list_running_servers())[0] driver.get('{url}?token={token}'.format(**server)) return Notebook.new_notebook(driver, kernel_name='kernel-iqsharp')
def _get_notebook_name(verbose=False) -> Optional[str]: """ Return the full path of the jupyter notebook. See https://github.com/jupyter/notebook/issues/1000 In some situations (e.g. running on the command line via nbconvert), the notebook name is not available. We return None in those cases. """ # kernel_id = re.search('kernel-(.*).json', # ipykernel.connect.get_connection_file()).group(1) try: ipy = get_ipython() info = ipy.ev("DWS_JUPYTER_INFO") return info.notebook_path except Exception as e: if verbose: print("DWS Jupyter extension was not loaded: %s" % e) try: connection_file = ipykernel.connect.get_connection_file() mo = re.search('kernel-(.*).json', connection_file) if mo is not None: kernel_id = mo.group(1) servers = list_running_servers() for ss in servers: response = requests.get(urljoin(ss['url'], 'api/sessions'), params={'token': ss.get('token', '')}) for nn in json.loads(response.text): if nn['kernel']['id'] == kernel_id: relative_path = nn['notebook']['path'] return join(ss['notebook_dir'], relative_path) print("Did not find a matching notebook server for %s" % connection_file) return None except Exception as e: if verbose: print("Unable to use notebook API to access session info: %s" % e) # all our atempts failed return None
def get_notebook_name(): """ Return the full path of the jupyter notebook. """ # taken from https://github.com/jupyter/notebook/issues/1000#issuecomment-359875246 from requests.compat import urljoin import ipykernel import requests import json import re from notebook.notebookapp import list_running_servers kernel_id = re.search('kernel-(.*).json', ipykernel.connect.get_connection_file()).group(1) servers = list_running_servers() for ss in servers: response = requests.get(urljoin(ss['url'], 'api/sessions'), params={'token': ss.get('token', '')}) for nn in json.loads(response.text): if nn['kernel']['id'] == kernel_id: relative_path = nn['notebook']['path'] return os.path.join(ss['notebook_dir'], relative_path)
def get_notebook_filepath(): """ Returns the filesystem path of the Jupyter notebook running the Client. This implementation is from https://github.com/jupyter/notebook/issues/1000#issuecomment-359875246. Returns ------- str Raises ------ OSError If one of the following is true: - Jupyter is not installed - Client is not being called from a notebook - the calling notebook cannot be identified """ try: connection_file = ipykernel.connect.get_connection_file() except ( NameError, # Jupyter not installed RuntimeError): # not in a Notebook pass else: kernel_id = re.search('kernel-(.*).json', connection_file).group(1) for server in list_running_servers(): response = requests.get(urljoin(server['url'], 'api/sessions'), params={'token': server.get('token', '')}) if response.ok: for session in response.json(): if session['kernel']['id'] == kernel_id: relative_path = session['notebook']['path'] return os.path.join(server['notebook_dir'], relative_path) raise OSError("unable to find notebook file")
def case_link(ucids, ext='html' , incl_token=False, server_index=-1): ''' Generate clickable case links for a set of ucids, from within a jupyter notebook Inputs: - ucids (iterable or str): iterable of ucids to create links for (if you supply a single ucid it will case to a list) - ext ('html' or 'json') - incl_token (bool): whether to include the token query string (e.g. if you're switching between browsers) - server index (int): if you have multiple jupyter servers running, specify which one, defaults to last ''' from notebook import notebookapp from IPython.core.display import display, HTML # Get server details server = list(notebookapp.list_running_servers())[server_index] notebook_dir = Path(server['notebook_dir']) token = server['token'] if incl_token else None # Coerce to iterable if a single ucid supplied if type(ucids) is str: ucids = (ucids,) # Iterate over all ucids for ucid in ucids: path = ftools.get_expected_path(ucid, ext=ext) if not path.exists(): print(f'{ucid}: No such file exists at {path}') continue rel_path = path.relative_to(notebook_dir) base_url = f"http://{server['hostname']}:{server['port']}/view/" full_url = base_url + str(rel_path) if incl_token: full_url += f'?token={token}' link = f'{ucid}: <a target="_blank" href="{full_url}">{full_url}</a>' display(HTML(link))
def quick_driver(lab=False): """Quickly create a selenium driver pointing at an active noteboook server. Usage example: from notebook.tests.selenium.quick_selenium import quick_driver driver = quick_driver Note: you need to manually close the driver that opens with driver.quit() """ try: server = list(list_running_servers())[0] except IndexError as e: raise NoServerError('You need a server running before you can run ' 'this command') driver = Firefox() auth_url = '{url}?token={token}'.format(**server) driver.get(auth_url) # If this redirects us to a lab page and we don't want that; # then we need to redirect ourselves to the classic notebook view if driver.current_url.endswith('/lab') and not lab: driver.get(driver.current_url.rstrip('lab')+'tree') return driver
def get_servers(): return list(notebookapp.list_running_servers(nbapp.runtime_dir))
# Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. from notebook.notebookapp import list_running_servers import json server_list = list_running_servers() server_info_list = [] for si in server_list: server_info_object = {} server_info_object["base_url"] = si['base_url'] server_info_object["notebook_dir"] = si['notebook_dir'] server_info_object["hostname"] = si['hostname'] server_info_object["password"] = si['password'] server_info_object["pid"] = si['pid'] server_info_object["port"] = si['port'] server_info_object["secure"] = si['secure'] server_info_object["token"] = si['token'] server_info_object["url"] = si['url'] server_info_list.append(server_info_object) print(json.dumps(server_info_list))
def test_list_running_servers(self): servers = list(notebookapp.list_running_servers()) assert len(servers) >= 1 assert self.port in {info['port'] for info in servers}
def notebook_is_running(runtime_dir=None): """Return true if a notebook process appears to be running.""" try: return bool(next(list_running_servers(runtime_dir=runtime_dir))) except StopIteration: return False
print("---") if __name__ == '__main__': import sys from notebook.notebookapp import list_running_servers show_files = False if len(sys.argv) > 1: if sys.argv[1] == '-f': show_files = True else: print("Usage: %s -f" % sys.argv[0]) # get running servers servers = list_running_servers() print("#"*10) print("Servers:") files = get_notebook_name(servers) # get PIDS to kernels pids = get_pid_kernels() print("#"*10) print("Kernels + Notebooks:") print("%36s %7s %10s %s" % ("ID","PID","PY","Notebook")) for i,j in files.items(): print("%36s %7s %10s %s" % ((i, pids[i],) + j)) if show_files: get_openfiles(pids[i]) print("#"*10)