diff --git a/doc/conf.py b/doc/conf.py index 9be64872c8c00c617dfa0d2726d93df88e65b925..84b783e6fa0f254fa5fea0d2c5f20aa63ac25e26 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -21,7 +21,7 @@ from sphinx.application import Sphinx sys.path.insert(0, os.path.abspath('.')) # add doc/ to the path #sys.path.insert(0, os.path.abspath('./tutorials')) # Add the `tutorials` directory -from utils.convert_notebook import process_notebook +from utils.convert_notebook import process_notebook, clear_notebook # ---------------------- Process Tutorials ---------------------- # source_tutorials_dir = '../tutorials' @@ -93,9 +93,11 @@ def clean_doc(app: Sphinx, exception): Remove temporary folders created during the build process. Cleans up the tutorials folder and the _dvlpt folder after the build is complete. """ - # do not clean the tutorials/ folder in order to zip its content in CI/CD - #if os.path.exists(tutorial_output_dir): - # print(f"Cleaning up {tutorial_output_dir}") + # clean the tutorials/ folder in order to zip its content in CI/CD + if os.path.exists(tutorial_output_dir): + print(f"Cleaning up {tutorial_output_dir}") + for notebook in notebooks: + clear_notebook(notebook_path=os.path.join(tutorial_output_dir, notebook)) # shutil.rmtree(tutorial_output_dir, ignore_errors=True) # Clean up the _dvlpt folder diff --git a/doc/utils/convert_notebook.py b/doc/utils/convert_notebook.py index 51794f561924776a7ace8694c2973849e1912e16..e0ffbeb8381b3e964bba04fdd676946f678ab0fb 100644 --- a/doc/utils/convert_notebook.py +++ b/doc/utils/convert_notebook.py @@ -45,7 +45,7 @@ def process_notebook(input_notebook_path, output_notebook_path, dynamic_plot=Fal print(f"Loaded notebook: {input_notebook_path}") # Step 2: Clear outputs - cleared_notebook = clear_notebook(notebook_content) + cleared_notebook = clear_notebook(notebook_content=notebook_content) # Step 3: Modify notebook content for PyVista modified_notebook = modify_notebook_for_pyvista(cleared_notebook, @@ -142,27 +142,44 @@ def process_notebook_deprecated(input_notebook_path, output_html_path=None, dyna return None -def clear_notebook(notebook_content): +def clear_notebook(notebook_content=None, notebook_path=None): """ Clear all outputs in a Jupyter notebook content. This function removes all cell outputs and return the notebook content. Parameters ---------- - notebook_content : dict + notebook_content : dict, optional The loaded notebook content (as a Python dictionary). - + notebook_path : str, optional + Path to the Jupyter notebook file (.ipynb). Returns ------- dict Cleared notebook content. """ try: + if notebook_path is not None: + if not os.path.exists(notebook_path): + raise FileNotFoundError(f"Notebook not found: {notebook_path}") + + with open(notebook_path, 'r', encoding='utf-8') as f: + notebook_content = nbformat.read(f, as_version=4) + print(f"Loaded notebook: {notebook_path}") + else: + assert notebook_content is not None + # Clear outputs from the notebook clear_preprocessor = ClearOutputPreprocessor() cleared_notebook, _ = clear_preprocessor.preprocess(notebook_content, {}) print("Cleared notebook outputs.") - return cleared_notebook + + if notebook_path is not None: + return cleared_notebook + else: + with open(notebook_path, 'w', encoding='utf-8') as f: + nbformat.write(notebook_content, f) + print(f"notebook written to: {notebook_path}") except Exception as e: print(f"Error while clearing notebook outputs: {e}")