diff --git a/examples-proposed/004-time-loop/mymodule/components.py b/examples-proposed/004-time-loop/mymodule/components.py index 00aaa66..418c12e 100644 --- a/examples-proposed/004-time-loop/mymodule/components.py +++ b/examples-proposed/004-time-loop/mymodule/components.py @@ -37,8 +37,8 @@ def step(self, timestamp=0.0): NOTEBOOK_NAME = 'full_state.ipynb' jupyter_state_files = self.services.get_staged_jupyterhub_files() self.services.stage_jupyter_notebook( - dest_notebook_name=NOTEBOOK_NAME, # path is relative to JupyterHub directory - source_notebook_path='base-notebook.ipynb', # path is relative to input directory + dest_notebook_name=NOTEBOOK_NAME, # path is relative to JupyterHub directory + source_notebook_path='base-notebook.ipynb', # path is relative to input directory tags=jupyter_state_files, ) self.services.portal_register_jupyter_notebook(NOTEBOOK_NAME) diff --git a/ipsframework/jupyter.py b/ipsframework/jupyter.py index 711fa23..bfea3ed 100644 --- a/ipsframework/jupyter.py +++ b/ipsframework/jupyter.py @@ -26,17 +26,19 @@ def _get_state_file_notebook_code_cell(variable: str, tags: List[str]): {variable} = [{itemsep.join([f"'data{sep}{file}'" for file in tags])}] """ + def stage_jupyter_notebook(dest: str, src: str, tags: List[str], variable_name: str, index: int): """""" # to avoid conversion, use as_version=nbf.NO_CONVERT - # + # nb: nbf.NotebookNode = nbf.read(src, as_version=4) header = '# Next cell generated by IPS Framework' - nb['cells'] = nb['cells'][:index] + [ - nbf.v4.new_markdown_cell(header), - nbf.v4.new_code_cell(_get_state_file_notebook_code_cell(variable_name, tags)) - ] + nb['cells'][index:] + nb['cells'] = ( + nb['cells'][:index] + + [nbf.v4.new_markdown_cell(header), nbf.v4.new_code_cell(_get_state_file_notebook_code_cell(variable_name, tags))] + + nb['cells'][index:] + ) nbf.validate(nb) with open(dest, 'w') as f: diff --git a/ipsframework/services.py b/ipsframework/services.py index df7c012..a1f0147 100644 --- a/ipsframework/services.py +++ b/ipsframework/services.py @@ -1885,17 +1885,18 @@ def _get_jupyterhub_url(self) -> Optional[str]: url += f'ipsframework/runs/{runid}/' return url - def stage_jupyter_notebook(self, - dest_notebook_name: str, - source_notebook_path: str, - tags: List[str], - variable_name: str = 'FILES', - cell_to_modify: int = 0, - ) -> None: + def stage_jupyter_notebook( + self, + dest_notebook_name: str, + source_notebook_path: str, + tags: List[str], + variable_name: str = 'FILES', + cell_to_modify: int = 0, + ) -> None: """Loads a notebook from source_notebook_path, adds a cell to load the data, and then saves it to source_notebook_path. Does not modify the source notebook. - + Params: - dest_notebook_name: name of the JupyterNotebook you want to write (do not include file paths). - source_notebook_path: location you want to load the source notebook from @@ -1908,7 +1909,7 @@ def stage_jupyter_notebook(self, if not self._jupyterhub_dir: if not self._init_jupyter(): raise Exception('Unable to initialize base JupyterHub dir') - + stage_jupyter_notebook(f'{self._jupyterhub_dir}{dest_notebook_name}', source_notebook_path, tags, variable_name, cell_to_modify) def portal_register_jupyter_notebook(self, notebook_name: str) -> None: