|
| 1 | +#!/usr/bin/env python |
| 2 | +import time |
| 3 | +import datetime |
| 4 | +import elabapi_python |
| 5 | +from elabapi_python.rest import ApiException |
| 6 | + |
| 7 | +# necessary imports for this example |
| 8 | +import csv |
| 9 | +import json |
| 10 | + |
| 11 | +######################### |
| 12 | +# CONFIG # |
| 13 | +######################### |
| 14 | +# replace with the URL of your instance |
| 15 | +API_HOST_URL = 'https://elab.local:3148/api/v2' |
| 16 | +# replace with your api key |
| 17 | +API_KEY = 'apiKey4Test' |
| 18 | +######################### |
| 19 | +# END CONFIG # |
| 20 | +######################### |
| 21 | + |
| 22 | +# Configure the api client |
| 23 | +configuration = elabapi_python.Configuration() |
| 24 | +configuration.api_key['api_key'] = API_KEY |
| 25 | +configuration.api_key_prefix['api_key'] = 'Authorization' |
| 26 | +configuration.host = API_HOST_URL |
| 27 | +configuration.debug = False |
| 28 | +configuration.verify_ssl = False |
| 29 | + |
| 30 | +# create an instance of the API class |
| 31 | +api_client = elabapi_python.ApiClient(configuration) |
| 32 | +# fix issue with Authorization header not being properly set by the generated lib |
| 33 | +api_client.set_default_header(header_name='Authorization', header_value=API_KEY) |
| 34 | + |
| 35 | +#### SCRIPT START ################## |
| 36 | +# Description: read a csv file |
| 37 | +# and create an item for each row, linked to a project |
| 38 | +#################################### |
| 39 | + |
| 40 | +# Load the items api |
| 41 | +itemsApi = elabapi_python.ItemsApi(api_client) |
| 42 | +# and the links too |
| 43 | +linksApi = elabapi_python.LinksToItemsApi(api_client) |
| 44 | + |
| 45 | +# this is the path to the CSV file that we will read |
| 46 | +csv_path = 'data/samples.csv' |
| 47 | + |
| 48 | +# in which category (items_types) our samples will be created |
| 49 | +# this corresponds to the previously created "Samples" category in our database |
| 50 | +category_id = 1 |
| 51 | + |
| 52 | +# project map (see comment below when linking to a project) |
| 53 | +project_map = {'Changes in a child’s subgingival microbiome following prophylaxis': 17, 'Another project': 16} |
| 54 | + |
| 55 | +# this is our metadata json, with an empty value for now, as it will change for every row |
| 56 | +# here the main body is hidden with display_main_text: false |
| 57 | +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}}' |
| 58 | +# we load it as an object |
| 59 | +metadata_as_json = json.loads(base_json) |
| 60 | +# read the csv and loop over rows |
| 61 | +with open(csv_path, newline='') as csvfile: |
| 62 | + csvreader = csv.DictReader(csvfile, delimiter=',', quotechar='"') |
| 63 | + for row in csvreader: |
| 64 | + # create an item in the correct category, we also add a tag with the sample_type here |
| 65 | + response = itemsApi.post_item_with_http_info(body={'category_id': category_id, 'tags': [row['sample_type']]}) |
| 66 | + # the previous request gives us the ID of the newly created item, so look into the Location header to get it |
| 67 | + locationHeaderInResponse = response[2].get('Location') |
| 68 | + print(f'The newly created item is here: {locationHeaderInResponse}') |
| 69 | + itemId = int(locationHeaderInResponse.split('/').pop()) |
| 70 | + # replace the value of the Sample type in metadata with the sample_type of the row |
| 71 | + metadata_as_json['extra_fields']['Sample type']['value'] = row['sample_type'] |
| 72 | + # and now we modify the item with the correct title/metadata |
| 73 | + itemsApi.patch_item(itemId, body={'title': row['sample_name'], 'body': '', 'metadata': json.dumps(metadata_as_json)}) |
| 74 | + |
| 75 | + # now we want to link our entry to the project |
| 76 | + # one option would be to GET all items of a particular category and look for the title of our project to get its ID |
| 77 | + # but we will instead use a map because we don't have too many projects and they will never change their ID |
| 78 | + project_id = project_map[row['project_name']] |
| 79 | + linksApi.post_entity_items_links('items', itemId, project_id) |
0 commit comments