Skip to content

Commit c259b8f

Browse files
committed
Jupyter API: improve data module and api interactions
data module can now load its files from both JupyterHub and a download improve importlib.reload interaction in user-generated notebooks have the API include itself in tarball generation Signed-off-by: Lance-Drane <[email protected]>
1 parent f915e44 commit c259b8f

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

ipsframework/_jupyter/api_v1.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,17 @@ def generate_tar_from_runids(runids: Union[Iterable[int], int]) -> str:
4949
"""
5050
tarball_name = f'{datetime.datetime.now(datetime.timezone.utc).isoformat().replace(":", "-").replace("+", "_")}__ips_runs'
5151
tarball = THIS_DIR / f'{tarball_name}.tar.gz'
52-
archive = tarfile.open(tarball, 'w:gz')
52+
with tarfile.open(tarball, 'w:gz') as archive:
53+
# add API files inside the tarball
54+
for api_file in THIS_DIR.glob('api_v*.py'):
55+
archive.add(api_file, arcname=os.path.join(tarball_name, api_file.name))
5356

54-
if isinstance(runids, int):
55-
runids = [runids]
57+
if isinstance(runids, int):
58+
runids = [runids]
5659

57-
for runid in runids:
58-
arcname = os.path.join(tarball_name, str(runid), 'data')
59-
archive.add(os.path.join(THIS_DIR, str(runid), 'data'), arcname=arcname)
60+
# add runids in directory
61+
for runid in runids:
62+
arcname = os.path.join(tarball_name, str(runid))
63+
archive.add(os.path.join(THIS_DIR, str(runid)), arcname=arcname)
6064

61-
archive.close()
6265
return str(tarball)

ipsframework/_jupyter/initializer.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import re
1818
import shutil
19-
from os.path import sep
2019
from pathlib import Path
2120
from typing import Optional
2221

@@ -37,13 +36,13 @@ def replace_last(source_string: str, old: str, new: str) -> str:
3736
return f'{head}{new}{tail}'
3837

3938

40-
def _initial_data_file_code(dest: str) -> str:
39+
def _initial_data_file_code() -> str:
4140
return f"""# This file should be imported by a jupyter notebook or the generated API. DO NOT EDIT UNTIL IPS RUN IS FINALIZED.
4241
4342
import os
43+
import pathlib
4444
45-
# NOTE: directory should be sim_name plus the run id from the Portal
46-
{DIRECTORY_VARIABLE_NAME} = '{str(Path(dest).parent / 'data') + sep}'
45+
{DIRECTORY_VARIABLE_NAME} = str(pathlib.Path(__file__).resolve().parent / 'data') + os.path.sep
4746
{DATA_VARIABLE_NAME} = {{
4847
}}
4948
"""
@@ -69,12 +68,17 @@ def initialize_jupyter_notebook(notebook_dest: str, notebook_src: str):
6968

7069
nb['cells'] = [
7170
# explicitly mark the IPS cell for users inspecting the file, unused programatically
72-
nbf.v4.new_markdown_cell('## Next cell generated by IPS Framework'),
71+
nbf.v4.new_markdown_cell("""## Next cell generated by IPS Framework
72+
73+
Execute this cell again to use new data during the simulation.
74+
"""),
7375
nbf.v4.new_code_cell(f"""
74-
from {DATA_MODULE_NAME} import {DATA_VARIABLE_NAME}
7576
import importlib
7677
77-
importlib.reload('{DATA_VARIABLE_NAME}')
78+
import {DATA_MODULE_NAME}
79+
importlib.reload({DATA_MODULE_NAME})
80+
{DATA_VARIABLE_NAME} = {DATA_MODULE_NAME}.{DATA_VARIABLE_NAME}
81+
7882
"""),
7983
] + nb['cells'][:]
8084

@@ -92,7 +96,7 @@ def initialize_jupyter_import_module_file(dest: str):
9296

9397
dest = f'{dest}{DATA_MODULE_NAME}.py'
9498
with open(dest, 'w') as f:
95-
f.write(_initial_data_file_code(dest))
99+
f.write(_initial_data_file_code())
96100

97101

98102
def update_module_file_with_data_file(dest: str, data_file: str, replace: bool, timestamp: float = 0.0) -> Optional[str]:

0 commit comments

Comments
 (0)