-
Notifications
You must be signed in to change notification settings - Fork 7
Handle clear output msg #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
3coins
merged 5 commits into
jupyter-ai-contrib:main
from
3coins:handle-clear-output-msg
Jun 16, 2025
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,14 @@ | |
|
|
||
| from pycrdt import Map | ||
|
|
||
| from traitlets import Unicode, Bool | ||
| from traitlets import Unicode, Bool, Dict | ||
| from traitlets.config import LoggingConfigurable | ||
| from jupyter_server_documents.kernels.message_cache import KernelMessageCache | ||
|
|
||
| class OutputProcessor(LoggingConfigurable): | ||
|
|
||
| _file_id = Unicode(default_value=None, allow_none=True) | ||
| _pending_clear_output_cells = Dict(default_value={}) | ||
|
|
||
| use_outputs_service = Bool( | ||
| default_value=True, | ||
|
|
@@ -46,29 +47,54 @@ def yroom_manager(self): | |
| """A shortcut for the jupyter server ydoc manager.""" | ||
| return self.settings["yroom_manager"] | ||
|
|
||
| async def clear_cell_outputs(self, cell_id, room_id): | ||
| """Clears the outputs of a cell in ydoc""" | ||
| async def get_jupyter_ydoc(self, file_id): | ||
| room_id = f"json:notebook:{file_id}" | ||
| room = self.yroom_manager.get_room(room_id) | ||
| if room is None: | ||
| self.log.error(f"YRoom not found: {room_id}") | ||
| return | ||
| notebook = await room.get_jupyter_ydoc() | ||
| self.log.info(f"Notebook: {notebook}") | ||
|
|
||
| ydoc = await room.get_jupyter_ydoc() | ||
|
|
||
| return ydoc | ||
|
|
||
| async def _clear_ydoc_outputs(self, cell_id): | ||
| """Clears the outputs of a cell in ydoc""" | ||
|
|
||
| if not self._file_id: | ||
| return | ||
|
|
||
| notebook = await self.get_jupyter_ydoc(self._file_id) | ||
| cell_index, target_cell = notebook.find_cell(cell_id) | ||
| if target_cell is not None: | ||
| target_cell["outputs"].clear() | ||
| self.log.info(f"Cleared outputs for ydoc: {room_id} {cell_index}") | ||
| self.log.info(f"Cleared outputs for {self._file_id=}, {cell_index=}") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool! Never seen this before. |
||
|
|
||
| def clear_cell_outputs(self, cell_id) -> asyncio.Task | None: | ||
| """ | ||
| Clears all outputs for a cell on disk and in ydoc. Returns an | ||
| `asyncio.Task` that clears outputs for the cell in ydoc. Callers | ||
| should wait for this task to complete, if they expect to update | ||
| the ydoc. | ||
|
|
||
| ``` | ||
| clear_output_task = clear("cellid") | ||
| await clear_outputs_task | ||
| ``` | ||
| """ | ||
|
|
||
| clear_outputs_task = None | ||
|
|
||
| def clear(self, cell_id): | ||
| """Clear all outputs for a given cell Id.""" | ||
| if self._file_id is not None: | ||
| if self.use_outputs_service: | ||
| room_id = f"json:notebook:{self._file_id}" | ||
| asyncio.create_task(self.clear_cell_outputs(cell_id, room_id)) | ||
| clear_outputs_task = asyncio.create_task( | ||
| self._clear_ydoc_outputs(cell_id) | ||
| ) | ||
| self.outputs_manager.clear(file_id=self._file_id, cell_id=cell_id) | ||
|
|
||
| self._pending_clear_output_cells.pop(cell_id, None) | ||
|
|
||
| return clear_outputs_task | ||
3coins marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Outgoing messages | ||
| def process_output(self, msg_type: str, cell_id: str, content: dict): | ||
| """Process outgoing messages from the kernel. | ||
|
|
||
|
|
@@ -87,6 +113,24 @@ def process_output(self, msg_type: str, cell_id: str, content: dict): | |
|
|
||
| async def output_task(self, msg_type, cell_id, content): | ||
| """A coroutine to handle output messages.""" | ||
|
|
||
| if msg_type == "clear_output": | ||
| wait = content.get("wait", False) | ||
| if not wait: | ||
3coins marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| clear_task = self.clear_cell_outputs(cell_id) | ||
| if clear_task: | ||
| await clear_task | ||
| else: | ||
| self._pending_clear_output_cells[cell_id] = True | ||
|
|
||
| return | ||
|
|
||
| # Check for pending clear_output before processing output | ||
| if cell_id in self._pending_clear_output_cells and ( | ||
| clear_task := self.clear_cell_outputs(cell_id) | ||
| ): | ||
| await clear_task | ||
|
|
||
3coins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try: | ||
| # TODO: The session manager may have multiple notebooks connected to the kernel | ||
| # but currently get_session only returns the first. We need to fix this and | ||
|
|
@@ -117,15 +161,10 @@ async def output_task(self, msg_type, cell_id, content): | |
| else: | ||
| output = self.transform_output(msg_type, content, ydoc=True) | ||
|
|
||
| # Get the notebook ydoc from the room | ||
| room_id = f"json:notebook:{file_id}" | ||
| room = self.yroom_manager.get_room(room_id) | ||
| if room is None: | ||
| self.log.error(f"YRoom not found: {room_id}") | ||
| notebook = await self.get_jupyter_ydoc(file_id) | ||
| if not notebook: | ||
| return | ||
| notebook = await room.get_jupyter_ydoc() | ||
| self.log.info(f"Notebook: {notebook}") | ||
|
|
||
|
|
||
| # Write the outputs to the ydoc cell. | ||
| _, target_cell = notebook.find_cell(cell_id) | ||
| if target_cell is not None and output is not None: | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.