Understanding metadata
Besides the body and attached files, every eLabFTW entry (Experiment or Resource) has a JSON object called metadata. This page explains what's inside metadata, how the toolkit handles it, and where to put what. For details on each function, see 2. Entries and fields.
What's in an entry
Metadata has three places with different roles.
| Place | What goes there | In the eLabFTW interface | In the toolkit |
|---|---|---|---|
extra_fields (fields) | Values of the fields defined by the template, plus their definitions (type, unit, options) | Shown as inputs | entry.fields |
elabftw | Settings eLabFTW uses, such as field groups | Used to group fields | Left as is |
structured_metadata | Free-form nested data | Not shown as fields | entry.metadata.update_structured() |
The body tables, attachments, tags, links, and Steps live outside metadata. Use entry.tables, entry.files, entry.tags, entry.links, and entry.steps for them.
Where to put what
| What you want to store | Where |
|---|---|
| Values people read or search in the interface, like sample name, purity, or temperature | Fields |
| Values chosen from options, or numbers with units | Fields (type, unit, and options are defined in the template) |
| Detailed nested data, like a full set of instrument settings | Structured metadata |
| A list of measurement conditions for people to read | A table in the body |
| Spectra or analysis result files | Attachments |
| Data to hand to other systems or databases | Build MaiML from the fields and attachments (How MaiML export works) |
When in doubt: values people look at go in fields; detailed data for machines goes in structured metadata.
Working with fields (extra_fields)
entry = elab.items.entry(item_id)
entry.fields["purity"] # read one
entry.fields.update({"purity": "99.9", "form": "powder"}) # write several
list(entry.fields) # field names
entry.fields.units # {field name: unit}- Only the values you write change. Field types, units, options, groups, and structured metadata stay as they are.
- Writing a field name that isn't in the template raises
ValueError. Create fields in the eLabFTW template first. - To put a Resource or Experiment into a field (as a link target), use
set_item_field_and_link()and similar functions (6. Links and tags).
Working with structured metadata
entry = elab.experiments.entry(exp_id)
entry.metadata.update_structured({
"schema_name": "example_experiment_metadata",
"schema_version": "0.1.0",
"measurement": {"beamline": "BL99XU", "technique": "XAFS"},
"sample": {"id": "sample-001"},
})
entry.metadata.structured() # read
entry.metadata.delete_structured() # delete- By default, the data goes under the
structured_metadatakey of metadata. You can use another key withmetadata_key=, but not keys eLabFTW uses, such asextra_fieldsorelabftw. - By default (
overwrite=False), new data is merged into the existing data, including nested parts. Withoverwrite=True, the given data replaces it. - The shape is up to you. Including information like
schema_nameandschema_versionthat says what shape the data has makes it easier for people and programs to read later. - Converting to and from a flat "key = value" list and checking against a schema are explained in Reading and writing structured_metadata. For SPring-8 BENTEN's
metadata.all.yml, see BENTEN metadata and schema.
Working with the whole metadata
If the field and structured metadata functions aren't enough, you can read and write the metadata JSON directly.
metadata = entry.metadata.get()
entry.metadata.update({...}, overwrite=False)Be careful with overwrite=True: it replaces the whole metadata, including field definitions.
Checking before you write
You can check that field values follow a schema (required fields, types, options, ranges, and so on) before writing them. Each facility prepares its rules as JSON or Excel. See 15. Validation and plans for details, and Writing the schema in Excel for the Excel layout.
What to read next
- Reading and writing structured_metadata: nested metadata, @ keys, schema checks
- BENTEN metadata and schema: a real example from SPring-8 BENTEN
- How MaiML export works: build MaiML from fields and attachments to hand data to others
- 2. Entries and fields: how to use the
fieldsandmetadatafunctions - 15. Validation and plans: schema validation, and trying changes without writing