Metadata-Version: 2.4
Name: elabftw-toolkit
Version: 0.1.3
Summary: A Python toolkit for automating and standardizing eLabFTW API workflows
Author-email: Takahiro Matsumoto <matumot@jasri.jp>
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/jasri-jp/elabftw-toolkit
Project-URL: Documentation, https://jasri-jp.github.io/elabftw-toolkit/
Project-URL: Repository, https://github.com/jasri-jp/elabftw-toolkit.git
Project-URL: Issues, https://github.com/jasri-jp/elabftw-toolkit/issues
Keywords: eLabFTW,ELN,electronic lab notebook,MaiML,synchrotron,research data management,MCP
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Natural Language :: English
Classifier: Natural Language :: Japanese
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: elabapi-python
Requires-Dist: pandas
Requires-Dist: python-dotenv
Requires-Dist: requests
Requires-Dist: beautifulsoup4
Requires-Dist: pillow
Requires-Dist: urllib3
Requires-Dist: ipython
Provides-Extra: maiml
Requires-Dist: pymaiml<2.0,>=1.0; extra == "maiml"
Provides-Extra: mcp
Requires-Dist: mcp>=1.0; extra == "mcp"
Provides-Extra: benten
Requires-Dist: pyyaml; extra == "benten"
Requires-Dist: openpyxl; extra == "benten"
Dynamic: license-file

# elabftw-toolkit

[![PyPI](https://img.shields.io/pypi/v/elabftw-toolkit)](https://pypi.org/project/elabftw-toolkit/) [![Python](https://img.shields.io/pypi/pyversions/elabftw-toolkit)](https://pypi.org/project/elabftw-toolkit/) [![Docs](https://img.shields.io/badge/docs-GitHub%20Pages-blue)](https://jasri-jp.github.io/elabftw-toolkit/) [![License](https://img.shields.io/badge/license-Apache--2.0-green)](https://github.com/jasri-jp/elabftw-toolkit/blob/main/LICENSE)

eLabFTW の実験ノート（Experiment）と Resource / Database item を Python から操作するための、非公式の toolkit です。
テンプレートからの作成、項目の読み書き、ファイルの添付、リンク、権限、lock などのよくある作業を、短いスクリプトで書けます。

**📖 ドキュメント（利用者ガイド・チートシート・レシピ集）: https://jasri-jp.github.io/elabftw-toolkit/**

[English](https://github.com/jasri-jp/elabftw-toolkit/blob/main/README_en.md) ／ [利用者ガイド](https://jasri-jp.github.io/elabftw-toolkit/manual/01) ／ [ドキュメントサイト](https://jasri-jp.github.io/elabftw-toolkit/)

> eLabFTW 本体・公式 SDK とは別のプロジェクトです。eLabFTW のバージョンや設定によって動きが変わることがあるので、本番で使う前に検証用の entry で試してください。

## できること

- **作る**：テンプレートから実験ノートや Resource item を作る（本文・項目・Steps・権限を引き継ぐ）
- **読む・書く**：項目の値を `entry.fields["purity"]` のように dict と同じ感覚で扱う
- **探す**：一覧を表 (DataFrame) で、全件を JSON で取得する。条件検索や title での検索もできる
- **添付する**：ファイルの添付（同じファイルは飛ばす）、一括ダウンロード、本文への画像の追加、PDF / ELN / JSON での書き出し
- **つなぐ・管理する**：entry どうしのリンク、タグ、権限、lock とタイムスタンプ、本文の表と Steps
- **安全に書き込む**：書き込まずに試す（dry_run）、スキーマでの確認、二重登録の防止、変更者の記録
- **書き出す**：MaiML（JIS K 0200）形式への書き出し（追加インストールが必要）

## インストール

Python 3.10 以上が必要です。

```bash
python -m pip install elabftw-toolkit            # 基本
python -m pip install "elabftw-toolkit[maiml]"   # MaiML への書き出しも使う場合
python -m pip install "elabftw-toolkit[mcp]"     # MCP サーバー（Claude Desktop など）も使う場合
python -m pip install "elabftw-toolkit[benten]"  # BENTEN のメタデータ（YAML・Excel）も扱う場合
```

開発する場合（ソースを直しながら使う）は、clone して editable install にします。

```bash
git clone https://github.com/jasri-jp/elabftw-toolkit.git
cd elabftw-toolkit
python -m pip install -e ".[maiml,mcp,benten]"
```

## 設定

eLabFTW の接続先と API key を環境変数で指定します。

```bash
ELABFTW_HOST_URL="https://your-elabftw.example.jp"
ELABFTW_API_KEY="your-api-key"
```

- API key をファイルに置くときは、`ELABFTW_API_KEY` の代わりに `ELABFTW_KEY_FILE="/path/to/api_key.txt"` を使います。
- `.env` ファイルは自動では読みません。使うときは先に `from dotenv import load_dotenv; load_dotenv()` を呼びます。
- 引数での指定や SSL の設定は [1. はじめに](https://jasri-jp.github.io/elabftw-toolkit/manual/01) を見てください。

## 5 分で試す

```python
import elabftw_toolkit

elab = elabftw_toolkit.client("your-team-name")

# 試料を title で探して開く
sample = elab.items.find("TEST_SAMPLE", category_title="試料")
entry = elab.items.entry(sample.id)

# 項目を読む・書く
print(entry.fields["purity"])
entry.fields.update({"purity": "99.9%", "form": "粉末"})

# lock されていても、一時的に外して書き換える（終わったら元に戻る）
with entry.unlocked():
    entry.fields["purity"] = "99.99%"

# ファイルを添付する（同じファイルがあれば飛ばす）
entry.files.upload("coa.pdf")

# テンプレートから実験ノートを作り、試料とつなぐ
exp_id = elab.experiments.init(
    title="2026T0001-XAFS-TEST_SAMPLE",
    template_name="計測試験",
    category_id=elab.experiments.categories.id("計測"),
)
elab.experiments.entry(exp_id).set_item_field_and_link(
    field_name="sample_id", item_title="TEST_SAMPLE", item_category_title="試料",
)
```

`"試料"`・`"計測"`・`"計測試験"` などの名前は例です。お使いの eLabFTW の設定に合わせてください。

## 利用者ガイド

| やりたいこと | ページ |
| --- | --- |
| 使い始める | [1. はじめに](https://jasri-jp.github.io/elabftw-toolkit/manual/01)、[12. トラブルシューティング](https://jasri-jp.github.io/elabftw-toolkit/manual/12) |
| 読む・探す | [2. entry と項目](https://jasri-jp.github.io/elabftw-toolkit/manual/02)、[3. 一覧と検索](https://jasri-jp.github.io/elabftw-toolkit/manual/03) |
| 作る・添付する・つなぐ | [4. 作成とテンプレート](https://jasri-jp.github.io/elabftw-toolkit/manual/04)、[5. 添付ファイルと画像](https://jasri-jp.github.io/elabftw-toolkit/manual/05)、[6. リンクとタグ](https://jasri-jp.github.io/elabftw-toolkit/manual/06) |
| 権限・lock・本文の表 | [7. 権限](https://jasri-jp.github.io/elabftw-toolkit/manual/07)、[8. lock とタイムスタンプ](https://jasri-jp.github.io/elabftw-toolkit/manual/08)、[9. HTML の表と Steps](https://jasri-jp.github.io/elabftw-toolkit/manual/09) |
| まとめて処理する・書き出す | [11. よくある作業の手順集](https://jasri-jp.github.io/elabftw-toolkit/manual/11)、[10. MaiML への書き出し](https://jasri-jp.github.io/elabftw-toolkit/manual/10) |
| 安全に書き込む | [15. 確かめと計画](https://jasri-jp.github.io/elabftw-toolkit/manual/15) |
| 関数を調べる・古いスクリプトを直す | [14. API 一覧](https://jasri-jp.github.io/elabftw-toolkit/manual/14) |
| 開発に参加する | [13. テストと開発](https://jasri-jp.github.io/elabftw-toolkit/manual/13) |
| Claude などの AI から使う・AI と一緒に作る | [16. MCP サーバー](https://jasri-jp.github.io/elabftw-toolkit/manual/16)、[17. AI と一緒に作る](https://jasri-jp.github.io/elabftw-toolkit/manual/17) |

## ドキュメントサイト

公開版: **https://jasri-jp.github.io/elabftw-toolkit/** （`main` に push すると GitHub Actions で作り直されます。`.github/workflows/docs.yml`）

同じガイドを、はじめての方向けの説明・チートシート・レシピと一緒にブラウザで読めます（[VitePress](https://vitepress.dev/)、[Node.js](https://nodejs.org/) が要る）。

```bash
npm install
npm run dev      # ガイドを取り込んで http://localhost:5173 で開く
npm run build    # docs/.vitepress/dist/ に HTML を書き出す
```

Windows では **`docs_preview.bat` をダブルクリック**すると、原稿を取り込んでブラウザで開きます（Node.js が要る）。
Google Drive などの同期フォルダの中でも動くように、サイトを `%USERPROFILE%\.cache\elabftw-toolkit-docs` に写して、そこで動かします
（初回だけ VitePress を入れるので少し時間がかかる）。原稿を直したら、もう一度ダブルクリックしてください。

| 内容 | 置き場所 |
| --- | --- |
| 利用者ガイドの原稿（日本語・英語） | `docs/ja`・`docs/en`（章の順は `mkdocs.yml`） |
| トップページ・はじめての方へ・チートシート・レシピ集 | `docs/index.md`・`docs/guide/`・`docs/recipes/`・`docs/en/index.md`・`docs/en/guide/` |
| 図・画像 | `docs/public/` |
| サイトの設定 | `docs/.vitepress/` |

`scripts/sync-manual.mjs` が原稿を章番号の URL（`/manual/04`、`/en/manual/04`）に写し、`examples/` の見本をダウンロードできるように置きます（どちらも生成物なので git には入れない）。
Google Drive などの同期フォルダの中では `node_modules` のリンクを作れず、`npm run dev` / `build` が失敗します。そのときは `docs_preview.bat` を使うか、同期の外に写して動かしてください。

## テスト

```bash
python -m pip install -e ".[maiml]" pytest
pytest    # eLabFTW には接続しない
```

`tests/live/` は実サーバーに書き込む確認用スクリプトで、`pytest` では実行しません。詳しくは [13. テストと開発](https://jasri-jp.github.io/elabftw-toolkit/manual/13) を見てください。

## ライセンス

Apache License 2.0（[LICENSE](https://github.com/jasri-jp/elabftw-toolkit/blob/main/LICENSE)）

このライブラリは eLabFTW の REST API を使うための補助ライブラリとして独自に実装したものです。eLabFTW のサーバー側のソースコードは含まず、改変・再配布もしていません。eLabFTW API の Python クライアント `elabapi-python`（MIT License）と組み合わせて使います。

設計と利用例の検討では、NAIST の eLabFTW 関連リポジトリ [naist-eln/eln](https://github.com/naist-eln/eln) を参考にしましたが、コードをそのまま複製したものではありません。なお、NAIST のリポジトリの `API/` には GNU Affero General Public License v3.0（AGPL-3.0）のライセンスファイルが含まれています。

---

Copyright 2026 Japan Synchrotron Radiation Research Institute (JASRI) and Takahiro Matsumoto
