Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ibm
GitHub Repository: ibm/watson-machine-learning-samples
Path: blob/master/cpd4.8/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-watson-machine-learning 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.10.

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 Pack for Data administrator and ask him for your account credentials

Connection to WML

Authenticate the Watson Machine Learning service on IBM Cloud Pack 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'
wml_credentials = { "username": username, "apikey": api_key, "url": url, "instance_id": 'openshift', "version": '4.8' }

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

wml_credentials = { "username": ***, "password": ***, "url": ***, "instance_id": 'openshift', "version": '4.8' }

Install and import the ibm-watson-machine-learning package

Note: ibm-watson-machine-learning documentation can be found here.

!pip install -U ibm-watson-machine-learning
from ibm_watson_machine_learning import APIClient client = APIClient(wml_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 its not 'active', you can monitor the state with a call to 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': 'space for wml samples tests', 'members': [{'id': '1000330999', 'role': 'admin', 'state': 'active', 'type': 'user'}], 'name': 'wml_samples', 'scope': {'bss_account_id': 'cpdaccount'}, 'status': {'state': 'preparing'}}, 'metadata': {'created_at': '2020-12-08T13:37:26.496Z', 'creator_id': '1000330999', 'id': '881fca23-421e-4cd9-bc6d-64c69e52c9d7', 'url': '/v2/spaces/881fca23-421e-4cd9-bc6d-64c69e52c9d7'}}

You can get space it by executing following cell.

space_id = client.spaces.get_id(space_details) print(space_id)
881fca23-421e-4cd9-bc6d-64c69e52c9d7

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_watson_machine_learning, 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': 'space for wml samples tests', 'name': 'wml_samples', 'scope': {'bss_account_id': 'cpdaccount'}, 'status': {'state': 'active'}}, 'metadata': {'created_at': '2020-12-08T13:37:26.496Z', 'creator_id': '1000330999', 'id': '881fca23-421e-4cd9-bc6d-64c69e52c9d7', 'updated_at': '2020-12-08T13:37:33.441Z', 'url': '/v2/spaces/881fca23-421e-4cd9-bc6d-64c69e52c9d7'}}

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': 'space for wml samples tests', 'members': [{'id': '1000330999', 'role': 'admin', 'state': 'active', 'type': 'user'}], 'name': 'Updated space name', 'scope': {'bss_account_id': 'cpdaccount'}, 'status': {'state': 'active'}}, 'metadata': {'created_at': '2020-12-08T13:37:26.496Z', 'creator_id': '1000330999', 'id': '881fca23-421e-4cd9-bc6d-64c69e52c9d7', 'updated_at': '2020-12-08T13:37:54.746Z', 'url': '/v2/spaces/881fca23-421e-4cd9-bc6d-64c69e52c9d7'}}

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-watson-machine-learning 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.