Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lay foundation to pass notebook names to kernel at startup. #656

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions jupyter_client/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""

Expand Down Expand Up @@ -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))

Expand Down
3 changes: 3 additions & 0 deletions jupyter_client/multikernelmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just kwargs['session_name'] ?

km = self.kernel_manager_factory(
connection_file=os.path.join(self.connection_dir, "kernel-%s.json" % kernel_id),
parent=self,
Expand Down
3 changes: 1 addition & 2 deletions jupyter_client/provisioning/local_provisioner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions jupyter_client/provisioning/provisioner_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down