4. Creating entries and templates
How to create new Experiments (lab notebook entries) and Resource / Database items. With a template, the body, field definitions, Steps, and more are carried over.
The short version
# Create an Experiment from a template
exp_id = elab.experiments.init(
title="2026T0001-XAFS-test_sample-300K",
template_name="Measurement test",
category_id=elab.experiments.categories.id("Measurement"),
)
# Create a Resource item from a template
item_id = elab.items.init(
title="TEST_SAMPLE",
category_id=elab.items.categories.id("Sample"),
template_name="Sample",
)
elab.items.entry(item_id).fields.update({"purity": "99.9%"}) # fill in fields afterwardsinit() returns the ID (int) of the created entry.
Checking templates
Experiment templates
templates_df = elab.experiments.templates.list(debug=True)
template_id = elab.experiments.templates.id("Measurement test")
template_raw = elab.experiments.templates.raw(template_id)
elab.experiments.templates.check(template_raw) # show body and metadata
template = elab.experiments.templates.entry(template_id) # the template itselfelab.experiments.templates.prepare_metadata(...) prepares template metadata for a new Experiment (normally init() calls it internally, so you do not need to).
Resource item templates / item types
item_templates_df = elab.items.templates.list(debug=True)
item_template_id = elab.items.templates.id("Proposal information")
item_template = elab.items.templates.entry(item_template_id) # same as raw()Creating an Experiment: elab.experiments.init()
With template_name or template_id, the Experiment is created from that template's body and metadata. Without either, an empty Experiment without body or template metadata is created.
exp_category_id = elab.experiments.categories.id("Measurement")
exp_status_id = elab.experiments.statuses.id("Planned")
title = elabftw_toolkit.make_experiment_title(
proposal_id="2026T0001",
sample_context="XAFS-test_sample-300K",
) # -> "2026T0001-XAFS-test_sample-300K" (<proposal_id>-<sample_context>)
new_experiment_id = elab.experiments.init(
title=title,
template_name="Measurement test",
category_id=exp_category_id,
status_id=exp_status_id,
tags=["2026T", "BL99XU", "XAFS"],
allowed_tags=elab.tags.load_allowed("allowed_tags.csv"),
require_existing_tags=True,
copy_template_metadata=True,
clear_template_values=False,
)
print("experiment_id =", new_experiment_id)Creating without a template
new_experiment_id = elab.experiments.init(
title="2026T0001-XAFS-test_sample-300K",
category_id=exp_category_id,
status_id=exp_status_id,
metadata_update={"extra_fields": {"description": {"value": "Note"}}}, # save metadata on creation
)Main arguments
| Argument | Default | Meaning |
|---|---|---|
title | (required) | Title |
template_name / template_id | none | Template to use. Cannot be given together |
category_id / status_id | none | Category / status ID (look up with categories.id("...") etc.) |
tags | none | A string or list of strings; tags added on creation |
allowed_tags, require_existing_tags | none, True | Only add tags contained in the allowed list (6. Tags) |
metadata_update | none | Metadata merged on top of the template metadata |
copy_template_metadata | True | Copy the template metadata |
clear_template_values | False | Clear only the values of template fields (definitions such as required are kept) |
copy_template_steps | True | Copy the template Steps |
copy_template_permissions | True | Apply the template's "for derived entries" permissions (see below) |
fail_if_title_exists_in_category | False | When True, raise an error instead of creating if an Experiment with the same title exists in the category |
What is filled in when creating from a template
With template_name / template_id, the following template-derived information is applied to the Experiment created through the eLabFTW API:
template body / metadata
template steps
the template's "for derived entries" read/write permissionsFor permissions, the toolkit uses the "for derived entries" permissions that apply to Experiments created from the template, not the template's own read/write permissions. It does not fall back to the template's own canread / canwrite.
canread_base <- canread_target_base
canread <- canread_target
canwrite_base <- canwrite_target_base
canwrite <- canwrite_targetThis is because creation through the eLabFTW API does not always apply the same template-derived properties as the Web UI. Normally no extra options are needed.
read/write permission lockdown is not supported. eLabFTW rejects PATCH requests to canread_is_immutable / canwrite_is_immutable on existing entries, so this library does not change them. If you need lockdown, use the eLabFTW UI or eLabFTW's own template creation flow.
Creating a Resource / Database item: elab.items.init()
item_category_id = elab.items.categories.id("Proposal")
item_status_id = elab.items.statuses.id("Planned")
new_item_id = elab.items.init(
title="2026T0001",
category_id=item_category_id,
template_name="Proposal information",
status_id=item_status_id,
tags=["2026T", "BL99XU"],
allowed_tags=elab.tags.load_allowed("allowed_tags.csv"),
require_existing_tags=True,
)
print("item_id =", new_item_id)- Specify the target category with
category_id(get it from a name withelab.items.categories.id("...")). template_id/template_namerefer to a Resource template (eLabFTW's/items_types; the ID shown in resources-templates.php), not an ordinary item (the ID in database.php).template_idandtemplate_namecannot be given together.- Expanding the template body and metadata is left to eLabFTW itself. With
metadata_update, the given metadata is merged on top of the expanded metadata.clear_template_values=Trueclears only the values. - For items,
fail_if_title_exists_in_categorydefaults toTrue: an error is raised if an item with the same title exists in the category. - As with Experiments, tags can be added with
tags,allowed_tags, andrequire_existing_tags.
Permissions derived from item templates / item types
Unlike Experiments, items.init() does not PATCH template-derived permissions on the toolkit side. For Resource / Database items, eLabFTW's own item type / template creation is expected to handle derived permissions, so permission inheritance is left to eLabFTW's standard behavior.
Pitfalls
- Running the same script twice can create the same entry twice. For Experiments, pass
fail_if_title_exists_in_category=Trueor check first withfind()/all()(11. Recipes). - If you change a template's structure (field names, table order), scripts that fill in fields or tables after creation must be updated too.
Next: 5. Files and images.