Skip to content

Commit 4f414d4

Browse files
authored
Merge branch 'main' into output-integration
2 parents 095bb38 + 72585b1 commit 4f414d4

File tree

5 files changed

+58
-41
lines changed

5 files changed

+58
-41
lines changed

jupyter_rtc_core/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import asyncio
44

55
from traitlets import Instance, Type
6-
from .handlers import RouteHandler, YRoomSessionHandler, FileIDIndexHandler
6+
from .handlers import RouteHandler, FileIDIndexHandler
77
from .websockets import GlobalAwarenessWebsocket, YRoomWebsocket
88
from .rooms.yroom_manager import YRoomManager
99
from .outputs import OutputsManager, outputs_handlers

jupyter_rtc_core/handlers.py

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,4 @@ class RouteHandler(APIHandler):
4141
def get(self):
4242
self.finish(json.dumps({
4343
"data": "This is /jupyter-rtc-core/get-example endpoint!"
44-
}))
45-
46-
47-
# TODO: remove this by v1.0.0 if deemed unnecessary. Just adding this for
48-
# compatibility with the `jupyter_collaboration` frontend.
49-
class YRoomSessionHandler(APIHandler):
50-
SESSION_ID = str(uuid.uuid4())
51-
52-
@tornado.web.authenticated
53-
def put(self, path):
54-
body = json.loads(self.request.body)
55-
format = body["format"]
56-
content_type = body["type"]
57-
# self.log.info("IN HANDLER")
58-
# for k, v in self.settings.items():
59-
# print(f"{k}: {v}")
60-
# print(len(self.settings.items()))
61-
# print(id(self.settings))
62-
63-
file_id_manager = self.settings["file_id_manager"]
64-
file_id = file_id_manager.index(path)
65-
66-
data = json.dumps(
67-
{
68-
"format": format,
69-
"type": content_type,
70-
"fileId": file_id,
71-
"sessionId": self.SESSION_ID,
72-
}
73-
)
74-
self.set_status(200)
75-
self.finish(data)
76-
44+
}))

jupyter_rtc_core/kernels/kernel_client.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ async def stop_listening(self):
8585
# Log any exceptions that were raised.
8686
except Exception as err:
8787
self.log.error(err)
88-
88+
8989
_listening_task: t.Optional[t.Awaitable] = Any(allow_none=True)
9090

9191
def handle_incoming_message(self, channel_name: str, msg: list[bytes]):
@@ -181,7 +181,7 @@ async def handle_outgoing_message(self, channel_name: str, msg: list[bytes]):
181181
return
182182

183183
# Update the last activity.
184-
#self.last_activity = self.session.msg_time
184+
# self.last_activity = self.session.msg_time
185185

186186
await self.send_message_to_listeners(channel_name, msg)
187187

@@ -229,6 +229,32 @@ async def handle_iopub_message(self, msg: list[bytes]) -> t.Optional[list[bytes]
229229
else:
230230
return msg
231231

232+
# NOTE: Inject display data into ydoc.
233+
if dmsg["msg_type"] == "display_data":
234+
# Forward to all yrooms.
235+
for yroom in self._yrooms:
236+
update_document_message = b""
237+
# yroom.add_message(update_document_message)
238+
239+
# TODO: returning message temporarily to not break UI
240+
# If the message isn't handled above, return it and it will
241+
# be forwarded to all listeners
242+
return msg
243+
244+
def send_kernel_awareness(self, kernel_status: dict):
245+
"""
246+
Send kernel status awareness messages to all yrooms
247+
"""
248+
for yroom in self._yrooms:
249+
awareness = yroom.get_awareness()
250+
if awareness is None:
251+
self.log.error(f"awareness cannot be None. room_id: {yroom.room_id}")
252+
continue
253+
self.log.debug(f"current state: {awareness.get_local_state()} room_id: {yroom.room_id}. kernel status: {kernel_status}")
254+
awareness.set_local_state_field("kernel", kernel_status)
255+
self.log.debug(f"current state: {awareness.get_local_state()} room_id: {yroom.room_id}")
256+
257+
232258
async def add_yroom(self, yroom: YRoom):
233259
"""
234260
Register a YRoom with this kernel client. YRooms will

jupyter_rtc_core/kernels/kernel_manager.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ def set_state(
107107

108108
if broadcast:
109109
# Broadcast this state change to all listeners
110-
# Turn off state broadcasting temporarily to avoid
111110
self._state_observers = None
112111
self.broadcast_state()
113112

@@ -202,4 +201,14 @@ def execution_state_listener(self, channel_name: str, msg: list[bytes]):
202201
if parent_channel and parent_channel == "shell":
203202
# Don't broadcast, since this message is already going out.
204203
self.set_state(LifecycleStates.CONNECTED, ExecutionStates(execution_state), broadcast=False)
204+
205+
kernel_status = {
206+
"execution_state": self.execution_state,
207+
"lifecycle_state": self.lifecycle_state
208+
}
209+
self.log.debug(f"Sending kernel status awareness {kernel_status}")
210+
self.main_client.send_kernel_awareness(kernel_status)
211+
self.log.debug(f"Sent kernel status awareness {kernel_status}")
212+
213+
205214

jupyter_rtc_core/session_manager.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from typing import Optional, Any
23
from jupyter_server.services.sessions.sessionmanager import SessionManager, KernelName, ModelName
34
from jupyter_server.serverapp import ServerApp
@@ -8,7 +9,7 @@
89

910

1011
class YDocSessionManager(SessionManager):
11-
"""A Jupyter Server Session Manager that's connects YDocuments
12+
"""A Jupyter Server Session Manager that connects YDocuments
1213
to Kernel Clients.
1314
"""
1415

@@ -65,10 +66,23 @@ async def create_session(
6566
)
6667
if kernel_id is None:
6768
kernel_id = output["kernel"]["id"]
68-
69+
6970
# Connect this session's yroom to the kernel.
70-
if type == "notebook":
71-
yroom = self.get_yroom(path, type)
71+
if type == "notebook":
72+
# If name or path is None, we cannot map to a yroom,
73+
# so just move on.
74+
if name is None or path is None:
75+
self.log.debug("`name` or `path` was not given, so a yroom was not set up for this session.")
76+
return output
77+
# When JupyterLab creates a session, it uses a fake path
78+
# which is the relative path + UUID, i.e. the notebook
79+
# name is incorrect temporarily. It later makes multiple
80+
# updates to the session to correct the path.
81+
#
82+
# Here, we create the true path to store in the fileID service
83+
# by dropping the UUID and appending the file name.
84+
real_path = os.path.join(os.path.split(path)[0], name)
85+
yroom = self.get_yroom(real_path, type)
7286
# TODO: we likely have a race condition here... need to
7387
# think about it more. Currently, the kernel client gets
7488
# created after the kernel starts fully. We need the

0 commit comments

Comments
 (0)