Skip to main content

Python implementation of LabThings, based on the Flask microframework

Project description

python-labthings

LabThings ReadTheDocs PyPI Code style: black codecov Riot.im

A Python implementation of the LabThings API structure, based on the Flask microframework.

Installation

pip install labthings

Quickstart example

This example assumes a PretendSpectrometer class, which already has data and integration_time attributes, as well as an average_data(n) method. LabThings allows you to easily convert this existing instrument control code into a fully documented, standardised web API complete with auto-discovery and automatic background task threading.

from labthings import fields, create_app
from labthings.example_components import PretendSpectrometer


# Create LabThings Flask app
app, labthing = create_app(
    __name__,
    title="My PretendSpectrometer API",
    description="LabThing API for PretendSpectrometer",
    version="0.1.0"
)


# Make some properties and actions out of our component
my_spectrometer = PretendSpectrometer()

# Single-shot data property
labthing.build_property(
    my_spectrometer,  # Python object
    "data",  # Objects attribute name
    description="A single-shot measurement",
    readonly=True,
    schema=fields.List(fields.Number())
)

# Integration time property
labthing.build_property(
    my_spectrometer,  # Python object
    "integration_time",  # Objects attribute name
    description="Single-shot integration time",
    schema=fields.Int(min=100, max=500, example=200, unit="microsecond")
)

# Averaged measurement action
labthing.build_action(
    my_spectrometer,  # Python object
    "average_data",  # Objects method name
    description="Take an averaged measurement",
    schema=fields.List(fields.Number()),
    args={  # How do we convert from the request input to function arguments?
        "n": fields.Int(description="Number of averages to take", example=5, default=5)
    },
)


# Start the app
if __name__ == "__main__":
    from labthings import Server
    Server(app).run()

Acknowledgements

Much of the code surrounding default response formatting has been liberally taken from Flask-RESTful. The integrated Marshmallow support was inspired by Flask-Marshmallow and Flask-ApiSpec.

Developer notes

Changelog generation

  • npm install -g conventional-changelog-cli
  • conventional-changelog -r 1 --config ./changelog.config.js -i CHANGELOG.md -s

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

labthings-0.8.0rc2.tar.gz (2.5 MB view hashes)

Uploaded Source

Built Distribution

labthings-0.8.0rc2-py3-none-any.whl (2.5 MB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page