Fix support for multiple jupyter servers running on the same machine

Fix issue with old/new notebook packages installed
This commit is contained in:
allegroai 2022-10-14 10:19:35 +03:00
parent 8364cd23d9
commit 76939339c4

View File

@ -315,7 +315,8 @@ class _JupyterObserver(object):
_script_exporter = ScriptExporter() _script_exporter = ScriptExporter()
except Exception as ex: except Exception as ex:
cls._get_logger().warning('Could not read Jupyter Notebook: {}'.format(ex)) cls._get_logger().warning('Could not read Jupyter Notebook: {}'.format(ex))
return _script_exporter = None
# load pigar # load pigar
# noinspection PyBroadException # noinspection PyBroadException
try: try:
@ -410,6 +411,11 @@ class _JupyterObserver(object):
except Exception: except Exception:
continue continue
if _script_exporter is None:
current_script_hash = 'error_notebook_not_found.py'
requirements_txt = ''
conda_requirements = ''
else:
# get notebook python script # get notebook python script
if script_code is None and local_jupyter_filename: if script_code is None and local_jupyter_filename:
script_code, _ = _script_exporter.from_filename(local_jupyter_filename) script_code, _ = _script_exporter.from_filename(local_jupyter_filename)
@ -552,41 +558,46 @@ class ScriptInfo(object):
or len(sys.argv) < 3 or not sys.argv[2].endswith('.json'): or len(sys.argv) < 3 or not sys.argv[2].endswith('.json'):
return None return None
server_info = None
# we can safely assume that we can import the notebook package here # we can safely assume that we can import the notebook package here
# noinspection PyBroadException # noinspection PyBroadException
try: try:
jupyter_servers = []
# noinspection PyBroadException # noinspection PyBroadException
try: try:
# noinspection PyPackageRequirements # noinspection PyPackageRequirements
from notebook.notebookapp import list_running_servers # <= Notebook v6 from notebook.notebookapp import list_running_servers # <= Notebook v6
# noinspection PyBroadException
try:
jupyter_servers += list(list_running_servers())
except Exception: except Exception:
server_info = cls.__legacy_jupyter_notebook_server_json_parsing()
if server_info:
jupyter_servers += [server_info]
except Exception:
pass
# noinspection PyBroadException
try:
# noinspection PyPackageRequirements # noinspection PyPackageRequirements
from jupyter_server.serverapp import list_running_servers from jupyter_server.serverapp import list_running_servers
# noinspection PyBroadException
try:
jupyter_servers += list(list_running_servers())
except Exception:
server_info = cls.__legacy_jupyter_notebook_server_json_parsing()
if server_info:
jupyter_servers += [server_info]
except Exception:
pass
import requests import requests
current_kernel = sys.argv[2].split(os.path.sep)[-1].replace('kernel-', '').replace('.json', '') current_kernel = sys.argv[2].split(os.path.sep)[-1].replace('kernel-', '').replace('.json', '')
# noinspection PyBroadException notebook_path = None
try: notebook_name = None
server_info = next(list_running_servers())
except Exception: for server_index, server_info in enumerate(jupyter_servers):
# on some jupyter notebook versions this function can crash on parsing the json file,
# we will parse it manually here
# noinspection PyPackageRequirements
import ipykernel
from glob import glob
import json
for f in glob(os.path.join(os.path.dirname(ipykernel.get_connection_file()), '??server-*.json')):
# noinspection PyBroadException
try:
with open(f, 'r') as json_data:
server_info = json.load(json_data)
except Exception:
server_info = None
if server_info:
break
cookies = None cookies = None
password = None password = None
@ -628,20 +639,27 @@ class ScriptInfo(object):
try: try:
r.raise_for_status() r.raise_for_status()
except Exception as ex: except Exception as ex:
# raise on last one only
if server_index == len(jupyter_servers)-1:
cls._get_logger().warning('Failed accessing the jupyter server{}: {}'.format( cls._get_logger().warning('Failed accessing the jupyter server{}: {}'.format(
' [password={}]'.format(password) if server_info.get('password') else '', ex)) ' [password={}]'.format(password) if server_info.get('password') else '', ex))
return os.path.join(os.getcwd(), 'error_notebook_not_found.py') return os.path.join(os.getcwd(), 'error_notebook_not_found.py')
notebooks = r.json() notebooks = r.json()
cur_notebook = None cur_notebook = None
for n in notebooks: for n in notebooks:
if n['kernel']['id'] == current_kernel: if n['kernel']['id'] == current_kernel:
cur_notebook = n cur_notebook = n
break break
# notebook not found
if not cur_notebook:
continue
notebook_path = cur_notebook['notebook'].get('path', '') notebook_path = cur_notebook['notebook'].get('path', '')
notebook_name = cur_notebook['notebook'].get('name', '') notebook_name = cur_notebook['notebook'].get('name', '')
if notebook_path:
break
is_google_colab = False is_google_colab = False
# check if this is google.colab, then there is no local file # check if this is google.colab, then there is no local file
@ -660,7 +678,7 @@ class ScriptInfo(object):
if not script_entry_point.lower().endswith('.py'): if not script_entry_point.lower().endswith('.py'):
script_entry_point += '.py' script_entry_point += '.py'
local_ipynb_file = None local_ipynb_file = None
else: elif notebook_path is not None:
# always slash, because this is from uri (so never backslash not even on windows) # always slash, because this is from uri (so never backslash not even on windows)
entry_point_filename = notebook_path.split('/')[-1] entry_point_filename = notebook_path.split('/')[-1]
@ -693,6 +711,10 @@ class ScriptInfo(object):
entry_point = entry_point.with_suffix('.py') entry_point = entry_point.with_suffix('.py')
script_entry_point = entry_point.as_posix() script_entry_point = entry_point.as_posix()
else:
# we could not find and access any jupyter server
cls._get_logger().warning('Failed accessing the jupyter server(s): {}'.format(jupyter_servers))
return None # 'error_notebook_not_found.py'
# install the post store hook, # install the post store hook,
# notice that if we do not have a local file we serialize/write every time the entire notebook # notice that if we do not have a local file we serialize/write every time the entire notebook
@ -891,6 +913,29 @@ class ScriptInfo(object):
return (ScriptInfoResult(script=script_info, warning_messages=messages, auxiliary_git_diff=auxiliary_git_diff), return (ScriptInfoResult(script=script_info, warning_messages=messages, auxiliary_git_diff=auxiliary_git_diff),
script_requirements) script_requirements)
@staticmethod
def __legacy_jupyter_notebook_server_json_parsing(self):
# noinspection PyBroadException
try:
# on some jupyter notebook versions this function can crash on parsing the json file,
# we will parse it manually here
# noinspection PyPackageRequirements
import ipykernel
from glob import glob
import json
for f in glob(os.path.join(os.path.dirname(ipykernel.get_connection_file()), '??server-*.json')):
# noinspection PyBroadException
try:
with open(f, 'r') as json_data:
server_info = json.load(json_data)
except Exception:
continue
if server_info:
return server_info
except Exception:
pass
return None
@classmethod @classmethod
def get(cls, filepaths=None, check_uncommitted=True, create_requirements=True, log=None, def get(cls, filepaths=None, check_uncommitted=True, create_requirements=True, log=None,
uncommitted_from_remote=False, detect_jupyter_notebook=True, add_missing_installed_packages=False, uncommitted_from_remote=False, detect_jupyter_notebook=True, add_missing_installed_packages=False,