Skip to content

Commit 49dbf0f

Browse files
authored
Allow to update kernels env in between restart. (#987)
This allow to update the running env of kernels. This will allow resolves some inconsistencies between kernel restart, and actually stopping and starting a kernel session. In parsley via a few lines in Jupyter Server in update_session, this will let us update the `__session__` which is supposed to reflect some information about the current session, and help correct ipython/ipykernel#1102 where renaming a notebook is not reflected in the kernel unless you manually stop/start the session, or restart the server.
1 parent 05e88b1 commit 49dbf0f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

jupyter_client/manager.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,21 @@ def client(self, **kwargs: t.Any) -> BlockingKernelClient:
268268
# Kernel management
269269
# --------------------------------------------------------------------------
270270

271+
def update_env(self, *, env: t.Dict[str, str]) -> None:
272+
"""
273+
Allow to update the environment of a kernel manager.
274+
275+
This will take effect only after kernel restart when the new env is
276+
passed to the new kernel.
277+
278+
This is useful as some of the information of the current kernel reflect
279+
the state of the session that started it, and those session information
280+
(like the attach file path, or name), are mutable.
281+
282+
.. version-added: 8.5
283+
"""
284+
self._launch_args['env'].update(env)
285+
271286
def format_kernel_cmd(self, extra_arguments: t.Optional[t.List[str]] = None) -> t.List[str]:
272287
"""Replace templated args (e.g. {connection_file})"""
273288
extra_arguments = extra_arguments or []

jupyter_client/multikernelmanager.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,17 @@ def pre_start_kernel(
212212
)
213213
return km, kernel_name, kernel_id
214214

215+
def update_env(self, *, kernel_id: str, env: t.Dict[str, str]) -> None:
216+
"""
217+
Allow to update the environment of the given kernel.
218+
219+
Forward the update env request to the corresponding kernel.
220+
221+
.. version-added: 8.5
222+
"""
223+
if kernel_id in self:
224+
self._kernels[kernel_id].update_env(env)
225+
215226
async def _add_kernel_when_ready(
216227
self, kernel_id: str, km: KernelManager, kernel_awaitable: t.Awaitable
217228
) -> None:

0 commit comments

Comments
 (0)