Python implementation of LabThings, based on the Flask microframework
Project description
python-labthings
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for labthings-0.8.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 133eb1480dc6ea851faa9a16bcf34d075d513935eac82f8da492e6dc5e336b6c |
|
MD5 | f80bf1cdfa211c0eba84e0950a88d911 |
|
BLAKE2b-256 | 130058a92112453d38554cf0665dae2ff7225c638a247d6cf83b0591bd4aabee |