Skip to content

Commit 8b320d1

Browse files
committed
Inject session identifier into environment variable.
There are many use case where users want to know the current notebook name/path. This help by adding a session identifier (to not really say this is the current notebook name), and by default make it the full path to the notebook document that created the session. This will of course not work if the notebook get renamed, but we can tackle this later. See also jupyter/jupyter_client#656, #6180. It will need to be ported to jupyter_server as well.
1 parent 52581f8 commit 8b320d1

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

notebook/services/sessions/sessionmanager.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Distributed under the terms of the Modified BSD License.
55

66
import uuid
7+
from pathlib import Path
78

89
try:
910
import sqlite3
@@ -14,7 +15,6 @@
1415
from tornado import gen, web
1516

1617
from traitlets.config.configurable import LoggingConfigurable
17-
from ipython_genutils.py3compat import unicode_type
1818
from traitlets import Instance
1919

2020
from notebook.utils import maybe_future
@@ -86,7 +86,7 @@ def session_exists(self, path):
8686

8787
def new_session_id(self):
8888
"Create a uuid for a new session"
89-
return unicode_type(uuid.uuid4())
89+
return str(uuid.uuid4())
9090

9191
@gen.coroutine
9292
def create_session(self, path=None, name=None, type=None, kernel_name=None, kernel_id=None):
@@ -107,8 +107,14 @@ def start_kernel_for_session(self, session_id, path, name, type, kernel_name):
107107
"""Start a new kernel for a given session."""
108108
# allow contents manager to specify kernels cwd
109109
kernel_path = self.contents_manager.get_kernel_path(path=path)
110+
111+
fullpath = Path(path).resolve()
110112
kernel_id = yield maybe_future(
111-
self.kernel_manager.start_kernel(path=kernel_path, kernel_name=kernel_name)
113+
self.kernel_manager.start_kernel(
114+
path=kernel_path,
115+
kernel_name=kernel_name,
116+
env={"JPY_SESSION_NAME": fullpath},
117+
)
112118
)
113119
# py2-compat
114120
raise gen.Return(kernel_id)

0 commit comments

Comments
 (0)