Attachments

This object handles the attachments attached to a element

API Classes

class chemotion_api.elements.attachments.Attachments(con: Connection, attachment_data: list[dict] | None = None)

Attachments is a container object to manage (read/write) all attachments of an element. Each attachemnt is a chemotion_api.elements.attachments.Attachment. It allows to load Attachments or add new ones. Attachment can be iterated!

Usage:

>>> from chemotion_api import Instance
>>> import logging
>>> try:
>>>     instance = Instance('http(d)://xxx.xxx.xxx').test_connection().login('<USER>', "<PASSWORD>")
>>> except ConnectionError as e:
>>>     logging.error(f"A connection to Chemotion ({instance.host_url}) cannot be established")
>>> rp = instance.get_research_plan(1)
>>> att = rp.attachments
>>> att.load_attachment(filename='MyAttachment.txt')
>>> att.add_file('./MySecondAttachment.txt')
>>> rp.save()
Parameters:
  • con – Host connection object.

  • attachment_data – list of attachment information (from the sever)

add_file(file_path: str) dict

Adds a file to the elements attachment container

Parameters:

file_path – Path to the file to be uploaded on your device

Returns:

a summary of the file

property attachment_data: list

The Attachment data contains all information needed to save the Attachments.

Returns:

List of all attachment data

load_attachment(id: str | int | None = None, identifier: str | None = None, filename: str | None = None) Attachment

Loads an attachment from the server. One of the parameters Id, Identifier or filename must be set in order to find the desired attachment. If Id is set, all other parameters are ignored; if Identifier is set, filename is ignored.

Parameters:
  • id – DB id of the attachment

  • identifier – UUID of the attachment

  • filename – name of the attachment

Raises:

ValueError – If no file fits the parameters.

Returns:

Attachment Object

save(id: int, element_type: str) Response

Updates or creates the Attachments of an Element after saving it!

Parameters:
  • id – ID of the element

  • element_type – must be in ‘Element’ or ‘ResearchPlan’

Returns:

response of the request

class chemotion_api.elements.attachments.Attachment(con: ~chemotion_api.connection.Connection, data: dict[slice(<class 'str'>, <class 'str'>, None)])

Attachment are a dict object to read one attachment of an element. It allows to load Attachments as bytes or save it to a file.

Usage:

>>> from chemotion_api import Instance
>>> import logging
>>> try:
>>>     instance = Instance('http(d)://xxx.xxx.xxx').test_connection().login('<USER>', "<PASSWORD>")
>>> except ConnectionError as e:
>>>     logging.error(f"A connection to Chemotion ({instance.host_url}) cannot be established")
>>> rp = instance.get_generic_by_label('My Generic',1)
>>> att = rp.attachments
>>> att.load_attachment(filename='MyAttachment.txt').save_file('.')
Parameters:
  • con – Host connection object.

  • data – attachment information (from the sever)

load_file() bytes

Loads an attachment file as binary object

Returns:

file as binary object

save_file(directory: str = '.') str

Loads an attachment file to your local filesystem

Parameters:

directory – path to the directory where the file shell be saved

Returns:

full path to the file