8. Adding Kitems into context

8. Adding Kitems into context#

8.1. Setting up#

Before you run this tutorial: make sure to have access to a DSMS-instance of your interest, alongwith with installation of this package and have establised access to the DSMS through DSMS-SDK (refer to Connecting to DSMS)

Import the needed classes and functions.

[1]:
from dsms import DSMS, KItem

Now source the environmental variables from an .env file and start the DSMS-session.

[2]:
dsms = DSMS(env=".env")

8.2 Putting KItems into contexts#

KItems can not only be linked, but also put into a context in order to group and cluster them. This can especially become handy, when a set of KItems are related e.g. to a specific process, catalog, etc.

We will quickly create a KItem of type dataset and we will put it into context with a dataset catalog.

[3]:
dataset = KItem(
    name="Test dataset",
    ktype_id=dsms.ktypes.Dataset,
    custom_properties={
        "Tensile Strength [MPa]": 180,
        "Material": "DXD100"
        }
    )

dsms.add(dataset)
dsms.commit()

print(dataset.url)
/app/dsms/knowledge/kitem.py:406: UserWarning: A flat dictionary was provided for custom properties.
                    Will be transformed into `KItemCustomPropertiesModel`.
  warnings.warn(
https://bue.materials-data.space/knowledge/dataset/testdataset-edc21685
/app/dsms/knowledge/kitem.py:692: UserWarning: No webform was defined for entry `Tensile Strength [MPa]`. Cannot check if value is of correct type.
  warnings.warn(
/app/dsms/knowledge/kitem.py:692: UserWarning: No webform was defined for entry `Material`. Cannot check if value is of correct type.
  warnings.warn(
[4]:
catalog = KItem(
    name="Test catalog",
    ktype_id=dsms.ktypes.DatasetCatalog,
    custom_properties={"Project": "Mechanical testing campaign 1"},
    contexts=[dataset]
)

dsms.add(catalog)
dsms.commit()

print(catalog.url)
/usr/local/lib/python3.11/site-packages/pydantic/main.py:463: UserWarning: Pydantic serializer warnings:
  PydanticSerializationUnexpectedValue(Expected `str` - serialized value may not be as expected [input_value=False, input_type=bool])
  return self.__pydantic_serializer__.to_python(
https://bue.materials-data.space/knowledge/dataset-catalog/testcatalog-4bcfabbb
/app/dsms/knowledge/kitem.py:692: UserWarning: No webform was defined for entry `Project`. Cannot check if value is of correct type.
  warnings.warn(

Now we can inspect the catalog and see which datasets are in context:

[5]:
catalog
[5]:
kitem:
  id: 4bcfabbb-dabf-4d4f-a964-2d56db7cdf54
  name: Test catalog
  ktype_id: dataset-catalog
  slug: testcatalog-4bcfabbb
  annotations: []
  attachments: []
  linked_kitems: []
  affiliations: []
  authors:
  - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
  avatar_exists: false
  contacts: []
  created_at: 2025-08-13 13:03:06.234294
  updated_at: 2025-08-13 13:03:06.234294
  external_links: []
  apps: []
  user_groups: []
  custom_properties:
    content:
      sections:
      - id: id1755090185982jzxbpc
        name: Misc
        entries:
        - id: id17550901859828l3uet
          type: Text
          label: Project
          value: Mechanical testing campaign 1
          measurementUnit: null
          relationMapping: null
          required: false
  rdf_exists: false
  contexts:
  - id: edc21685-2d25-4d7a-bd2e-7c757371d3f8
    name: Test dataset
    ktype_id: dataset
    slug: testdataset-edc21685