Skip to content

2. Entries and fields (fields, metadata, structured metadata) ​

How to read and write a single Experiment or Resource item. In most cases entry.fields is all you need.

The short version ​

python
entry = elab.items.entry(5)           # for an Experiment: elab.experiments.entry(12)

entry.fields["purity"]                # read a field (extra_fields) value
entry.fields.update({"purity": "99.9%", "form": "powder"})   # write several at once

To change a locked entry, wrap the code in with entry.unlocked(): (8. Lock and timestamp).

Reading and writing fields (extra_fields): entry(id).fields ​

entry(id).fields lets you treat extra_fields (eLabFTW's "extra fields") like a {name: value} dict. Writes go through metadata.update(), so the rest of the metadata (structured_metadata, groups, field types, options, units, and so on) is kept.

python
f = elab.items.entry(item_id).fields   # the metadata is fetched once here
f["purity"]                            # value (KeyError if the field does not exist)
f.get("lot_number", "")                # default if missing
f.units                                # {name: unit} (only fields with a unit)
f.definitions                          # the raw extra_fields {name: {"type", "value", "unit", ...}}
dict(f)                                # {name: value}

f.update({"purity": "99.9%", "form": "powder"})   # write several (1 fetch, 1 PATCH)
f["purity"] = "99.99%"                            # write a single field

Options and pitfalls ​

  • By default (strict=True), writing a field that does not exist in the entry raises ValueError. With f.update({...}, strict=False) it is added as a value-only field (to add definitions such as the type too, use metadata.update()).
  • Every access to entry(id).fields returns a fresh copy. If you keep using the same f, changes made elsewhere are not visible (reload with f.refresh()).

Reading from an already fetched entry (dict) ​

For entries you already have as API v2 JSON (from elab.items.all() or entry.raw()), use these functions:

python
x = elab.items.entry(item_id).raw()
elabftw_toolkit.extra_fields(x)        # {name: value} (None for fields without a value)
elabftw_toolkit.extra_field_units(x)   # {name: unit}
elabftw_toolkit.metadata_of(x)         # the metadata dict (parsed if it is a JSON string)

Reading the raw API JSON and patching attributes: raw() / patch() ​

python
x = elab.experiments.entry(experiment_id).raw()   # API v2 JSON (dict)
x["title"], x["locked"], x["uploads"], x["items_links"]

elab.experiments.entry(experiment_id).patch(body="<p>Body</p>")   # PATCH attributes as is
elab.items.entry(item_id).patch(title="New title")
elab.items.entry(item_id).patch(metadata=new_metadata)           # a dict is sent as a JSON string (replaces the whole metadata)
  • raw() includes metadata (a JSON string), uploads, items_links, experiments_links, tags, locked, and more. elab.experiments.raw(id) / elab.items.raw(id) return the same thing.
  • patch(metadata=...) replaces the whole metadata. To change only part of it, use fields.update() or metadata.update().
  • The entry ID is available as entry.id (experiment_id / item_id also work).

Getting and updating metadata: entry(id).metadata ​

Experiments and Resource / Database items handle metadata the same way.

python
metadata = elab.experiments.entry(experiment_id).metadata.get()
print(metadata)

metadata_update = {
    "extra_fields": {
        "description": {
            "value": "Updated experiment",
        },
    },
}

updated_metadata = elab.experiments.entry(experiment_id).metadata.update(
    metadata_update,
    overwrite=False,
    strict_extra_fields=True,
)

The same works for Resource / Database items:

python
metadata = elab.items.entry(item_id).metadata.get()

updated_metadata = elab.items.entry(item_id).metadata.update(
    {"extra_fields": {"description": {"value": "Updated resource"}}},
    overwrite=False,
    strict_extra_fields=True,
)

Options ​

ArgumentMeaning
overwrite=False (recommended)Merge into the existing metadata
overwrite=TrueReplace the whole metadata (other fields and definitions are lost)
strict_extra_fields=TrueRaise an error when updating an extra_fields entry that does not exist in the current metadata. Useful to avoid accidentally breaking the template definition

Structured metadata ​

Apart from extra_fields, you can store arbitrary structured data in a top-level key of the metadata. By default metadata["structured_metadata"] is used (another key can be chosen with metadata_key=).

python
structured_metadata = {
    "schema_name": "example_experiment_metadata",
    "schema_version": "0.1.0",
    "proposal": {"id": "2026T0001"},
    "measurement": {"beamline": "BL99XU", "technique": "XAFS"},
    "sample": {"id": "sample-001"},
}

# update
elab.experiments.entry(experiment_id).metadata.update_structured(structured_metadata, overwrite=False)
elab.items.entry(item_id).metadata.update_structured(structured_metadata, overwrite=False)

# get
structured = elab.experiments.entry(experiment_id).metadata.structured()
item_structured = elab.items.entry(item_id).metadata.structured()

# delete
elab.experiments.entry(experiment_id).metadata.delete_structured()
elab.items.entry(item_id).metadata.delete_structured()
  • With overwrite=False, it is deep-merged with the existing structured metadata.
  • With overwrite=True, it is replaced with the given content (old keys do not remain).

Getting and updating category / status ​

python
exp = elab.experiments.entry(experiment_id)

exp.category()          # {"id": ..., "title": ...}
exp.category_id()
exp.category_title()

exp.status()            # {"id": ..., "title": ...}
exp.status_id()
exp.status_title()

exp.set_category(category_title="Measurement")
exp.set_status(status_title="Running")

The same works for Resource / Database items:

python
item = elab.items.entry(item_id)
item.category_title()
item.set_category(category_title="Sample")
item.set_status(status_title="In use")

set_category() / set_status() also accept category_id / status_id directly. You cannot give both an ID and a title.

Pitfalls ​

  • overwrite=True replaces the entire existing metadata. Normally use overwrite=False.
  • Field names must match the names shown in eLabFTW exactly (watch for full-width/half-width characters and spaces). Unknown names raise KeyError (when reading) / ValueError (when writing).

Next: 3. Listing and search.

Apache License 2.0