Examples#

Runnable scripts for common tasks, in examples/. Each one runs without an account, and CI runs each one end to end.

Example

Task

Needs

save_metadata

Write a record to any destination, read it back, update it, and handle a conflict

Base install

open_labarchives_notebook

Log in to LabArchives, list notebooks, open one, and load a record

metarelay[labarchives], and the fake server from docker compose up

open_elabftw_experiment

Connect to eLabFTW with an API key, list entries, open one, and load a record

metarelay[elabftw], and eLabFTW from docker compose up elabftw

open_github_repo

Connect to GitHub with a token, list folders, open one, and load a record

metarelay[github], and the fake from python -m metarelay.testing.github_fake

credentials_env_file

Read a token from a .env file with _env_file=

metarelay[github]. It serves its own fake GitHub

Run them from the repository root as modules, after uv sync (add --extra labarchives for LabArchives, --extra elabftw for eLabFTW, --extra github for GitHub). save_metadata writes to every destination; its README shows the URI and settings for LabArchives, eLabFTW, and GitHub:

mkdir -p metadata
uv run python -m examples.save_metadata "file://$PWD/metadata"

Shared pieces#

File

Holds

examples/login.py

open_session(uri): connect() for most backends (eLabFTW reads its API key from METARELAY_ELABFTW_TOKEN, GitHub its token from METARELAY_GITHUB_TOKEN), the browser login for LabArchives

examples/lab_schemas.py

The lab.implant schema every example uses. MetaRelay’s own schemas replace it in #10 and #11

How they stay correct#

  • Each example splits reusable functions, which take a session and return data, from main(), which handles arguments, login, output, and exit codes. This is the same layout as labapi’s examples.

  • tests/examples/ runs every example against a temporary folder and in-process fake LabArchives and GitHub servers, both by calling main() and as python -m examples.<name>. The eLabFTW tests run against the dev stack’s eLabFTW when METARELAY_ELABFTW_TEST_API_URL is set, as in CI’s elabftw job.

  • The code in the quickstart, concepts, LabArchives, eLabFTW, and GitHub pages is copied from marked regions of the examples. tests/examples/test_docs_snippets.py fails if a page and its example drift apart. After changing an example, update the pages with uv run python tests/examples/test_docs_snippets.py.

Writing a backend isn’t an example here. The “write a backend” guide (#13) covers it. Until then, read src/metarelay/testing/memory.py (the smallest backend), then backends/local.py and backends/labarchives.py, and see architecture.