Skip to content

6. Links and tags ​

Linking entries together, and adding or removing tags.

The short version ​

python
exp = elab.experiments.entry(experiment_id)

# Put a sample into the items-type field "sample_id" and create the link
exp.set_item_field_and_link(
    field_name="sample_id",
    item_title="TEST_SAMPLE",
    item_category_title="Sample",
)

exp.links.add_item(target_item_id=99)                              # just a link
exp.tags.add(tags=["2026T", "XAFS"], require_existing=False)       # add tags (no allowed-list check)

These methods look up the target's ID from its title and put it into an eLabFTW items-type or experiments-type field. The _and_link variants also create a link between the entries.

Called onValueField onlyField + link
Experiment / itemResource itemset_item_field(...)set_item_field_and_link(...)
Experiment / itemExperimentset_experiment_field(...)set_experiment_field_and_link(...)

The names and arguments are the same for Experiments and items.

A Resource item in an Experiment field ​

python
updated_metadata = elab.experiments.entry(experiment_id).set_item_field(
    field_name="proposal_number",
    item_title="2026T0001",
    item_category_title="Proposal",
)

# also create the link
updated_metadata = elab.experiments.entry(experiment_id).set_item_field_and_link(
    field_name="proposal_number",
    item_title="2026T0001",
    item_category_title="Proposal",
    link_back=False,
)

# when the field belongs to a group
updated_metadata = elab.experiments.entry(experiment_id).set_item_field_and_link(
    field_name="sample_id",
    item_title="TEST_SAMPLE",
    item_category_title="Sample",
    expected_group_name="sample_info",   # or expected_group_id=...
)

Another Experiment in an Experiment field ​

python
updated_metadata = elab.experiments.entry(experiment_id).set_experiment_field(
    field_name="related_exp",
    experiment_title="2026T0001-XAFS-reference_sample",
    experiment_category_title="Measurement",
)

updated_metadata = elab.experiments.entry(experiment_id).set_experiment_field_and_link(
    field_name="related_exp",
    experiment_title="2026T0001-XAFS-reference_sample",
    experiment_category_title="Measurement",
    link_back=False,
)

Another item / an Experiment in a Resource item field ​

python
elab.items.entry(item_id).set_item_field(
    field_name="proposal_number",
    item_title="2026T0001T",
    item_category_title="Proposal",
)
elab.items.entry(item_id).set_item_field_and_link(
    field_name="proposal_number",
    item_title="2026T0001",
    item_category_title="Proposal",
    link_back=False,
)

elab.items.entry(item_id).set_experiment_field(
    field_name="related_exp",
    experiment_title="2026T0001-XAFS-test_sample-300K",
    experiment_category_title="Measurement",
)
elab.items.entry(item_id).set_experiment_field_and_link(
    field_name="related_exp",
    experiment_title="2026T0001-XAFS-test_sample-300K",
    experiment_category_title="Measurement",
    link_back=False,
)

The service-level methods (elab.experiments.set_experiment_field(...) and so on) use the same names.

Pitfalls ​

  • item_title, experiment_title, and so on must match the title exactly. If several candidates are found, narrow them with item_category_title or similar.
  • For fields that belong to a group, specify expected_group_name or expected_group_id.
python
links = elab.experiments.entry(experiment_id).links   # for a Resource item: elab.items.entry(item_id).links

links.add_item(target_item_id=99)                  # link to a Resource item
links.add_experiment(target_experiment_id=99)      # link to another Experiment

item_links = links.item_list()                     # list links
experiment_links = links.experiment_list()

links.unlink_item(target_item_id=99)               # remove links
links.unlink_experiment(target_experiment_id=99)

The same methods work with a Resource item as the source (elab.items.entry(item_id).links.add_experiment(target_experiment_id=99) and so on).

When set_*_field_and_link() creates a link that already exists, it is ignored instead of raising (only for HTTP 409, or a message containing already / duplicate / exists / unique; a 400 with an empty body raises).

Adding and removing tags ​

Load the list of allowed tags from a CSV file and add the given tags. To add tags on creation, experiments.init(..., tags=...) / items.init(..., tags=...) also work (4. Creating).

python
allowed_tags = elab.tags.load_allowed("allowed_tags.csv")
tags = ["2026T", "BL99XU", "XAFS"]

elab.experiments.entry(experiment_id).tags.add(
    tags=tags,
    allowed_tags=allowed_tags,
    require_existing=True,
    debug=True,
)
elab.items.entry(item_id).tags.add(tags=tags, allowed_tags=allowed_tags, require_existing=True)

With require_existing=True (the default), tags not in allowed_tags are not added (and omitting allowed_tags raises ValueError). To skip the allowed-list check, pass require_existing=False. An example CSV is tests/live/test_allowed_tags.csv.

To remove tags (confirm=True is required to prevent accidents):

python
elab.experiments.entry(experiment_id).tags.clear(confirm=True)
elab.items.entry(item_id).tags.clear(confirm=True)

Pitfalls ​

  • tags.clear(confirm=True) removes the entry's tags. Double-check the target first.

Next: 7. Permissions.

Apache License 2.0