Skip to content

Commit 4afe680

Browse files
committed
Lay foundation to pass notebook names to kernel at startup.
This has been a controversial topic from some time: jupyter/notebook#1000 https://forums.databricks.com/questions/21390/is-there-any-way-to-get-the-current-notebook-name.html https://stackoverflow.com/questions/12544056/how-do-i-get-the-current-ipython-jupyter-notebook-name https://ask.sagemath.org/question/36873/access-notebook-filename-from-jupyter-with-sagemath-kernel/ This is also sometime critical to linter, and tab completion to know current name. Of course current answer is that the question is ill-defined, there might not be a file associated with the current kernel, there might be multiple files, files might not be on the same system, it could change through the execution and many other gotchas. This suggest to add an JPY_KERNEL_SESSION_NAME env variable which is not too visible, but give an escape hatch which should mostly be correct unless the notebook is renamed or kernel attached to a new one. Do do so this handles the new associated_file parameters in a few function of the kernel manager. On jupyter_server this one line change make the notebook name available using typical local installs: --- a/jupyter_server/services/sessions/sessionmanager.py +++ b/jupyter_server/services/sessions/sessionmanager.py @@ -96,7 +96,12 @@ class SessionManager(LoggingConfigurable): """Start a new kernel for a given session.""" # allow contents manager to specify kernels cwd kernel_path = self.contents_manager.get_kernel_path(path=path) - kernel_id = await self.kernel_manager.start_kernel(path=kernel_path, kernel_name=kernel_name) + + kernel_id = await self.kernel_manager.start_kernel( + path=kernel_path, kernel_name=kernel_name, session_name=name + ) return kernel_id Of course only launchers that will pass forward this value will allow the env variable to be set. I'm thinking that various kernels may use this and expose it in different ways. like __notebook_name__ if it ends with `.ipynb` in ipykernel. Commit ammended – originally the name was associated_file, and JPY_ASSOCIATED_FILE, but was changed.
1 parent 98faaf9 commit 4afe680

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

jupyter_client/manager.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@
3535
from jupyter_client import launch_kernel
3636

3737

38+
# Name of the env variable that contains the name of the current session associated
39+
# with the kernel we are launching.
40+
# Frontends can decide to set this session name to the name of the file when
41+
# when the kernel is started.
42+
# This is useful in notebook context to find which notebook we are working with
43+
# though we might not be working with a notebook, we could be working with a
44+
# markdown file, or python file.
45+
# as with other Jupyter Related Env variable with use the JPY prefix.
46+
JPY_KERNEL_SESSION_NAME = 'JPY_KERNEL_SESSION_NAME'
47+
48+
3849
class _ShutdownStatus(Enum):
3950
"""
4051

@@ -298,6 +309,9 @@ def pre_start_kernel(self, **kw) -> t.Tuple[t.List[str], t.Dict[str, t.Any]]:
298309
# Don't allow PYTHONEXECUTABLE to be passed to kernel process.
299310
# If set, it can bork all the things.
300311
env.pop("PYTHONEXECUTABLE", None)
312+
session_name = kw.pop('session_name', None)
313+
if (env.get(JPY_KERNEL_SESSION_NAME, None) is None) and (session_name is not None):
314+
env[JPY_KERNEL_SESSION_NAME] = session_name
301315

302316
# Environment variables from kernel spec are added to os.environ.
303317
assert self.kernel_spec is not None

jupyter_client/multikernelmanager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ def pre_start_kernel(
180180
constructor_kwargs = {}
181181
if self.kernel_spec_manager:
182182
constructor_kwargs["kernel_spec_manager"] = self.kernel_spec_manager
183+
184+
if 'session_name' in kwargs:
185+
constructor_kwargs['session_name'] = kwargs.pop('session_name')
183186
km = self.kernel_manager_factory(
184187
connection_file=os.path.join(self.connection_dir, "kernel-%s.json" % kernel_id),
185188
parent=self,

0 commit comments

Comments
 (0)