mirror of
https://github.com/clearml/clearml
synced 2025-06-26 18:16:07 +00:00
Fix support for multiple jupyter servers running on the same machine
Fix issue with old/new notebook packages installed
This commit is contained in:
parent
8364cd23d9
commit
76939339c4
@ -315,7 +315,8 @@ class _JupyterObserver(object):
|
||||
_script_exporter = ScriptExporter()
|
||||
except Exception as ex:
|
||||
cls._get_logger().warning('Could not read Jupyter Notebook: {}'.format(ex))
|
||||
return
|
||||
_script_exporter = None
|
||||
|
||||
# load pigar
|
||||
# noinspection PyBroadException
|
||||
try:
|
||||
@ -410,6 +411,11 @@ class _JupyterObserver(object):
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
if _script_exporter is None:
|
||||
current_script_hash = 'error_notebook_not_found.py'
|
||||
requirements_txt = ''
|
||||
conda_requirements = ''
|
||||
else:
|
||||
# get notebook python script
|
||||
if script_code is None and 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'):
|
||||
return None
|
||||
|
||||
server_info = None
|
||||
|
||||
# we can safely assume that we can import the notebook package here
|
||||
# noinspection PyBroadException
|
||||
try:
|
||||
jupyter_servers = []
|
||||
# noinspection PyBroadException
|
||||
try:
|
||||
# noinspection PyPackageRequirements
|
||||
from notebook.notebookapp import list_running_servers # <= Notebook v6
|
||||
# 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
|
||||
|
||||
# noinspection PyBroadException
|
||||
try:
|
||||
# noinspection PyPackageRequirements
|
||||
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
|
||||
current_kernel = sys.argv[2].split(os.path.sep)[-1].replace('kernel-', '').replace('.json', '')
|
||||
|
||||
# noinspection PyBroadException
|
||||
try:
|
||||
server_info = next(list_running_servers())
|
||||
except Exception:
|
||||
# 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
|
||||
notebook_path = None
|
||||
notebook_name = None
|
||||
|
||||
for server_index, server_info in enumerate(jupyter_servers):
|
||||
|
||||
cookies = None
|
||||
password = None
|
||||
@ -628,20 +639,27 @@ class ScriptInfo(object):
|
||||
try:
|
||||
r.raise_for_status()
|
||||
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(
|
||||
' [password={}]'.format(password) if server_info.get('password') else '', ex))
|
||||
return os.path.join(os.getcwd(), 'error_notebook_not_found.py')
|
||||
|
||||
notebooks = r.json()
|
||||
|
||||
cur_notebook = None
|
||||
for n in notebooks:
|
||||
if n['kernel']['id'] == current_kernel:
|
||||
cur_notebook = n
|
||||
break
|
||||
|
||||
# notebook not found
|
||||
if not cur_notebook:
|
||||
continue
|
||||
|
||||
notebook_path = cur_notebook['notebook'].get('path', '')
|
||||
notebook_name = cur_notebook['notebook'].get('name', '')
|
||||
if notebook_path:
|
||||
break
|
||||
|
||||
is_google_colab = False
|
||||
# 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'):
|
||||
script_entry_point += '.py'
|
||||
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)
|
||||
entry_point_filename = notebook_path.split('/')[-1]
|
||||
|
||||
@ -693,6 +711,10 @@ class ScriptInfo(object):
|
||||
entry_point = entry_point.with_suffix('.py')
|
||||
|
||||
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,
|
||||
# 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),
|
||||
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
|
||||
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,
|
||||
|
Loading…
Reference in New Issue
Block a user