117 lines
4 KiB
Diff
117 lines
4 KiB
Diff
diff --git a/exputils/gui/jupyter/__init__.py b/exputils/gui/jupyter/__init__.py
|
|
index 6e9aefb..fdfdd28 100644
|
|
--- a/exputils/gui/jupyter/__init__.py
|
|
+++ b/exputils/gui/jupyter/__init__.py
|
|
@@ -30,8 +30,8 @@ from exputils.gui.jupyter.misc import remove_children_from_widget
|
|
from exputils.gui.jupyter.misc import set_children_of_widget
|
|
from exputils.gui.jupyter.misc import generate_random_state_backup_name
|
|
|
|
-from exputils.gui.jupyter.ipynbname import get_notebook_name
|
|
-from exputils.gui.jupyter.ipynbname import get_notebook_path
|
|
+from ipynbname import name as get_notebook_name
|
|
+from ipynbname import path as get_notebook_path
|
|
|
|
DEFAULT_CONFIG_DIRECTORY = '.ipython_config'
|
|
|
|
diff --git a/exputils/gui/jupyter/ipynbname.py b/exputils/gui/jupyter/ipynbname.py
|
|
deleted file mode 100644
|
|
index 51e21b7..0000000
|
|
--- a/exputils/gui/jupyter/ipynbname.py
|
|
+++ /dev/null
|
|
@@ -1,86 +0,0 @@
|
|
-##
|
|
-## This file is part of the exputils package.
|
|
-##
|
|
-## Copyright: INRIA
|
|
-## Year: 2022, 2023
|
|
-## Contact: chris.reinke@inria.fr
|
|
-##
|
|
-## exputils is provided under GPL-3.0-or-later
|
|
-##
|
|
-# Taken from https://pypi.org/project/ipynbname/
|
|
-# TODO: add them to licence
|
|
-
|
|
-from notebook import notebookapp
|
|
-import urllib, json, os, ipykernel, ntpath
|
|
-
|
|
-FILE_ERROR = "Can't identify the notebook {}."
|
|
-CONN_ERROR = "Unable to access server;\n \
|
|
- + ipynbname requires either no security or token based security."
|
|
-
|
|
-def _get_kernel_id():
|
|
- """ Returns the kernel ID of the ipykernel.
|
|
- """
|
|
- connection_file = os.path.basename(ipykernel.get_connection_file())
|
|
- kernel_id = connection_file.split('-', 1)[1].split('.')[0]
|
|
- return kernel_id
|
|
-
|
|
-
|
|
-def _get_sessions(srv):
|
|
- """ Given a server, returns sessions, or HTTPError if access is denied.
|
|
- NOTE: Works only when either there is no security or there is token
|
|
- based security. An HTTPError is raised if unable to connect to a
|
|
- server.
|
|
- """
|
|
- try:
|
|
- qry_str = ""
|
|
- token = srv['token']
|
|
- if token:
|
|
- qry_str = f"?token={token}"
|
|
- url = f"{srv['url']}api/sessions{qry_str}"
|
|
- req = urllib.request.urlopen(url)
|
|
- return json.load(req)
|
|
- except:
|
|
- raise urllib.error.HTTPError(CONN_ERROR)
|
|
-
|
|
-
|
|
-def _get_nb_path(sess, kernel_id):
|
|
- """ Given a session and kernel ID, returns the notebook path for the
|
|
- session, or None if there is no notebook for the session.
|
|
- """
|
|
- if sess['kernel']['id'] == kernel_id:
|
|
- return sess['notebook']['path']
|
|
- return None
|
|
-
|
|
-
|
|
-def get_notebook_name():
|
|
- """ Returns the short name of the notebook w/o the .ipynb extension,
|
|
- or raises a FileNotFoundError exception if it cannot be determined.
|
|
- """
|
|
- kernel_id = _get_kernel_id()
|
|
- for srv in notebookapp.list_running_servers():
|
|
- try:
|
|
- sessions = _get_sessions(srv)
|
|
- for sess in sessions:
|
|
- nb_path = _get_nb_path(sess, kernel_id)
|
|
- if nb_path:
|
|
- return ntpath.basename(nb_path).replace('.ipynb', '')
|
|
- except:
|
|
- pass # There may be stale entries in the runtime directory
|
|
- raise FileNotFoundError(FILE_ERROR.format('name'))
|
|
-
|
|
-
|
|
-def get_notebook_path():
|
|
- """ Returns the absolute path of the notebook,
|
|
- or raises a FileNotFoundError exception if it cannot be determined.
|
|
- """
|
|
- kernel_id = _get_kernel_id()
|
|
- for srv in notebookapp.list_running_servers():
|
|
- try:
|
|
- sessions = _get_sessions(srv)
|
|
- for sess in sessions:
|
|
- nb_path = _get_nb_path(sess, kernel_id)
|
|
- if nb_path:
|
|
- return os.path.join(srv['notebook_dir'], nb_path)
|
|
- except:
|
|
- pass # There may be stale entries in the runtime directory
|
|
- raise FileNotFoundError(FILE_ERROR.format('path'))
|
|
\ No newline at end of file
|
|
diff --git a/setup.cfg b/setup.cfg
|
|
index 9d9cbb0..6080ed6 100644
|
|
--- a/setup.cfg
|
|
+++ b/setup.cfg
|
|
@@ -25,3 +25,4 @@ install_requires =
|
|
tensorboard >= 1.15.0
|
|
fasteners >= 0.18
|
|
pyyaml >= 6.0
|
|
+ ipynbname
|