diff --git a/jupyter_server_documents/outputs/manager.py b/jupyter_server_documents/outputs/manager.py index 595ba3a..ba4f1af 100644 --- a/jupyter_server_documents/outputs/manager.py +++ b/jupyter_server_documents/outputs/manager.py @@ -302,7 +302,7 @@ def _process_loaded_no_placeholders(self, file_id: str, nb: dict) -> dict: cell['outputs'] = processed_outputs return nb - def process_saving_notebook(self, nb: dict) -> dict: + def process_saving_notebook(self, nb: dict, file_id: str) -> dict: """Process a notebook before saving to disk. This method is called when the yroom_file_api saves notebooks. @@ -311,6 +311,7 @@ def process_saving_notebook(self, nb: dict) -> dict: Args: nb (dict): The notebook dict + file_id (str): The file identifier Returns: dict: The modified file data with placeholder_outputs set to True @@ -326,6 +327,12 @@ def process_saving_notebook(self, nb: dict) -> dict: # Clear outputs for all code cells, as they are saved to disk for cell in nb.get('cells', []): if cell.get('cell_type') == 'code': + # If outputs is already an empty list, call clear for this cell + if cell.get('outputs') == []: + cell_id = cell.get('id') + if cell_id: + self.clear(file_id, cell_id) + cell['outputs'] = [] return nb diff --git a/jupyter_server_documents/rooms/yroom_file_api.py b/jupyter_server_documents/rooms/yroom_file_api.py index 05078d4..72eabd1 100644 --- a/jupyter_server_documents/rooms/yroom_file_api.py +++ b/jupyter_server_documents/rooms/yroom_file_api.py @@ -376,7 +376,7 @@ async def save(self, jupyter_ydoc: YBaseDoc): self._save_scheduled = False if self.file_type == "notebook": - content = self.outputs_manager.process_saving_notebook(content) + content = self.outputs_manager.process_saving_notebook(content, self.file_id) # Save the YDoc via the ContentsManager async with self._content_lock: