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="A magic denoise property",
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 0 --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.0rc1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc59384ef2b0be0d537b3c0403e956d2e86297db03c11dcb62abfb2c3c3eadb1 |
|
MD5 | 6d7d5e1cfaf6a44a923072a14f8dfab5 |
|
BLAKE2b-256 | 9214c116fe5bc9cf9de7404d8ea1a0e8580ffe90479c1a9d2dda5c6d235fecfd |