Migrating from project-lib for Python to ibm-watson-studio-lib
Last updated: Jul 04, 2023
Migrating from project-lib for Python to ibm-watson-studio-lib
The ibm-watson-studio-lib library is the successor of the project-lib library. Although you can still continue using project-lib API in your notebooks, you should think about migrating existing notebooks
to use the ibm-watson-studio-lib library.
Advantages of using ibm-watson-studio-lib include:
The asset browsing API provides read-only access to all types of assets, not only those explicitly supported by the library.
ibm-watson-studio-lib uses a constistent API naming convention that structures available functions according to their area of application.
The following sections describe the changes you need to make in existing Python notebooks to start using the ibm-watson-studio-lib library.
Set up the library
Copy link to section
You need to make the following changes in existing notebooks to start using ibm-watson-studio-lib:
In code using project-lib change:
from project_lib import Project
project = Project("<ProjectId>","<ProjectToken>")
Copy to clipboardCopied to clipboard
To the following using ibm-watson-studio-lib:
from ibm_watson_studio_lib import access_project_or_space
wslib = access_project_or_space({"token":"<ProjectToken>"})
Copy to clipboardCopied to clipboard
Set up the library in Spark environments
Copy link to section
You need to make the following changes in existing notebooks to start using ibm-watson-studio-lib in Spark environments.
In code using project-lib change:
from project_lib import Project
project = Project(sc,"<ProjectId>","<ProjectToken>")
Copy to clipboardCopied to clipboard
To the following using ibm-watson-studio-lib:
from ibm_watson_studio_lib import access_project_or_space
wslib = access_project_or_space({"token":"<ProjectToken>"})
wslib.spark.provide_spark_context(sc)
Copy to clipboardCopied to clipboard
Library usage
Copy link to section
The following sections describe the code changes that you need to make in your notebooks when migrating functions in project-lib to the corresponding functions in ibm-watson-studio-lib.
In ibm-watson-studio-lib, you can retrieve any metadata about the project, for example the name of a project or its description, via the entrypoint wslib.here.
In code using project-lib change:
name = project.get_name()
desc = project.get_description()
Copy to clipboardCopied to clipboard
To the following using ibm-watson-studio-lib:
name = wslib.here.get_name()
desc = wslib.here.get_description()
Copy to clipboardCopied to clipboard
Get metadata
Copy link to section
There is no replacement for get_matadata in project-lib:
project.get_metadata()
Copy to clipboardCopied to clipboard
The function wslib.here in ibm-watson-studio-lib exposes parts of this information. To see what project metadata information is available, use:
help(wslib.here.API)
Copy to clipboardCopied to clipboard
For example:
wslib.here.get_name(): Returns the project name
wslib.here.get_description(): Returns the proejct description
wslib.here.get_ID(): Returns the project ID
wslib.here.get_storage(): Returns the storage metadata
Get storage metadata
Copy link to section
In code using project-lib change:
project.get_storage_metadata()
Copy to clipboardCopied to clipboard
To the following using ibm-watson-studio-lib:
wslib.here.get_storage()
Copy to clipboardCopied to clipboard
Fetch data
Copy link to section
To access data in a file, you need to change the following functions.
In code using project-lib change:
buffer = project.get_file("MyAssetName.csv")
# or, without direct storage access:
buffer = project.get_file("MyAssetName.csv", direct_storage=False)
# or:
buffer = project.get_file("MyAssetName.csv", direct_os_retrieval=False)
Copy to clipboardCopied to clipboard
To the following using ibm-watson-studio-lib:
buffer = wslib.load_data("MyAssetName.csv")
Copy to clipboardCopied to clipboard
Additionally, ibm-watson-studio-lib offers a function to download a data asset and store it in the local file system:
info = wslib.download_file("MyAssetName.csv", "MyLocalFile.csv")
Copy to clipboardCopied to clipboard
Save data
Copy link to section
To save data to a file, you need to change the following functions.
In code using project-lib change (and for all variations of direct_store=False and set_project_asset=True):
In project-lib, it is not possible to access files (stored data assets) by ID. You can only do this by name. The ibm-watson-studio-lib library supports accessing files by ID. See Using ibm-watson-studio-lib.
Fetch assets by asset type
Copy link to section
When you retrieve the list of all project assets, you can pass the optional parameter asset_type to the function get_assets which allows you to filter assets by type. The accepted values for this parameter in project-lib are data_asset, connection and asset.
In code using project-lib change:
project.get_assets()
# Or, for a supported asset type:
project.get_assets("<asset_type>")
# Or:
project.get_assets(asset_type="<asset_type>")
Copy to clipboardCopied to clipboard
To the following using ibm-watson-studio-lib:
assets = wslib.assets.list_assets("asset")
wslib.show(assets)
# Or, for a specific asset type:
assets = wslib.assets.list_assets("<asset_type>")
# Example, list all notebooks:
notebook_assets = wslib.assets.list_assets("notebook")
wslib.show(notebook_assets)
To work with Spark, you need to change the functions that enable Spark support and retrieving the URL to a file.
Set up Spark support
Copy link to section
To set up Spark support:
In code using project-lib change:
# Provide SparkContext during setupfrom project_lib import Project
project = Project(sc,"<ProjectId>","<ProjectToken>")
Copy to clipboardCopied to clipboard
To the following using ibm-watson-studio-lib:
from ibm_watson_studio_lib import access_project_or_space
wslib = access_project_or_space({'token':'<ProjectToken>'}
# provide SparkContext in a subsequent step
wslib.spark.provide_spark_context(sc)
Copy to clipboardCopied to clipboard
Retrieve URL to access a file from Spark
Copy link to section
To retrieve a URL to access a file referenced by an asset from Spark via Hadoop:
In code using project-lib change:
url = project.get_file_url("MyAssetName.csv")
# or
url = project.get_file_url("MyAssetName.csv", direct_storage=False)
# or
url = project.get_file_url("MyAssetName.csv", direct_os_retrieval=False)
Copy to clipboardCopied to clipboard
To the following using ibm-watson-studio-lib:
url = wslib.spark.get_data_url("MyAssetName.csv")
Copy to clipboardCopied to clipboard
Get file URL for usage with Spark
Copy link to section
Retrieve a URL to access a file referenced by an asset from Spark via Hadoop.
In code using project-lib change:
project.get_file_url("MyFileName.csv", direct_storage=True)
# or
project.get_file_url("MyFileName.csv", direct_os_retrieval=True)
# Save and do not create an asset in a project
project.save_data("NewFileName.csv", data, direct_storage=True)
# Or:
project.save_data("NewFileName.csv", data, set_project_asset=False)
Copy to clipboardCopied to clipboard
To the following using ibm-watson-studio-lib:
wslib.storage.store_data("NewFileName.csv", data)
Copy to clipboardCopied to clipboard
In code using project-lib change:
# Save (and overwrite if file exists) and do not create an asset in the project
project.save_data("MyFileName.csv", data, direct_storage=True, overwrite=True)
# Or:
project.save_data("MyFileName.csv", data, set_project_asset=False, overwrite=True)
About cookies on this siteOur websites require some cookies to function properly (required). In addition, other cookies may be used with your consent to analyze site usage, improve the user experience and for advertising.For more information, please review your cookie preferences options. By visiting our website, you agree to our processing of information as described in IBM’sprivacy statement. To provide a smooth navigation, your cookie preferences will be shared across the IBM web domains listed here.