Skip to main content

Simple Code Blocks

Simple code blocks to use as part of your workflow

Ilya letnik avatar
Written by Ilya letnik
Updated over 10 months ago

General Functions

In-app notification popup

def notifyLabguru(content, members_ids, flow_title):
url = f'{base()}/api/v1/web_notifications/notify'
data = {
"item": {
"title":f"Labguru Automation - {flow_title}",
"content": content
},
"recipients": members_ids,
"token": token()
}
response = requests.post(url, json=data)
print ("notifyLabguru response status_code", response.status_code)

### member_ids should be sent as an array

Inventory Items

Update an inventory item

# Update the "name" field of the trigger_bacterium item using a PUT request

# Get the value of the "trigger_bacterium" variable
new_bac = variable("trigger_bacterium")

res = requests.put(f"{base()}/api/v1/bacteria/{new_bac['id']}", json={"token":token(), "item":{"name":new_item_name}})
print("request code is ", res.status_code)
if res.status_code // 100 == 2:
print("created new item with name ", new_item_name)

Get a filtered list of inventory items

# Get the value of the "trigger_bacterium" variable
new_bac = variable("trigger_bacterium")

# Initialize page count for pagination
page_count = 1

# Initialize a variable to store the last item in the list
last_item = ""

# Start a loop until the last_item is found
while not last_item:

# Prepare the request body for the API call
kendo_body = {
"token": token(),
"kendo": True,
"page": page_count,
"sort": {"0": {"field": "name", "dir": "desc"}},
"filter":{
"logic":"or",
"filters":
{"0":
{
"field":"name",
"operator":"startswith",
"value": "b"
},
}
}
}

# Send a GET request to the API to retrieve page items and parse the response as JSON
items_in_page = requests.get(f"{base()}/api/v1/bacteria", json=kendo_body).json()

ELN Experiments

Add sample to a sample element

#Adds a sample to sample element, provide sample element and item objects
def add_sample_to_sample_element(item, sample_element_id):
# reset sample element data, necessary for update element
reset = requests.put(f'{base()}/api/v1/elements/{sample_element_id}', json={
'token': token(),
'element': {
'data': None
}
})


# Add sample to sample element
response = requests.post(f'{base()}/api/v1/samples', json={
'token': token(),
'item': {
"name": item['name'],
"item_type": item['class_name'],
"item_id": item['id'],
"container_id": sample_element_id,
"container_type": "Element",
}
})
return response
#Examples

se = requests.get(f"{base()}/api/v1/elements/559", json={"token": token()}).json() #get a sample element
cell_line = requests.get(f"{base()}/api/v1/cell_lines/21", json={"token": token()}).json() #get an item

sample = add_sample_to_sample_element(cell_line, se)

Get elements from an experiment

#get elements from an experiment. must write exp. id. If all parameters left blank, return all elements in an experiment
def get_elements_from_experiment(exp_id, element_type='', element_name=''):
if element_type:
elements = requests.get(f'{base()}/api/v1/experiments/{exp_id}/elements', json={'token': token(), 'element_type':element_type}).json()
elif element_name:
elements = requests.get(f'{base()}/api/v1/experiments/{exp_id}/elements', json={'token': token(), 'name': element_name}).json()
else:
elements = requests.get(f'{base()}/api/v1/experiments/{exp_id}/elements', json={'token': token()}).json()
return elements

#Example
get_elements_from_experiment(33, element_type='samples', element_name='')

Copy section

#For entity, put only string with either "experiments" or "reports"
def copy_section(entity ,destination_id, section_id):
if entity == "experiments" or entity == "reports":
body = {'item':
{'item_id': f"{destination_id}",
'section_id': f"{section_id}"},
"token": token()}
res = requests.post(f'{base()}/api/v1/{entity}/copy_section', json=body)
return res
else:
return "INVALID ENTITY"

Datasets

Get a dataset

# Retrieve data from dataset
data_list =[]
dataset = requests.get (f'{base()}/api/v1/datasets/nnn', json = {'token': token()})
# print ("get dataset", dataset.status_code)
dataset = variable ("dataset_1")
for vector in dataset["vectors"]:
data_list.append(vector["vector"]["data"])
print (vector["vector"]["data"])

# Create a DataFrame from the data
df = pd.DataFrame(data_list)

Did this answer your question?