Skip to content

Commit 392d5da

Browse files
committed
Set JPY_NOTEBOOK_DIR env variable in the kernel
This is done to get the dir of the notebook where notebook initialising the kernel belongs. #980
1 parent 7d2154a commit 392d5da

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

jupyter_server/services/sessions/sessionmanager.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,22 @@ async def create_session(
268268
self._pending_sessions.remove(record)
269269
return result
270270

271+
def _session_path_to_notebook_dir(self, session_path):
272+
"""Remove the tail of the path because it is just a uuid string obtained when starting the kernel,
273+
the head is the dir of the notebook starting the kernel.
274+
"""
275+
notebook_dir, _ = os.path.split(session_path)
276+
return notebook_dir
277+
271278
async def start_kernel_for_session(self, session_id, path, name, type, kernel_name):
272279
"""Start a new kernel for a given session."""
273280
# allow contents manager to specify kernels cwd
274281
kernel_path = await ensure_async(self.contents_manager.get_kernel_path(path=path))
282+
notebook_path = self._session_path_to_notebook_dir(path)
275283
kernel_id = await self.kernel_manager.start_kernel(
276284
path=kernel_path,
277285
kernel_name=kernel_name,
278-
env={**os.environ, "JPY_SESSION_NAME": path},
286+
env={**os.environ, "JPY_SESSION_NAME": path, "JPY_NOTEBOOK_DIR": notebook_path},
279287
)
280288
return kernel_id
281289

0 commit comments

Comments
 (0)