Skip to content

Commit

Permalink
add example with creating items with metadata/tag/link from CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasCARPi committed Jul 4, 2023
1 parent 1f3fabe commit bd00adc
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
79 changes: 79 additions & 0 deletions examples/08.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python
import time
import datetime
import elabapi_python
from elabapi_python.rest import ApiException

# necessary imports for this example
import csv
import json

#########################
# CONFIG #
#########################
# replace with the URL of your instance
API_HOST_URL = 'https://elab.local:3148/api/v2'
# replace with your api key
API_KEY = 'apiKey4Test'
#########################
# END CONFIG #
#########################

# Configure the api client
configuration = elabapi_python.Configuration()
configuration.api_key['api_key'] = API_KEY
configuration.api_key_prefix['api_key'] = 'Authorization'
configuration.host = API_HOST_URL
configuration.debug = False
configuration.verify_ssl = False

# create an instance of the API class
api_client = elabapi_python.ApiClient(configuration)
# fix issue with Authorization header not being properly set by the generated lib
api_client.set_default_header(header_name='Authorization', header_value=API_KEY)

#### SCRIPT START ##################
# Description: read a csv file
# and create an item for each row, linked to a project
####################################

# Load the items api
itemsApi = elabapi_python.ItemsApi(api_client)
# and the links too
linksApi = elabapi_python.LinksToItemsApi(api_client)

# this is the path to the CSV file that we will read
csv_path = 'data/samples.csv'

# in which category (items_types) our samples will be created
# this corresponds to the previously created "Samples" category in our database
category_id = 1

# project map (see comment below when linking to a project)
project_map = {'Changes in a child’s subgingival microbiome following prophylaxis': 17, 'Another project': 16}

# this is our metadata json, with an empty value for now, as it will change for every row
# here the main body is hidden with display_main_text: false
base_json='{"extra_fields":{"Sample type":{"type":"select","value":"","options":["Saliva","Tongue","Nose"],"group_id":null,"description":"The type of the sample"}},"elabftw":{"display_main_text": false}}'
# we load it as an object
metadata_as_json = json.loads(base_json)
# read the csv and loop over rows
with open(csv_path, newline='') as csvfile:
csvreader = csv.DictReader(csvfile, delimiter=',', quotechar='"')
for row in csvreader:
# create an item in the correct category, we also add a tag with the sample_type here
response = itemsApi.post_item_with_http_info(body={'category_id': category_id, 'tags': [row['sample_type']]})
# the previous request gives us the ID of the newly created item, so look into the Location header to get it
locationHeaderInResponse = response[2].get('Location')
print(f'The newly created item is here: {locationHeaderInResponse}')
itemId = int(locationHeaderInResponse.split('/').pop())
# replace the value of the Sample type in metadata with the sample_type of the row
metadata_as_json['extra_fields']['Sample type']['value'] = row['sample_type']
# and now we modify the item with the correct title/metadata
itemsApi.patch_item(itemId, body={'title': row['sample_name'], 'body': '', 'metadata': json.dumps(metadata_as_json)})

# now we want to link our entry to the project
# one option would be to GET all items of a particular category and look for the title of our project to get its ID
# but we will instead use a map because we don't have too many projects and they will never change their ID
project_id = project_map[row['project_name']]
linksApi.post_entity_items_links('items', itemId, project_id)
4 changes: 4 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ Create users in batch.
# 07.py

Patch instance config: modify the settings of the Sysadmin Panel.

# 08.py

Read a CSV file for samples, create them in the database with metadata, tags and links.
6 changes: 6 additions & 0 deletions examples/data/samples.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sample_name,project_name,sample_type
Sample 1,Changes in a child’s subgingival microbiome following prophylaxis,Saliva
Sample 2,Changes in a child’s subgingival microbiome following prophylaxis,Saliva
Sample 3,Changes in a child’s subgingival microbiome following prophylaxis,Tongue
Sample 4,Another project,Nose
Sample 5,Another project,Saliva

0 comments on commit bd00adc

Please sign in to comment.