Scripts and notebooks
Everything you can click on the dashboard, a Python script can do too, at the same time: the dashboard shows what the script changes.
Install the package
Section titled “Install the package”pip install laborchestraIt needs Python 3.11 or newer and has two small dependencies (websockets and msgpack). Install it wherever your scripts run: in a virtual environment, in Jupyter, in conda.
Connect
Section titled “Connect”On the lab PC, connect() finds the server by itself:
import laborchestra
lab = laborchestra.connect()print(lab.devices) # ['vacuum_gauge', ...]From another computer, give the address of the lab PC and the passphrase. This needs network access (see Next steps):
import os
lab = laborchestra.connect("https://lab-pc:5443", os.environ["LABORCHESTRA_PASSPHRASE"])Keep the passphrase out of the script (an environment variable, or a file that is not in git). The computer also has to trust the server’s certificate once: laborchestra trust (see Network access).
Read the state
Section titled “Read the state”Every device is an attribute of lab, and .state is its live state, the same that the dashboard shows:
gauge = lab.vacuum_gaugegauge.state["channels"][1]["pressure"] # 1.68e-08gauge.state["channels"][1]["name"] # 'Chamber'gauge.status # 'ready'.state costs no round trip: the server pushes every change, and your script always has the newest values.
Call actions
Section titled “Call actions”Actions are methods of the device. A call returns when the device is done, and its effects are already in .state:
lab.stage.set_position(0, 45.0) # returns when the stage is at 45°lab.stage.state["channels"][0]["actual_position"] # 45.0If an action fails, you get a Python exception with the device’s message, as if the device were a local object.
Wait for something
Section titled “Wait for something”lab.vacuum_gauge.wait_for(lambda s: s["channels"][1]["pressure"] < 1e-6, timeout=3600)wait_for returns as soon as the condition holds, without polling, and raises TimeoutError when the time is up.
Record from a script
Section titled “Record from a script”import time
with lab.recording("scan", tags={"sample": "A"}): for angle in range(0, 90, 5): lab.stage.set_position(0, angle) time.sleep(10)The recording stops when the block ends, also when the script fails. Every change the script makes is recorded with the script as its cause.
From script to procedure
Section titled “From script to procedure”A script runs as long as your notebook or terminal does. When a measurement should be started from the dashboard, show its progress, and be paused or aborted safely, turn it into a procedure: an async function in the procedures/ folder of the workspace. The dashboard makes a form from its parameters.
from laborchestra import procedure
@procedure(devices=["stage"])async def angle_scan(lab, angles: list[float], duration_s: float = 10): """Records at several angles of the stage.""" for angle in lab.progress(angles): await lab.stage.set_position(0, angle) async with lab.recording(f"angle {angle}", tags={"angle": angle}): await lab.sleep(duration_s)In a script, lab.recording(...) is a normal with block and device calls simply wait. A procedure is async instead: its lab has the same names, but calls are written await lab.stage.set_position(...) and async with lab.recording(...), so that pause and abort can take effect between the steps.
It appears on the Procedures page at once. Scripts can start it too: lab.start_procedure("angle_scan", angles=[0, 45, 90]).
Next: next steps.
Something wrong or missing on this page? Open a GitHub issue or use the form (no account needed).