Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
Signed-off-by: Lance-Drane <[email protected]>
  • Loading branch information
Lance-Drane committed Jul 24, 2024
1 parent 5dd8406 commit 62f6d8d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
4 changes: 2 additions & 2 deletions examples-proposed/004-time-loop/mymodule/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions ipsframework/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 10 additions & 9 deletions ipsframework/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 62f6d8d

Please sign in to comment.