9. HTML の表と Steps
本文(body)の中の HTML table の読み書きと、Steps(チェックリスト)の操作です。Experiment と Resource item で同じ書き方ができます。
まずはこれだけ
python
tables = elab.experiments.entry(experiment_id).tables # item なら elab.items.entry(item_id).tables
tables.list(debug=True) # 本文中の表の一覧
tables.get_horizontal(table_index=0) # 1 つ目の表(横型)の値
steps = elab.experiments.entry(experiment_id).steps
steps.set_finished_by_body("解析スクリプト", finished=True)HTML table
表の種類
- 横型 table: 1 行目がヘッダー、2 行目以降がデータ。ヘッダー名に対応させて値を入れます。
- 縦型 table: 左の列が key、右の列が value。
横型 table を更新する
python
update_contents = [
{
"課題番号": "2026T0001",
"試料ID": "99",
"備考": "テーブル更新",
},
]
tables.update_horizontal(
update_contents,
table_index=0,
header_row_index=0,
target_row_index=1,
debug=True,
)縦型 table を更新する
python
tables.update_vertical(
update_contents,
table_index=2,
key_col_index=0,
value_col_index=1,
allow_new_keys=False,
debug=True,
)allow_new_keys=True を指定すると、無い key を新しい行として足します。
値を読む
python
horizontal_values = tables.get_horizontal(table_index=0)
vertical_values = tables.get_vertical(table_index=2, value_col_index=1)横型 table をクリアする
update_contents の列数が変わる場合などは、まずヘッダー行と空の値行だけにリセットしてから更新できます。
python
tables.clear_horizontal(table_index=0, header_row_index=0)
tables.update_horizontal(update_contents, table_index=0, header_row_index=0, target_row_index=1)折りたたみ表示にする
python
tables.set_collapsible(
table_index=0,
summary_text="Measurement conditions",
open=False,
)注意
- 表の更新は、本文中の表の順序、ヘッダー行、更新する行、key / value の列の位置に依存します。template の構造を変えたら、
table_index・header_row_index・target_row_index・key_col_index・value_col_indexを確かめてください。
Steps / チェックリスト
Experiment template に Steps を用意しておき、elab.experiments.init(..., copy_template_steps=True)(既定)で作った実験ノートにコピーしたあと、Experiment 側の Steps を進み具合の管理に使う運用を想定しています。
python
steps = elab.experiments.entry(experiment_id).steps
steps_df = steps.list(debug=True) # 一覧
steps.add("解析スクリプトを添付する", finished=False) # 追加
steps.set_finished(step_id=1, finished=True) # チェック ON
steps.set_finished(step_id=1, finished=False) # チェック OFF
steps.update(step_id=1, body="解析スクリプトと結果図を添付する") # 本文を更新
steps.set_finished_by_body("解析スクリプト", finished=True) # 本文の一部で探してチェックResource / Database item の Steps も同じです。
python
item_steps = elab.items.entry(item_id).steps
item_steps.list(debug=True)
item_steps.add("試料情報を確認する")ほかに steps.toggle(step_id)(チェックを反転)、steps.delete(step_id)、steps.replace(step_bodies)(Steps を指定した本文の並びに置き換える)、steps.copy_to(target_entity_id)(別の entry にコピー)、steps.raw() があります。
次は 10. MaiML への書き出し へ。