Upload 500+ materials to CRIPT from Excel file #259
Replies: 2 comments
-
A great script that shows how to reuse already existing material nodes from the CRIPT DB in your project, by @InnocentBug import os
import json
import cript
from cript.api.exceptions import CRIPTAPISaveError
uniq = "9"
data = [
{"name": "askdfjasdf"+uniq, "bigsmiles": "NXC"},
{"name": "Ammonia", "bigsmiles": "N"},
{"name": "benzene", "bigsmiles": "c1ccccc1"},
{"name": "askdfja"+uniq, "bigsmiles": "NXC"},
]
# create API client
with cript.API(
host=os.getenv("CRIPT_HOST"),
api_token=os.getenv("CRIPT_TOKEN"),
storage_token=os.getenv("CRIPT_STORAGE_TOKEN"),
) as api:
# create new project
my_project = cript.Project(name="material_search_test_project" + uniq)
# loop over rows in Excel sheet, and create materials with it
for i, d in enumerate(data):
name = d["name"]
# create a new material and set its identifier
new_material = cript.Material(name=name, identifiers=[{"bigsmiles": d["bigsmiles"]}])
my_paginator = api.search(node_type=cript.Material, search_mode=cript.SearchModes.EXACT_NAME, value_to_search=new_material.name)
if len(my_paginator.current_page_results) > 0:
my_material_from_api_dict = my_paginator.current_page_results[0]
try:
new_material = cript.load_nodes_from_json(json.dumps(my_material_from_api_dict))
except Exception as exc:
print(exc)
else:
print("reuse material", new_material.name)
# attach each new material to project
my_project.material += [new_material]
else:
my_project.material += [new_material]
api.save(project=my_project)
print(my_project.get_json(indent=2).json)
my_paginator = api.search(node_type=cript.Project, search_mode=cript.SearchModes.EXACT_NAME, value_to_search=my_project.name)
# get the project from paginator
my_project_from_api_dict = my_paginator.current_page_results[0]
print(json.dumps(my_project_from_api_dict, indent=2)) |
Beta Was this translation helpful? Give feedback.
-
Please note, this script is designed to add material nodes to a project in the CRIPT database one at a time. While it's a handy utility for quickly populating your project with materials, it does not establish connections between the nodes as they are saved in chunks. This means that if you are looking for a way to create a fully-connected, complex network of material nodes, this script may not be suitable. It's intended for more straightforward use-cases where you simply want to add a list of materials to a project without linking them together. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
All reactions