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.
[5]:
from dsms import DSMS
Now source the environmental variables from an .env file and start the DSMS-session.
[6]:
dsms = DSMS(env=".env")
Now lets get the kitem we created in the 2nd tutorial : Creation of Kitems
[7]:
item = dsms["5cb3a95e-4aac-434d-81ba-7c242fd245aa"]
[8]:
item
[8]:
kitem:
id: 5cb3a95e-4aac-434d-81ba-7c242fd245aa
name: Specimen123
ktype_id: specimen
slug: specimen123-5cb3a95e
annotations: []
attachments:
- name: subgraph.ttl
linked_kitems: []
affiliations: []
authors:
- user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
avatar_exists: false
contacts: []
created_at: 2025-08-13 14:29:01.826691
updated_at: 2025-08-13 14:29:01.826691
external_links: []
apps: []
user_groups: []
custom_properties:
content:
sections:
- id: idb424123144cdd8
name: Untitled Section
entries:
- id: id6c76bbffe7ca78
type: Number
label: Width
value: 0.5
measurementUnit:
iri: http://qudt.org/vocab/unit/MilliM
label: Millimetre
symbol: null
namespace: http://qudt.org/vocab/unit
relationMapping: null
required: false
- id: id717d07130a7618
type: Slider
label: Length
value:
- 0.1
- 0.2
measurementUnit:
iri: http://qudt.org/vocab/unit/MilliM
label: Millimetre
symbol: null
namespace: http://qudt.org/vocab/unit
relationMapping: null
required: false
rdf_exists: true
contexts: []
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), listaccumulation (+= or -=), e.g. for the annotations, attachments, external_link, etc.
NOTE: using append or extend will not validate the pydantic model of the respective fields and hence will cause an error during commiting. Hence, always use +=, = or -=.
Other properties which are not list-like can be simply set by attribute-assignment (e.g. name, slug, ktype_id, etc).
[9]:
item.name = "Specimen-123"
item.custom_properties.Width = 1
item.attachments += ["testfile.txt"]
item.annotations += ["https://w3id.org/pmd/co/Specimen"]
item.external_links += [
{"url": "http://specimens.org", "label": "specimen-link"}
]
item.contacts += [{"name": "Specimen preparation", "email": "specimenpreparation@group.mail"}]
[10]:
dsms.add(item)
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:
[11]:
item
[11]:
kitem:
id: 5cb3a95e-4aac-434d-81ba-7c242fd245aa
name: Specimen-123
ktype_id: specimen
slug: specimen123-5cb3a95e
annotations:
- iri: https://w3id.org/pmd/co/Specimen
label: Specimen
namespace: https://w3id.org/pmd/co
attachments:
- name: subgraph.ttl
- name: testfile.txt
linked_kitems: []
affiliations: []
authors:
- user_id: 7f0e5a37-353b-4bbc-b1f1-b6ad575f562d
avatar_exists: false
contacts:
- name: Specimen preparation
email: specimenpreparation@group.mail
created_at: 2025-08-13 14:29:01.826691
updated_at: 2025-08-13 14:30:26.915234
external_links:
- label: specimen-link
url: http://specimens.org
apps: []
user_groups: []
custom_properties:
content:
sections:
- id: idb424123144cdd8
name: Untitled Section
entries:
- id: id6c76bbffe7ca78
type: Number
label: Width
value: 1
measurementUnit:
iri: http://qudt.org/vocab/unit/MilliM
label: Millimetre
symbol: null
namespace: http://qudt.org/vocab/unit
relationMapping: null
required: false
- id: id717d07130a7618
type: Slider
label: Length
value:
- 0.1
- 0.2
measurementUnit:
iri: http://qudt.org/vocab/unit/MilliM
label: Millimetre
symbol: null
namespace: http://qudt.org/vocab/unit
relationMapping: null
required: false
rdf_exists: true
contexts: []
Furthermore we can also download the file we uploaded again:
[12]:
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: <https://w3id.org/emmo#> .
@prefix ns2: <http://qudt.org/schema/qudt/> .
@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-5cb3a95e> a <https://w3id.org/pmd/co/Specimen> ;
rdfs:label "Specimen-123"^^xsd:string ;
ns1:EMMO_17e27c22_37e1_468c_9dd7_95e137f73e7f <https://bue.materials-data.space/knowledge/specimen/emmo#EMMO_e4de48b1_dabb_4490_ac2b_040f926c64f0_specimen123-5cb3a95e>,
<https://bue.materials-data.space/knowledge/specimen/max_specimen123-5cb3a95e>,
<https://bue.materials-data.space/knowledge/specimen/min_specimen123-5cb3a95e> .
<https://bue.materials-data.space/knowledge/specimen/emmo#EMMO_e4de48b1_dabb_4490_ac2b_040f926c64f0_specimen123-5cb3a95e> a ns1:EMMO_e4de48b1_dabb_4490_ac2b_040f926c64f0 ;
ns2:hasUnit "http://qudt.org/vocab/unit/MilliM"^^xsd:anyURI ;
ns2:value 1 .
<https://bue.materials-data.space/knowledge/specimen/max_specimen123-5cb3a95e> a ns1:EMMO_e4de48b1_dabb_4490_ac2b_040f926c64f0 ;
ns2:hasUnit "http://qudt.org/vocab/unit/MilliM"^^xsd:anyURI ;
ns2:value "0.2"^^xsd:float .
<https://bue.materials-data.space/knowledge/specimen/min_specimen123-5cb3a95e> a ns1:EMMO_cd2cd0de_e0cc_4ef1_b27e_2e88db027bac ;
ns2:hasUnit "http://qudt.org/vocab/unit/MilliM"^^xsd:anyURI ;
ns2:value "0.1"^^xsd:float .
|---------------------------------------End of file---------------------------------------|
Downloaded file: testfile.txt
|------------------------------------Beginning of file------------------------------------|
This is a specimen preparation protocol!
|---------------------------------------End of file---------------------------------------|