From 6025b3d9ffe7fab295d605e7ee70c0eaf3e568c3 Mon Sep 17 00:00:00 2001 From: "r.jaepel" <r.jaepel@fz-juelich.de> Date: Mon, 4 Dec 2023 17:29:57 +0100 Subject: [PATCH] Incorporate jupyter functionality --- cadetrdm/jupyter_functionality.py | 33 ++++++++++++++----------------- cadetrdm/repositories.py | 15 ++++++++++++++ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/cadetrdm/jupyter_functionality.py b/cadetrdm/jupyter_functionality.py index d932e73..19b5d43 100644 --- a/cadetrdm/jupyter_functionality.py +++ b/cadetrdm/jupyter_functionality.py @@ -14,6 +14,10 @@ class Notebook: def __init__(self, notebook_path): self.notebook_path = Path(notebook_path) + @property + def notebook_name(self): + return str(self.notebook_path.name).replace(".", "_") + def check_execution_order(self, check_all_executed=False, check_top_to_bottom=False, @@ -96,21 +100,15 @@ class Notebook: app = JupyterFrontEnd() app.commands.execute('docmanager:reload') - def wait_for_user(self, message): - proceed = input(message + " Y/n") - if proceed.lower() == "y" or proceed == "": - return True - else: - return False - - def clear_and_rerun_notebook(self, force_rerun=False, timeout=600): + def check_and_rerun_notebook(self, force_rerun=False, timeout=600): if "nbconvert_call" in sys.argv: return self.save_ipynb() + # wait a second for the save process to finish. time.sleep(1) - is_in_order = self.check_execution_order(self.notebook_filename, exclude_last_cell=False) + is_in_order = self.check_execution_order(exclude_last_cell=False) if is_in_order and not force_rerun: print("Notebook was already executed in order.") @@ -122,27 +120,26 @@ class Notebook: return print("Rerunning.") - with open(self.notebook_filename) as f: + with open(self.notebook_path) as f: nb = nbf.read(f, as_version=4) ep = ExecutePreprocessor(timeout=timeout, kernel_name='python3', extra_arguments=["nbconvert_call"]) ep.preprocess(nb, ) - with open(self.notebook_filename, 'w', encoding='utf-8') as f: + with open(self.notebook_path, 'w', encoding='utf-8') as f: nbf.write(nb, f) self.reload_notebook() - def convert_ipynb(self, formats: list = None): + def convert_ipynb(self, output_dir, formats: list = None): if formats is None: formats = ["pdf", "python"] app = NbConvertApp() app.initialize() - output_root_directory = os.path.join(r"C:\Users\ronal\PycharmProjects\git_lfs_test_2", "results", - self.notebook_path.name.replace('.', '_')) + output_root_directory = os.path.join(output_dir, self.notebook_name) for export_format in formats: app.export_format = export_format - app.notebooks = [self.notebook_path] + app.notebooks = [str(self.notebook_path)] app.output_base = os.path.join(output_root_directory, self.notebook_path.name.replace('.ipynb', '')) if not os.path.exists(output_root_directory): @@ -151,7 +148,7 @@ class Notebook: def export_all_figures(self, output_dir): file_without_extension = self.notebook_path.stem - images = junix.export_images(filepath=self.notebook_path, - output_dir=os.path.join(output_dir, - str(self.notebook_path.name).replace(".", "_")), + images = junix.export_images(filepath=str(self.notebook_path), + output_dir=os.path.join(output_dir, self.notebook_name), prefix=file_without_extension) + diff --git a/cadetrdm/repositories.py b/cadetrdm/repositories.py index 7e3452e..d6603f8 100644 --- a/cadetrdm/repositories.py +++ b/cadetrdm/repositories.py @@ -1015,6 +1015,21 @@ class ProjectRepo(BaseRepo): else: self.exit_context(message=results_commit_message) + def commit_notebook(self, notebook_path: str, results_commit_message: str, + force_rerun=False, timeout=600, conversion_formats: list = None): + if not Path(notebook_path).is_absolute(): + notebook_path = self.working_dir / notebook_path + + self.enter_context() + + notebook = Notebook(notebook_path) + notebook.check_and_rerun_notebook(force_rerun=force_rerun, + timeout=timeout) + notebook.convert_ipynb(self.output_path, formats=conversion_formats) + notebook.export_all_figures(self.output_path) + + self.exit_context(results_commit_message) + class OutputRepo(BaseRepo): pass -- GitLab