Skip to content

Commit

Permalink
Set JPY_NOTEBOOK_DIR env variable in the kernel
Browse files Browse the repository at this point in the history
This is done to get the dir of the notebook where notebook initialising the kernel belongs.

#980
  • Loading branch information
divyansshhh committed Sep 14, 2022
1 parent 7d2154a commit 392d5da
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion jupyter_server/services/sessions/sessionmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,22 @@ async def create_session(
self._pending_sessions.remove(record)
return result

def _session_path_to_notebook_dir(self, session_path):
"""Remove the tail of the path because it is just a uuid string obtained when starting the kernel,
the head is the dir of the notebook starting the kernel.
"""
notebook_dir, _ = os.path.split(session_path)
return notebook_dir

async def start_kernel_for_session(self, session_id, path, name, type, kernel_name):
"""Start a new kernel for a given session."""
# allow contents manager to specify kernels cwd
kernel_path = await ensure_async(self.contents_manager.get_kernel_path(path=path))
notebook_path = self._session_path_to_notebook_dir(path)
kernel_id = await self.kernel_manager.start_kernel(
path=kernel_path,
kernel_name=kernel_name,
env={**os.environ, "JPY_SESSION_NAME": path},
env={**os.environ, "JPY_SESSION_NAME": path, "JPY_NOTEBOOK_DIR": notebook_path},
)
return kernel_id

Expand Down

0 comments on commit 392d5da

Please sign in to comment.