Skip to content

Commit 414cfe2

Browse files
committed
root_dir defaults to current working directorY
1 parent fcd7979 commit 414cfe2

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

kernel_gateway/services/kernels/manager.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33
"""Kernel manager that optionally seeds kernel memory."""
4-
4+
import os
55
from typing import List, Optional
6+
from traitlets import default
67
from jupyter_server.services.kernels.kernelmanager import AsyncMappingKernelManager
78
from jupyter_client.ioloop import AsyncIOLoopKernelManager
89

@@ -15,8 +16,14 @@ class SeedingMappingKernelManager(AsyncMappingKernelManager):
1516
_seed_source: Optional[List]
1617
_seed_kernelspec: Optional[str]
1718

19+
@default("root_dir")
20+
def _default_root_dir(self):
21+
return os.getcwd()
22+
1823
def _kernel_manager_class_default(self):
19-
return 'kernel_gateway.services.kernels.manager.KernelGatewayIOLoopKernelManager'
24+
return (
25+
"kernel_gateway.services.kernels.manager.KernelGatewayIOLoopKernelManager"
26+
)
2027

2128
@property
2229
def seed_kernelspec(self) -> Optional[str]:
@@ -30,14 +37,16 @@ def seed_kernelspec(self) -> Optional[str]:
3037
str
3138
Name of the notebook kernelspec or None if no seed notebook exists
3239
"""
33-
if hasattr(self, '_seed_kernelspec'):
40+
if hasattr(self, "_seed_kernelspec"):
3441
return self._seed_kernelspec
3542

3643
if self.parent.seed_notebook:
3744
if self.parent.force_kernel_name:
3845
self._seed_kernelspec = self.parent.force_kernel_name
3946
else:
40-
self._seed_kernelspec = self.parent.seed_notebook['metadata']['kernelspec']['name']
47+
self._seed_kernelspec = self.parent.seed_notebook["metadata"][
48+
"kernelspec"
49+
]["name"]
4150
else:
4251
self._seed_kernelspec = None
4352

@@ -52,13 +61,14 @@ def seed_source(self) -> Optional[List]:
5261
list
5362
Notebook code cell contents or None if no seed notebook exists
5463
"""
55-
if hasattr(self, '_seed_source'):
64+
if hasattr(self, "_seed_source"):
5665
return self._seed_source
5766

5867
if self.parent.seed_notebook:
5968
self._seed_source = [
60-
cell['source'] for cell in self.parent.seed_notebook.cells
61-
if cell['cell_type'] == 'code'
69+
cell["source"]
70+
for cell in self.parent.seed_notebook.cells
71+
if cell["cell_type"] == "code"
6272
]
6373
else:
6474
self._seed_source = None
@@ -78,8 +88,10 @@ async def start_kernel(self, *args, **kwargs):
7888
seed notebook exists.
7989
"""
8090
if self.parent.force_kernel_name:
81-
kwargs['kernel_name'] = self.parent.force_kernel_name
82-
kernel_id = await super(SeedingMappingKernelManager, self).start_kernel(*args, **kwargs)
91+
kwargs["kernel_name"] = self.parent.force_kernel_name
92+
kernel_id = await super(SeedingMappingKernelManager, self).start_kernel(
93+
*args, **kwargs
94+
)
8395

8496
if kernel_id and self.seed_source is not None:
8597
# Only run source if the kernel spec matches the notebook kernel spec
@@ -104,13 +116,15 @@ async def start_kernel(self, *args, **kwargs):
104116
msg_type = "kernel_info_reply"
105117
while msg_type == "kernel_info_reply":
106118
msg = await client.get_shell_msg()
107-
msg_type = msg['msg_type']
108-
if msg['content']['status'] != 'ok':
119+
msg_type = msg["msg_type"]
120+
if msg["content"]["status"] != "ok":
109121
# Shutdown the channels to remove any lingering ZMQ messages
110122
client.stop_channels()
111123
# Shutdown the kernel
112124
await self.shutdown_kernel(kernel_id)
113-
raise RuntimeError('Error seeding kernel memory', msg['content'])
125+
raise RuntimeError(
126+
"Error seeding kernel memory", msg["content"]
127+
)
114128
# Shutdown the channels to remove any lingering ZMQ messages
115129
client.stop_channels()
116130
return kernel_id
@@ -121,14 +135,16 @@ class KernelGatewayIOLoopKernelManager(AsyncIOLoopKernelManager):
121135
122136
Sets the environment variable 'KERNEL_GATEWAY' to '1' to indicate that the
123137
kernel is executing within a Jupyter Kernel Gateway instance. Removes the
124-
KG_AUTH_TOKEN from the environment variables passed to the kernel when it
138+
KG_AUTH_TOKEN from the environment variables passed to the kernel when it
125139
starts.
126140
"""
127141

128142
async def _async_launch_kernel(self, kernel_cmd, **kw):
129143
# TODO - should probably figure out a better place to deal with this
130-
env = kw['env']
131-
env['KERNEL_GATEWAY'] = '1'
132-
if 'KG_AUTH_TOKEN' in env:
133-
del env['KG_AUTH_TOKEN']
134-
return await super(KernelGatewayIOLoopKernelManager, self)._async_launch_kernel(kernel_cmd, **kw)
144+
env = kw["env"]
145+
env["KERNEL_GATEWAY"] = "1"
146+
if "KG_AUTH_TOKEN" in env:
147+
del env["KG_AUTH_TOKEN"]
148+
return await super(KernelGatewayIOLoopKernelManager, self)._async_launch_kernel(
149+
kernel_cmd, **kw
150+
)

0 commit comments

Comments
 (0)