29
29
from . import ipsutil , messages
30
30
from .cca_es_spec import initialize_event_service
31
31
from .ips_es_spec import eventManager
32
- from .jupyter import add_data_file_to_notebook , initialize_jupyter_notebook , remove_data_file_from_notebook , remove_last_data_file_from_notebook
32
+ from .jupyter import add_data_file_to_notebook , initialize_jupyter_notebook , remove_last_data_file_from_notebook
33
33
from .taskManager import TaskInit
34
34
35
35
RunningTask = namedtuple ('RunningTask' , ['process' , 'start_time' , 'timeout' , 'nproc' , 'cores_allocated' , 'command' , 'binary' , 'args' ])
@@ -1903,7 +1903,7 @@ def initialize_jupyter_notebook(
1903
1903
self ,
1904
1904
dest_notebook_name : str ,
1905
1905
source_notebook_path : str ,
1906
- variable_name : str = 'IPS_STATE_FILES ' ,
1906
+ variable_name : str = 'ANALYSIS_FILES ' ,
1907
1907
cell_to_modify : int = 0 ,
1908
1908
) -> None :
1909
1909
"""Loads a notebook from source_notebook_path, adds a cell to load the data, and then saves it to source_notebook_path. Will also try to register the notebook with the IPS Portal, if available.
@@ -1913,7 +1913,7 @@ def initialize_jupyter_notebook(
1913
1913
Params:
1914
1914
- dest_notebook_name: name of the JupyterNotebook you want to write (do not include file paths).
1915
1915
- source_notebook_path: location you want to load the source notebook from
1916
- - variable_name: name of the variable you want to load files from (default: "IPS_STATE_FILES ")
1916
+ - variable_name: name of the variable you want to load files from (default: "ANALYSIS_FILES ")
1917
1917
- cell_to_modify: which cell in the JupyterNotebook you want to add the data call to (0-indexed).
1918
1918
(This will not overwrite any cells, just appends.)
1919
1919
By default, the data listing will happen in the FIRST cell.
@@ -1942,7 +1942,7 @@ def initialize_jupyter_notebook(
1942
1942
self .publish ('_IPS_MONITOR' , 'PORTAL_REGISTER_NOTEBOOK' , event_data )
1943
1943
self ._send_monitor_event ('IPS_PORTAL_REGISTER_NOTEBOOK' , f'URL = { url } ' )
1944
1944
1945
- def add_data_file_to_notebook (self , state_file_path : str , timestamp : float , notebook_name : str , index : Optional [int ] = None ):
1945
+ def add_data_file_to_notebook (self , state_file_path : str , timestamp : float , notebook_name : str , replace : bool = False , index : Optional [int ] = None ):
1946
1946
"""Add data file to JupyterHub directory, and reference it in the notebook.
1947
1947
1948
1948
This function assumes that a notebook has already been created with intialize_jupyter_notebook. Using this function does not call the IPS Portal.
@@ -1951,83 +1951,30 @@ def add_data_file_to_notebook(self, state_file_path: str, timestamp: float, note
1951
1951
- state_file_path: location of the current state file we want to copy to the Jupyter directory
1952
1952
- timestamp: label to assign to the data (currently must be a floating point value)
1953
1953
- notebook_name: name of notebook which will be modified. Note that this path is relative to the JupyterHub directory.
1954
+ - replace: If True, replace the last data file added with the new data file. If False, simply append the new data file.
1954
1955
- index: optional index of the IPS notebook cell. If not provided, the IPS Framework will attempt to automatically find the cell it created,
1955
1956
which should work for every usecase where you don't anticipate modifying the notebook until after the run is complete.
1956
1957
"""
1957
-
1958
1958
if not self ._jupyterhub_dir :
1959
1959
if not self ._init_jupyter ():
1960
1960
# TODO generic exception
1961
1961
raise Exception ('Unable to initialize base JupyterHub dir' )
1962
1962
1963
- file_parts = state_file_path .split ('.' )
1964
- if len (file_parts ) > 2 : # name of the file could just be a floating point value with no extension
1965
- extension = f'.{ file_parts [- 1 ]} '
1966
- else :
1967
- extension = ''
1968
-
1969
- state_file_name = f'{ timestamp } { extension } '
1970
- jupyter_data_dir = os .path .join (self ._jupyterhub_dir , 'data' , state_file_name )
1963
+ data_file_name = f'{ timestamp } _{ os .path .basename (state_file_path )} '
1964
+ jupyter_data_dir = os .path .join (self ._jupyterhub_dir , 'data' , data_file_name )
1971
1965
# this may raise an OSError, it is the responsibility of the caller to handle it.
1972
1966
shutil .copyfile (state_file_path , jupyter_data_dir )
1973
1967
1974
- # TODO - maybe add flag which allows us to replace old state files
1975
- add_data_file_to_notebook (f'{ self ._jupyterhub_dir } { notebook_name } ' , state_file_name , index )
1976
-
1977
- def remove_data_file_from_notebook (self , state_file_path : str , timestamp : float , notebook_name : str , index : Optional [int ] = None ):
1978
- """Remove data file from JupyterHub data directory and from being referenced in the notebook.
1979
-
1980
- This function assumes that a notebook has already been created with intialize_jupyter_notebook. Using this function does not call the IPS Portal.
1981
-
1982
- Params:
1983
- - state_file_path: location of the current state file we want to copy to the Jupyter directory
1984
- - timestamp: label to assign to the data (currently must be a floating point value)
1985
- - notebook_name: name of notebook which will be modified. Note that this path is relative to the JupyterHub directory.
1986
- - index: optional index of the IPS notebook cell. If not provided, the IPS Framework will attempt to automatically find the cell it created,
1987
- which should work for every usecase where you don't anticipate modifying the notebook until after the run is complete.
1988
- """
1989
-
1990
- if not self ._jupyterhub_dir :
1991
- if not self ._init_jupyter ():
1992
- # TODO generic exception
1993
- raise Exception ('Unable to initialize base JupyterHub dir' )
1968
+ if replace :
1969
+ # first try to remove the reference from the Jupyter Notebook
1970
+ filename_to_remove = remove_last_data_file_from_notebook (f'{ self ._jupyterhub_dir } { notebook_name } ' , index )
1971
+ if filename_to_remove is not None :
1972
+ # now remove the state file from the filesyste,
1973
+ file_to_remove = os .path .join (self ._jupyterhub_dir , 'data' , filename_to_remove )
1974
+ shutil .rmtree (file_to_remove , ignore_errors = True )
1994
1975
1995
- file_parts = state_file_path .split ('.' )
1996
- if len (file_parts ) > 2 : # name of the file could just be a floating point value with no extension
1997
- extension = f'.{ file_parts [- 1 ]} '
1998
- else :
1999
- extension = ''
2000
-
2001
- state_file_name = f'{ timestamp } { extension } '
2002
- jupyter_data_dir = os .path .join (self ._jupyterhub_dir , 'data' , state_file_name )
2003
-
2004
- # if this errors out, we can safely ignore them
2005
- shutil .rmtree (jupyter_data_dir , ignore_errors = True )
2006
-
2007
- # TODO - maybe add flag which allows us to replace old state files
2008
- remove_data_file_from_notebook (f'{ self ._jupyterhub_dir } { notebook_name } ' , state_file_name , index )
2009
-
2010
- def remove_last_data_file_from_notebook (self , notebook_name : str , index : Optional [int ] = None ):
2011
- """Remove the last added data file from a notebook and from the filesystem.
2012
-
2013
- This function assumes that a notebook has already been created with intialize_jupyter_notebook. Using this function does not call the IPS Portal.
2014
-
2015
- Params:
2016
- - notebook_name: name of notebook which will be modified. Note that this path is relative to the JupyterHub directory.
2017
- - index: optional index of the IPS notebook cell. If not provided, the IPS Framework will attempt to automatically find the cell it created,
2018
- which should work for every usecase where you don't anticipate modifying the notebook until after the run is complete.
2019
- """
2020
-
2021
- if not self ._jupyterhub_dir :
2022
- if not self ._init_jupyter ():
2023
- # TODO generic exception
2024
- raise Exception ('Unable to initialize base JupyterHub dir' )
2025
-
2026
- last_state_file = remove_last_data_file_from_notebook (notebook_name , index )
2027
- if last_state_file is None :
2028
- return
2029
- data_file = os .path .join (self ._jupyterhub_dir , 'data' , last_state_file )
2030
- shutil .rmtree (data_file , ignore_errors = True )
1976
+ # add newest data file to notebook
1977
+ add_data_file_to_notebook (f'{ self ._jupyterhub_dir } { notebook_name } ' , data_file_name , index )
2031
1978
2032
1979
def publish (self , topicName : str , eventName : str , eventBody : Any ):
2033
1980
"""
0 commit comments