Skip to content

Commit eb9596e

Browse files
committed
Add get_thing_states to action docs
1 parent 7baafe1 commit eb9596e

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

docs/source/actions.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,32 @@ a long time to run, or it may simply be a helpful debugging aid.
6767

6868
See :mod:`.logs` for details of how this is implemented.
6969

70+
.. _get_thing_states:
71+
72+
Collecting metadata
73+
--------------------
74+
It's relatively common to want to collect metadata that summarises all of the `.Thing`
75+
instances in the current server. It's good practice to embed this kind of metadata in
76+
data files or images, and so LabThings provides a convenience function to collect it.
77+
`.ThingServerInterface.get_thing_states` will return a mapping of `.Thing` names to
78+
metadata dictionaries, suitable for embedding in a data file. The metadata dictionaries
79+
are generated by the ``thing_state`` property of each `.Thing`\ .
80+
81+
The metadata can be accessed through the Thing-Server Interface, which is a property
82+
of the `.Thing`\ :
83+
84+
.. code-block:: python
85+
86+
import json
87+
import labthings_fastapi as lt
88+
89+
class MyThing(lt.Thing):
90+
@lt.action
91+
def gather_metadata(self) -> str:
92+
"""Return metadata about all Things, as a string."""
93+
metadata_dict = self._thing_server_interface.get_thing_states()
94+
return json.dumps(metadata_dict)
95+
7096
.. _action_cancellation:
7197

7298
Cancelling actions

0 commit comments

Comments
 (0)