Documentation for LabThings-FastAPI

labthings-fastapi implements a Web of Things interface for laboratory hardware using Python. This is a ground-up rewrite of python-labthings, replacing Flask 1 and Marshmallow with FastAPI and Pydantic. It is the underlying framework for v3 of the OpenFlexure Microscope software.

labthings-fastapi aims to simplify the process of making laboratory instruments available via an HTTP API. Key features and design aims are below:

  • Functionality together in Thing subclasses, which represent units of hardware or software (see Web of Things Core Concepts)

  • Methods and properties of Thing subclasses may be added to the HTTP API and Thing Description using decorators

  • Vocabulary and concepts are aligned with the W3C Web of Things standard (see Web of Things Core Concepts)
    • Things are classes, with properties and actions defined exactly once

    • Thing Descriptions are automatically generated, and validated with pydantic

    • OpenAPI documentation is automatically generated by FastAPI

  • We follow FastAPI’s lead and try to use standard Python features to minimise unnecessary code
    • Datatypes of action input/outputs and properties are defined with Python type hints

    • Actions are defined exactly once, as a method of a Thing class

    • Properties and actions are declared using decorators (or descriptors if that’s preferred)

    • FastAPI “Dependency injection” is used to manage relationships between Things and dependency on the server

  • Lifecycle and concurrency are appropriate for hardware: Thing code is always run in a thread, and each Thing is instantiated and shut down only once.
    • Starlette (used by FastAPI) can handle requests asynchronously - this improves performance and enables websockets and other long-lived connections.

    • Thing code is still, for now, threaded. In the future it may become possible to us other concurrency models in Thing code.

Compared to python-labthings, this framework updates dependencies, shrinks the codebase, and simplifies the API (see LabThings Core Concepts). * FastAPI more or less completely eliminates OpenAPI generation code from our codebase * Marshmallow schemas and endpoint classes are replaced with Python type hints, eliminating double- or triple-definition of actions and their inputs/outputs. * Thing Description generation is very much simplified by the new structure (multiple Things instead of one massive Thing with many extensions)

Installation

pip install labthings-fastapi

Indices and tables