3. Updating KItems with the SDK

3. Updating KItems with the SDK#

In this tutorial we see how to update existing Kitems.

3.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)

Now let us import the needed classes and functions for this tutorial.

[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")

Now lets get the kitem we created in the 2nd tutorial : Creation of Kitems

[3]:
item = dsms.get_kitems(limit=100)[-1]
[4]:
item
[4]:
kitem:
  name: Specimen123
  id: 8ba20087-210d-482c-a847-6f80c20b7d51
  ktype_id: specimen
  slug: specimen123-8ba20087
  annotations: []
  attachments:
  - name: subgraph.ttl
    content: null
  linked_kitems: []
  affiliations: []
  authors:
  - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
  avatar_exists: false
  contacts: []
  created_at: 2025-01-17 10:44:42.478274
  updated_at: 2025-01-17 10:44:42.478274
  external_links: []
  kitem_apps: []
  user_groups: []
  custom_properties:
    sections:
    - id: ida3c5c42685526
      name: Untitled Section
      entries:
      - id: id175f885f8dda58
        type: Slider
        label: Width
        value: 0.5
        measurement_unit:
          iri: http://qudt.org/vocab/unit/MilliM
          label: Millimetre
          namespace: http://qudt.org/vocab/unit
      - id: idef5a37328789f
        type: Slider
        label: Length
        value:
        - 0.1
        - 0.2
        measurement_unit:
          iri: http://qudt.org/vocab/unit/MilliM
          label: Millimetre
          namespace: http://qudt.org/vocab/unit
      - id: id54decb9641f9a
        type: Number
        label: Radius
        value: 10
        measurement_unit:
          iri: http://qudt.org/vocab/unit/MilliM
          label: Millimetre
          namespace: http://qudt.org/vocab/unit
  rdf_exists: true

3.2. Updating Kitems#

Now, we would like to update the properties of our KItem we created previously.

Depending on the schema of each property (see DSMS KItem Schema), we can simply use the standard list-method as we know them from basic Python (e.g. for the annotations, attachments, external_link, etc).

Other properties which are not list-like can be simply set by attribute-assignment (e.g. name, slug, ktype_id, etc).

[5]:
item.name = "Specimen-123"
item.custom_properties.Width = 1
item.attachments.append("testfile.txt")
item.annotations.append("https://w3id.org/pmd/co/Specimen")
item.external_links.append(
    {"url": "http://specimens.org", "label": "specimen-link"}
)
item.contacts.append({"name": "Specimen preparation", "email": "specimenpreparation@group.mail"})
[6]:
dsms.commit()

We can see now that the local system path of the attachment is changed to a simply file name, which means that the upload was successful. If not so, an error would have been thrown during the commit.

We can see the updates when we print the item:

[7]:
item
[7]:
kitem:
  name: Specimen-123
  id: 8ba20087-210d-482c-a847-6f80c20b7d51
  ktype_id: specimen
  in_backend: true
  slug: specimen123-8ba20087
  annotations:
  - iri: https://w3id.org/pmd/co/Specimen
    label: Specimen
    namespace: https://w3id.org/pmd/co
  attachments:
  - name: subgraph.ttl
    content: null
  - name: testfile.txt
    content: null
  linked_kitems: []
  affiliations: []
  authors:
  - user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
  avatar_exists: false
  contacts:
  - name: Specimen preparation
    email: specimenpreparation@group.mail
    user_id: null
  created_at: 2025-01-17 10:44:42.478274
  updated_at: 2025-01-17 10:51:55.054607
  external_links:
  - label: specimen-link
    url: http://specimens.org
  kitem_apps: []
  user_groups: []
  custom_properties:
    sections:
    - id: ida3c5c42685526
      name: Untitled Section
      entries:
      - id: id175f885f8dda58
        type: Slider
        label: Width
        value: 1
        measurement_unit:
          iri: http://qudt.org/vocab/unit/MilliM
          label: Millimetre
          namespace: http://qudt.org/vocab/unit
      - id: idef5a37328789f
        type: Slider
        label: Length
        value:
        - 0.1
        - 0.2
        measurement_unit:
          iri: http://qudt.org/vocab/unit/MilliM
          label: Millimetre
          namespace: http://qudt.org/vocab/unit
      - id: id54decb9641f9a
        type: Number
        label: Radius
        value: 10
        measurement_unit:
          iri: http://qudt.org/vocab/unit/MilliM
          label: Millimetre
          namespace: http://qudt.org/vocab/unit
  rdf_exists: true

Furthermore we can also download the file we uploaded again:

[11]:
for file in item.attachments:
    download = file.download()

    print("\t\t\t Downloaded file:", file.name)
    print("|------------------------------------Beginning of file------------------------------------|")
    print(download)
    print("|---------------------------------------End of file---------------------------------------|\n\n")
                         Downloaded file: subgraph.ttl
|------------------------------------Beginning of file------------------------------------|
@prefix ns1: <http://qudt.org/schema/qudt/> .
@prefix ns2: <https://w3id.org/steel/ProcessOntology/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<https://bue.materials-data.space/knowledge/specimen/specimen123-8ba20087> a <https://w3id.org/pmd/co/Specimen> ;
    rdfs:label "Specimen-123"^^xsd:string ;
    ns2:hasRadius_Object <https://bue.materials-data.space/knowledge/specimen/emmo#EMMO_32dcd601_47c7_4028_b7fa_5e972ae57f12_specimen123-8ba20087> ;
    ns2:hasTotalLength_Object <https://bue.materials-data.space/knowledge/specimen/max_specimen123-8ba20087>,
        <https://bue.materials-data.space/knowledge/specimen/min_specimen123-8ba20087> ;
    ns2:hasWidth_Object <https://bue.materials-data.space/knowledge/specimen/emmo#EMMO_e4de48b1_dabb_4490_ac2b_040f926c64f0_specimen123-8ba20087> .

<https://bue.materials-data.space/knowledge/specimen/emmo#EMMO_32dcd601_47c7_4028_b7fa_5e972ae57f12_specimen123-8ba20087> a <https://w3id.org/emmo#EMMO_32dcd601_47c7_4028_b7fa_5e972ae57f12> ;
    ns1:hasUnit "http://qudt.org/vocab/unit/MilliM"^^xsd:anyURI ;
    ns1:value 10 .

<https://bue.materials-data.space/knowledge/specimen/emmo#EMMO_e4de48b1_dabb_4490_ac2b_040f926c64f0_specimen123-8ba20087> a <https://w3id.org/emmo#EMMO_e4de48b1_dabb_4490_ac2b_040f926c64f0> ;
    ns1:hasUnit "http://qudt.org/vocab/unit/MilliM"^^xsd:anyURI ;
    ns1:value 1 .

<https://bue.materials-data.space/knowledge/specimen/max_specimen123-8ba20087> a ns2:TotalLength ;
    ns1:hasUnit "http://qudt.org/vocab/unit/MilliM"^^xsd:anyURI ;
    ns1:value "0.2"^^xsd:float .

<https://bue.materials-data.space/knowledge/specimen/min_specimen123-8ba20087> a ns2:TotalLength ;
    ns1:hasUnit "http://qudt.org/vocab/unit/MilliM"^^xsd:anyURI ;
    ns1:value "0.1"^^xsd:float .


|---------------------------------------End of file---------------------------------------|


                         Downloaded file: testfile.txt
|------------------------------------Beginning of file------------------------------------|
This is a specimen preparation protocol!

|---------------------------------------End of file---------------------------------------|