diff --git a/jupyter_client/manager.py b/jupyter_client/manager.py index 0c489d3d4..391242925 100644 --- a/jupyter_client/manager.py +++ b/jupyter_client/manager.py @@ -34,6 +34,17 @@ from jupyter_client import kernelspec +# Name of the env variable that contains the name of the current session associated +# with the kernel we are launching. +# Frontends can decide to set this session name to the name of the file when +# when the kernel is started. +# This is useful in notebook context to find which notebook we are working with +# though we might not be working with a notebook, we could be working with a +# markdown file, or python file. +# as with other Jupyter Related Env variable with use the JPY prefix. +JPY_KERNEL_SESSION_NAME = 'JPY_SESSION_NAME' + + class _ShutdownStatus(Enum): """ @@ -332,6 +343,7 @@ async def _async_start_kernel(self, **kw): # launch the kernel subprocess self.log.debug("Starting kernel: %s", kernel_cmd) + kw.pop('session_name', None) await ensure_async(self._launch_kernel(kernel_cmd, **kw)) await ensure_async(self.post_start_kernel(**kw)) diff --git a/jupyter_client/multikernelmanager.py b/jupyter_client/multikernelmanager.py index f9e25d809..9683a4b97 100644 --- a/jupyter_client/multikernelmanager.py +++ b/jupyter_client/multikernelmanager.py @@ -151,6 +151,9 @@ def pre_start_kernel( constructor_kwargs = {} if self.kernel_spec_manager: constructor_kwargs["kernel_spec_manager"] = self.kernel_spec_manager + + if 'session_name' in kwargs: + constructor_kwargs['session_name'] = kwargs.copy().pop('session_name') km = self.kernel_manager_factory( connection_file=os.path.join(self.connection_dir, "kernel-%s.json" % kernel_id), parent=self, diff --git a/jupyter_client/provisioning/local_provisioner.py b/jupyter_client/provisioning/local_provisioner.py index 2b2264f9c..1b4deafa6 100644 --- a/jupyter_client/provisioning/local_provisioner.py +++ b/jupyter_client/provisioning/local_provisioner.py @@ -139,6 +139,7 @@ async def pre_launch(self, **kwargs: Any) -> Dict[str, Any]: # This should be considered temporary until a better division of labor can be defined. km = self.parent + extra_arguments = kwargs.pop('extra_arguments', []) if km: if km.transport == 'tcp' and not is_local_ip(km.ip): raise RuntimeError( @@ -149,7 +150,6 @@ async def pre_launch(self, **kwargs: Any) -> Dict[str, Any]: "Currently valid addresses are: %s" % (km.ip, local_ips()) ) # build the Popen cmd - extra_arguments = kwargs.pop('extra_arguments', []) # write connection file / get default ports # TODO - change when handshake pattern is adopted @@ -169,7 +169,6 @@ async def pre_launch(self, **kwargs: Any) -> Dict[str, Any]: extra_arguments=extra_arguments ) # This needs to remain here for b/c else: - extra_arguments = kwargs.pop('extra_arguments', []) kernel_cmd = self.kernel_spec.argv + extra_arguments return await super().pre_launch(cmd=kernel_cmd, **kwargs) diff --git a/jupyter_client/provisioning/provisioner_base.py b/jupyter_client/provisioning/provisioner_base.py index 2ff350419..aeb49183e 100644 --- a/jupyter_client/provisioning/provisioner_base.py +++ b/jupyter_client/provisioning/provisioner_base.py @@ -16,6 +16,16 @@ from ..connect import KernelConnectionInfo +# Name of the env variable that contains the name of the current session associated +# with the kernel we are launching. +# Frontends can decide to set this session name to the name of the file when +# when the kernel is started. +# This is useful in notebook context to find which notebook we are working with +# though we might not be working with a notebook, we could be working with a +# markdown file, or python file. +# as with other Jupyter Related Env variable with use the JPY prefix. +JPY_SESSION_NAME = 'JPY_SESSION_NAME' + class KernelProvisionerMeta(ABCMeta, type(LoggingConfigurable)): # type: ignore pass @@ -160,6 +170,8 @@ async def pre_launch(self, **kwargs: Any) -> Dict[str, Any]: :meth:`launch_kernel()`. """ env = kwargs.pop('env', os.environ).copy() + if 'session_name' in kwargs: + env.update({JPY_SESSION_NAME: kwargs['session_name']}) env.update(self.__apply_env_substitutions(env)) self._finalize_env(env) kwargs['env'] = env