Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ibm
GitHub Repository: ibm/watson-machine-learning-samples
Path: blob/master/cpd5.1/notebooks/python_sdk/instance-management/Space management.ipynb
6405 views
Kernel: Python 3 (ipykernel)

Space management

This notebook contains steps and code to demonstrate how to manage spaces in context of Watson Machine Learning service. It facilitates ibm-watsonx-ai library available in PyPI repository. It introduces commands for creating, updating & deleting spaces, getting list and detailed information about them.

Some familiarity with Python is helpful. This notebook uses Python 3.11.

Learning goals

The learning goals of this notebook are:

  • Create new space

  • List existing spaces

  • Get spaces details

  • Set default space

  • Update exisitng space

  • Delete space

Contents

This notebook contains the following parts:

  1. Set up the environment

  2. Create new space

  3. List all existing spaces

  4. Get details about space

  5. Set default space

  6. Update space metadata

  7. Delete existing space

  8. Summary and next steps

1. Set up the environment

Before you use the sample code in this notebook, you must perform the following setup tasks:

  • Contact with your Cloud Pak for Data administrator and ask them for your account credentials

Install and import the ibm-watsonx-ai and dependecies

Note: ibm-watsonx-ai documentation can be found here.

!pip install -U ibm-watsonx-ai | tail -n 1

Connection to WML

Authenticate the Watson Machine Learning service on IBM Cloud Pak for Data. You need to provide platform url, your username and api_key.

username = 'PASTE YOUR USERNAME HERE' api_key = 'PASTE YOUR API_KEY HERE' url = 'PASTE THE PLATFORM URL HERE'
from ibm_watsonx_ai import Credentials credentials = Credentials( username=username, api_key=api_key, url=url, instance_id="openshift", version="5.1" )

Alternatively you can use username and password to authenticate WML services.

credentials = Credentials( username=***, password=***, url=***, instance_id="openshift", version="5.1" )
from ibm_watsonx_ai import APIClient client = APIClient(credentials)

2. Create new space

First of all, you need to create a space that will be used for your work. If you do not have space already created, you can use {PLATFORM_URL}/ml-runtime/spaces?context=icp4data to create one.

  • Click New Deployment Space

  • Create an empty space

  • Go to space Settings tab

  • Copy space_id and paste it below

Tip: You can also use SDK to prepare the space for your work. More information can be found here.

Action: Assign space ID below

space_metadata = { 'name': 'PUT_YOUR_SPACE_NAME_HERE', 'description': 'PUT_YOUR_DESCRIPTION_HERE', }

Next you can create space by following cell execution.

space_details = client.spaces.store(space_metadata) print(space_details)
Space has been created. However some background setup activities might still be on-going. Check for 'status' field in the response. It has to show 'active' before space can be used. If it's not 'active', you can monitor the state with a call to spaces.get_details(space_id). Alternatively, use background_mode=False when calling client.spaces.store(). {'entity': {'compute': [{'crn': 'crn:v1:cpd:private:pm-20:private:a/cpduser:99999999-9999-9999-9999-999999999999::', 'guid': '99999999-9999-9999-9999-999999999999', 'name': 'Watson Machine Learning', 'type': 'machine_learning'}], 'description': 'sample description', 'members': [{'id': '1000330999', 'role': 'admin', 'state': 'active', 'type': 'user'}], 'name': 'Sample name', 'scope': {'bss_account_id': 'cpdaccount'}, 'stage': {'production': False}, 'status': {'state': 'preparing'}}, 'metadata': {'created_at': '2024-04-26T12:45:34.067Z', 'creator_id': '1000330999', 'id': 'e4c74d4d-a34c-4d43-b674-535246b54b8e', 'url': '/v2/spaces/e4c74d4d-a34c-4d43-b674-535246b54b8e'}}

You can get space it by executing following cell.

space_id = client.spaces.get_id(space_details) print(space_id)
e4c74d4d-a34c-4d43-b674-535246b54b8e

Tip In order to check if the space creation is completed succesfully change next cell format to code and execute it. It should return 'active'.

client.spaces.get_details(space_id)['entity']['status']['state']
'active'

Action: If you didn't create new space in this notebook by ibm_watsonx_ai, please assign space ID below and change cell format to code.

space_id = 'PASTE YOUR SPACE ID HERE'

3. List all existing spaces

You can use list method to print all existing spaces.

client.spaces.list()

4. Get details about space

You can use get_details method to print details about given space. You need to provide space_id of desired space.

client.spaces.get_details(space_id)
{'entity': {'compute': [{'crn': 'crn:v1:cpd:private:pm-20:private:a/cpduser:99999999-9999-9999-9999-999999999999::', 'guid': '99999999-9999-9999-9999-999999999999', 'name': 'Watson Machine Learning', 'type': 'machine_learning'}], 'description': 'sample description', 'name': 'Sample name', 'scope': {'bss_account_id': 'cpdaccount'}, 'stage': {'production': False}, 'status': {'state': 'active'}}, 'metadata': {'created_at': '2024-04-26T12:45:34.067Z', 'creator_id': '1000330999', 'id': 'e4c74d4d-a34c-4d43-b674-535246b54b8e', 'updated_at': '2024-04-26T12:45:40.942Z', 'url': '/v2/spaces/e4c74d4d-a34c-4d43-b674-535246b54b8e'}}

5. Set default space

To be able to interact with all resources available in Watson Machine Learning, you need to set space which you will be using.

client.set.default_space(space_id)
'SUCCESS'

6. Update space metadata

You can update your space by reassigning space metadata and executing: client.spaces.update(space_id, space_metadata).

updated_space_metadata = { client.spaces.ConfigurationMetaNames.NAME: "Updated space name" } client.spaces.update(space_id, updated_space_metadata)
changes in update: {'name': 'Updated space name'} patch payload: [{'op': 'replace', 'path': '/name', 'value': 'Updated space name'}]
{'entity': {'compute': [{'crn': 'crn:v1:cpd:private:pm-20:private:a/cpduser:99999999-9999-9999-9999-999999999999::', 'guid': '99999999-9999-9999-9999-999999999999', 'name': 'Watson Machine Learning', 'type': 'machine_learning'}], 'description': 'sample description', 'members': [{'id': '1000330999', 'role': 'admin', 'state': 'active', 'type': 'user'}], 'name': 'Updated space name', 'scope': {'bss_account_id': 'cpdaccount'}, 'stage': {'production': False}, 'status': {'state': 'active'}}, 'metadata': {'created_at': '2024-04-26T12:45:34.067Z', 'creator_id': '1000330999', 'id': 'e4c74d4d-a34c-4d43-b674-535246b54b8e', 'updated_at': '2024-04-26T12:46:28.624Z', 'url': '/v2/spaces/e4c74d4d-a34c-4d43-b674-535246b54b8e'}}

7. Delete existing space

You can use the command below to delete existing space. You need to provide space_id of the space you want to delete.

client.spaces.delete(space_id)
DELETED
'SUCCESS'

8. Summary and next steps

You successfully completed this notebook! You learned how to use ibm-watsonx-ai client for Watson Machine Learning instance space management and clean up.

Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.

Authors

Szymon Kucharczyk, Software Engineer at IBM.

Daniel Ryszka, Software Engineer at IBM.

Copyright © 2020-2025 IBM. This notebook and its source code are released under the terms of the MIT License.