diff --git a/cadetrdm/__init__.py b/cadetrdm/__init__.py index 461fa2503a07f7ea10a21daf5872b398558a5dd8..79c82684dbfc7b7b60a1bcf12c930067e2f27da2 100644 --- a/cadetrdm/__init__.py +++ b/cadetrdm/__init__.py @@ -1,4 +1,4 @@ -from .repositories import ProjectRepo +from .repositories import ProjectRepo, JupyterInterfaceRepo from .initialize_repo import initialize_repo, clone from .conda_env_utils import prepare_conda_env from .version import version diff --git a/cadetrdm/jupyter_functionality.py b/cadetrdm/jupyter_functionality.py index 2bb382675df18f9862535e39a9fae2ec1e65af11..35db56351c926e9cbec9866a07a401886d05816a 100644 --- a/cadetrdm/jupyter_functionality.py +++ b/cadetrdm/jupyter_functionality.py @@ -112,14 +112,15 @@ class Notebook: 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.") - return - else: - rerun_confirmed_bool = wait_for_user("Notebook was not in order, rerun notebook now?") - if not rerun_confirmed_bool and not force_rerun: - print("Aborting.") + if not force_rerun: + if is_in_order: + print("Notebook was already executed in order.") return + else: + rerun_confirmed_bool = wait_for_user("Rerun notebook now?") + if not rerun_confirmed_bool: + print("Aborting.") + return print("Rerunning.") with open(self.notebook_path) as f: diff --git a/cadetrdm/repositories.py b/cadetrdm/repositories.py index 09813e31f1d8ca406fe1ae803b3d7a60adc95725..8a347c672793412de044dcde79f1e936f0ee84b2 100644 --- a/cadetrdm/repositories.py +++ b/cadetrdm/repositories.py @@ -1012,18 +1012,22 @@ class ProjectRepo(BaseRepo): self._on_context_enter_commit_hash = None @contextlib.contextmanager - def track_results(self, results_commit_message: str, debug=False): + def track_results(self, results_commit_message: str, debug=False, force=False): """ Context manager to be used when running project code that produces output that should be tracked in the output repository. :param results_commit_message: Commit message for the commit of the output repository. + :param debug: + Perform calculations without tracking output. + :param force: + Skip confirmation and force tracking of results. """ if debug: yield "debug" return - new_branch_name = self.enter_context() + new_branch_name = self.enter_context(force=force) try: yield new_branch_name except Exception as e: @@ -1040,25 +1044,28 @@ class OutputRepo(BaseRepo): class JupyterInterfaceRepo(ProjectRepo): def commit(self, message: str, add_all=True): + if "nbconvert_call" in sys.argv: + print("Not committing during nbconvert.") + return + Notebook.save_ipynb() + super().commit(message, add_all) def commit_nb_output(self, notebook_path: str, results_commit_message: str, - force_rerun=False, timeout=600, conversion_formats: list = None): + force_rerun=True, timeout=600, conversion_formats: list = None): + if "nbconvert_call" in sys.argv: + return + # This is reached in the first call of this function if not Path(notebook_path).is_absolute(): notebook_path = self.working_dir / notebook_path notebook = Notebook(notebook_path) - if "nbconvert_call" not in sys.argv: - # This is reached in the first call of this function - self.enter_context(force=False) - + with self.track_results(results_commit_message, force=True): notebook.check_and_rerun_notebook(force_rerun=force_rerun, timeout=timeout) - else: - # This is executed during the nbconvert call + + # This is executed after the nbconvert call notebook.convert_ipynb(self.output_path, formats=conversion_formats) notebook.export_all_figures(self.output_path) - - self.exit_context(results_commit_message)