def add_file_path_as_data_asset_cpd25(file_path: str, asset_name: str = None) -> None:
    """Add a data file to the Watson Studio project.
    Applies to CPDv2.5. Works for any file. Allows the file to be viewed and downloaded from Watson Studio UI.
    Needs to be called after the file has been saved regularly in the file system.
    Typically, that would be in `/project_data/data_asset/`.
    Ensures the file is visible in the Data Assets of the Watson Studio UI.

    Args:
        file_path (str): full file path, including the file name and extension
        asset_name (str): name of data asset. Default is None. If None, the asset_name will be extracted from the file_path.

    Usage::

        # Write some file as an example:
        file_path = '/project_data/data_asset/myfile.csv'
        with open(file_path, 'w+') as f:
             f.write("Hello World")
        # Add file as a data asset:
        add_file_as_data_asset_cpd25(file_path)

    Beware that the same data now exists in 2 different places:
        * In the Cloud Object Storage (COS)
        * As a file in `/project_data/data_asset/`
    Changing any of the 2 independently can cause inconsistencies.
    """
    if asset_name is None:
        asset_name = os.path.basename(file_path)
    with open(file_path, 'rb') as f:
        from project_lib import Project
        project = Project.access()
        project.save_data(file_name=asset_name, data=f, overwrite=True)
コード例 #2
0
def add_file_path_as_data_asset_wsc(file_path: str, asset_name: str = None, project=None) -> None:
    """Add a data file to the Watson Studio project.
    Applies to WS Cloud and CPDv2.5. Works for any file. Allows the file to be viewed and downloaded from Watson Studio UI.
    Needs to be called after the file has been saved regularly in the file system.
    Typically, that would be in:

        * CPDv2.5: `/project_data/data_asset/`
        * WS Cloud: `/home/dsxuser/work/`, or `os.environ['PWD']`, or `./`, or no path

    Ensures the file is visible in the Data Assets of the Watson Studio UI.

    Args:
        project (project_lib.Project): required for WS Cloud
        file_path (str): full file path, including the file name and extension
        asset_name (str): name of data asset. Default is None. If None, the asset_name will be extracted from the file_path.

    Usage::

        # Write some file as an example:
        file_path = '/project_data/data_asset/myfile.csv'
        with open(file_path, 'w+') as f:
             f.write("Hello World")
        # Add file as a data asset:
        add_file_as_data_asset_cpd25(file_path)
    """
    if project is None:
        from project_lib import Project
        project = Project.access()
    if asset_name is None:
        asset_name = os.path.basename(file_path)
    with open(file_path, 'rb') as f:
        project.save_data(file_name=asset_name, data=f, overwrite=True)
def add_file_as_data_asset_cpd25(file_name: str) -> None:
    """Adds a file located in `/project_data/data_asset/` as a Data Asset to the Watson Studio project.
    So that it appears in the UI and can be exported.
    """
    file_path = os.path.join('/project_data/data_asset/', file_name)
    with open(file_path, 'rb') as f:
        from project_lib import Project
        project = Project.access()
        project.save_data(file_name=file_name, data=f, overwrite=True)
コード例 #4
0
 def write_do_model_to_file(self, do_model_name: str, response: requests.Response) -> str:
     import os
     export_file_name = DOModelExporter._get_export_file_name(do_model_name)
     file_path = os.path.join(self.export_directory, export_file_name)
     with open(file_path, 'wb') as f:
         f.write(response.content)
     # If CPD2.5 then also add as asset:
     if self.export_directory == '/project_data/data_asset/':
         print("Adding export file to Data Assets of this project.")
         with open(file_path, 'rb') as f:
             from project_lib import Project
             project = Project.access()
             project.save_data(file_name=export_file_name, data=f, overwrite=True)
     return file_path
def write_data_asset_as_file_cpd25(asset_name: str, path: str = '/project_data/data_asset/') -> str:
    """Writes a named data asset to file.
    Assumes a data asset with `asset_name` exists.
    Makes the file accessible for things like:
    * Load from disk
    * Pip install
    * Module import

    Args:
        asset_name (str): name of the asset
        path (str, Optional): Default is '/project_data/data_asset/'. Use path='' for current directory.
    """
    from project_lib import Project
    project = Project.access()
    file_path = os.path.join(path, asset_name)
    with open(file_path, "wb") as f:
        f.write(project.get_file(asset_name).read())
    return file_path
コード例 #6
0
    def add_data_file_to_project_s(file_path: str,
                                   file_name: Optional[str] = None) -> None:
        """Add a data file to the Watson Studio project.
        Applies to CP4Dv2.5.
        Needs to be called after the file has been saved regularly in the file system in `/project_data/data_asset/`.
        Ensures the file is visible in the Data Assets of the Watson Studio UI.

        Args:
            file_path (str): full file path, including the file name and extension
            file_name (str): name of data asset. Default is None. If None, the file-name will be extracted from the file_path.
        """
        # Add to Project
        if file_name is None:
            file_name = os.path.basename(file_path)
        with open(file_path, 'rb') as f:
            from project_lib import Project
            project = Project.access()
            project.save_data(file_name=file_name, data=f, overwrite=True)
コード例 #7
0
def write_data_asset_as_file_wsc(asset_name: str, path: str = '/home/dsxuser/work/', project=None) -> str:
    """Writes a named data asset to file (for WS Cloud).
    Assumes a data asset with `asset_name` exists.
    Makes the file accessible for things like:

        * Load from disk
        * Pip install
        * Module import

    Args:
        asset_name (str): name of the asset
        path (str, Optional): Default (for WS Cloud) is '/home/dsxuser/work/'. Use path='' for current directory.
        project (project_lib.Project): required for WS Cloud. For CPD, leave as None.
    """
    if project is None:
        from project_lib import Project
        project = Project.access()
    file_path = os.path.join(path, asset_name)
    with open(file_path, "wb") as f:
        f.write(project.get_file(asset_name).read())
    return file_path
コード例 #8
0
ファイル: utils.py プロジェクト: IBMStreams/pypi.streamsx.wml
def get_project_space(credentials):
    """Returns the notebooks project space GUID.
       
    Argument:
    credentials -- the credentials to be used to connect to WML
      
    Call it only in the notebook where the topology is created, not at Streams runtime.
    Models and deployments are placed in projects space if no other space is given at 
    their creation time.
    The space GUID is needed to instantiate a WMLOnlineScoring object.
    """
    from project_lib import Project
        
    wml_client = APIClient(copy.copy(credentials))
    spaces = wml_client.spaces.get_details()["resources"]
    project = Project.access()
    project_guid = project.get_metadata()["metadata"]["guid"]
    # get the space associated with the project
    project_space=None
    for space in spaces:
        for tag in space["entity"]["tags"]:
            if tag["value"]=="dsx-project."+project_guid:
                project_space = space["metadata"]["id"]
    return project_space