ibm-watson-studio-lib for Python

The ibm-watson-studio-lib library for Python provides access to assets in Watson Studio. It can be used in notebooks in a project or notebooks that have been promoted to a deployment space. ibm-watson-studio-lib provides support for working with data assets and connections, as well as browsing functionality for all other asset types.

There are two kinds of data assets:

Note:

Setting up the ibm-watson-studio-lib library

When ibm-watson-studio-lib is used in a notebook, the project or space in which it runs is determined automatically.

Use the following import statement to set up ibm-watson-studio-lib:

from ibm_watson_studio_lib import access_project_or_space
credentials_dic={"project_id":'<ProjectId>', "token":'<ProjectToken>'}
wslib = access_project_or_space(params=credentials_dic)

Helper functions

You can get information about the supported functions in the ibm-watson-studio-lib library programmatically by using help(wslib), or for an individual function by using help(wslib.<function_name>, for example help(wslib.get_connection).

You can use the helper function wslib.show(...) for formatted printing of Python dictionaries and lists of dictionaries, which are the common result output type of the ibm-watson-studio-lib functions.

The ibm-watson-studio-lib functions

The ibm-watson-studio-lib library exposes a set of functions that are grouped in the following way:

Get project or space information

While developing code, you might not know the exact names of data assets or connections. The following functions provide lists of assets, from which you can pick the relevant ones. In all examples, you can use wslib.show(assets) to pretty-print the list. The index of each item is printed in front of the item.

Fetch data

You can use the following functions to fetch data from a stored data asset (a file) in your project or space.

Save data

You can use the following function to save data in memory to a file associated with your project or space. This function does multiple things. Firstly, it puts the data into a file in the project or space storage and then it adds this data as a data asset to your project or space so you can see the data that you saved as a file in the data assets list in your project or space.

Get connection information

You can use the following function to access the connection metadata of a given connection.

Get connected data information

You can use the following function to access the metadata of a connected data asset.

Access asset by ID instead of name

You should preferably always access data assets and connections by a unique name. Asset names are not necessarily always unique and the ibm-watson-studio-lib functions will raise an exception when a name is ambiguous. You can rename data assets in the UI to resolve the conflict.

Accessing assets by a unique ID is possible but is discouraged as IDs are valid only in the current project or space and will break code when transferred to a different project or space. This can happen for example, when projects are exported and re-imported, or when notebooks or assets are promoted from projects to spaces. You can get the ID of a connection, connected or stored data asset by using the corresponding list function, for example list_connections().

The entry point wslib.by_id provides the following functions:

Work with the mounted project storage

In Cloud Pak for Data the project or space storage is mounted in the local file system of your runtime. You can retrieve information about the mounted project storage using the entry point wslib.mount.

The entry point wslib.mount provides the following functions:

Access project storage directly

You can fetch data from project storage and store data in project storage without synchronizing the project assets using the entry point wslib.storage.

In Cloud Pak for Data, the project or space storage is mounted in the notebook runtime and you will typically use the entry point wslib.mount and file system operations to access the storage.

The entry point wslib.storage provides the following functions:

Browse project assets

The entry point wslib.assets provides generic, read-only access to assets of any type. For selected asset types, there are dedicated functions that provide additional data. To get help on the available functions, use help(wslib.assets.API).

The following naming conventions apply:

To pretty-print a dictionary or list of dictionaries, use wslib.show().

The functions expect either the name of an asset, or an item from a list as the parameter. By default, the functions return only a subset of the available asset properties. By setting the parameter raw=True, you can get the full set of asset properties.

The entry point wslib.assets provides the following functions:

Learn more

For examples of how to use some of the functions provided by the library in a notebook, see Working with ibm-watson-studio-lib.

Parent topic: Using ibm-watson-studio-lib