class DenoiseProperty(PropertyView): schema = fields.Integer( required=True, example=200, minimum=100, maximum=500, description="Value of magic_denoise", ) # Main function to handle GET requests (read) def get(self): """Show the current magic_denoise value""" # When a GET request is made, we'll find our attached component my_component = find_component("org.labthings.example.mycomponent") return my_component.magic_denoise # Main function to handle POST requests (write) def post(self, new_property_value): """Change the current magic_denoise value""" # Find our attached component my_component = find_component("org.labthings.example.mycomponent") # Apply the new value my_component.magic_denoise = new_property_value return my_component.magic_denoise
class MeasurementAction(ActionView): # Expect JSON parameters in the request body. # Pass to post function as dictionary argument. args = { "averages": fields.Integer( missing=20, example=20, description="Number of data sets to average over", ) } # Marshal the response as a list of numbers schema = fields.List(fields.Number) # Main function to handle POST requests @op.invokeaction def post(self, args): """Start an averaged measurement""" logging.warning("Starting a measurement") # Find our attached component my_component = find_component("org.labthings.example.mycomponent") # Get arguments and start a background task n_averages = args.get("averages") logging.warning("Finished a measurement") # Return the task information return my_component.average_data(n_averages)
class TestSchema(Schema): nested = fields.Nested(TestNestedSchema, metadata={ "description": "Nested 1", "title": "Title1" }) yourfield_nested = fields.Integer(required=True)
class ExtensionMeasurementAction(ActionView): """Start an averaged measurement""" args = { "averages": fields.Integer( missing=10, example=10, description="Number of data sets to average over", ) } def post(self, args): # Find our attached component my_component = find_component("org.labthings.example.mycomponent") # Get arguments and start a background task n_averages = args.get("averages") return my_component.average_data(n_averages)
class UserSchema(Schema): name = fields.String(required=True, validate=validate.Length(min=1, max=255)) age = fields.Float() created = fields.DateTime() created_formatted = fields.DateTime(format="%Y-%m-%d", attribute="created", dump_only=True) created_iso = fields.DateTime(format="iso", attribute="created", dump_only=True) updated = fields.DateTime() species = fields.String(attribute="SPECIES") id = fields.String(default="no-id") homepage = fields.Url() email = fields.Email() balance = fields.Decimal() registered = fields.Boolean() hair_colors = fields.List(fields.Raw) sex_choices = fields.List(fields.Raw) finger_count = fields.Integer() uid = fields.UUID() time_registered = fields.Time() birthdate = fields.Date() since_created = fields.TimeDelta() sex = fields.Str(validate=validate.OneOf( choices=["male", "female", "non_binary", "other"], labels=["Male", "Female", "Non-binary/fluid", "Other"], )) various_data = fields.Dict() addresses = fields.Nested(Address, many=True, validate=validate.Length(min=1, max=3)) github = fields.Nested(GithubProfile) const = fields.String(validate=validate.Length(equal=50)) bytestring = fields.Bytes()
class TestSchema(Schema): myfield = fields.String(metadata={"foo": "Bar"}) yourfield = fields.Integer(required=True, baz="waz")
class Address(Schema): id = fields.String(default="no-id") street = fields.String(required=True) number = fields.String(required=True) city = fields.String(required=True) floor = fields.Integer(validate=validate.Range(min=1, max=4))
class SchemaNoMax(Schema): foo = fields.Integer(validate=validate.Range(min=0))
class SchemaNoMin(Schema): foo = fields.Integer(validate=validate.Range(max=4))
class TestSchema(Schema): foo = fields.Integer(validate=validate.Range( min=1, min_inclusive=False, max=3, max_inclusive=False)) bar = fields.Integer(validate=validate.Range(min=2, max=4))
class TestSchema(Schema): id = fields.Integer(required=True) readonly_fld = fields.String(allow_none=True)
class TestSchema(Schema): id = fields.Integer(required=True) readonly_fld = fields.String(dump_only=True)
class InnerSchema(Schema): foo = fields.Integer(required=True)
class TestSchema(Schema): foo2 = fields.Integer(required=True) nested = fields.Nested("TestNamedNestedSchema")
class TestNamedNestedSchema(Schema): foo = fields.Integer(required=True)
class TestSchema(Schema): foo = fields.Integer( validate=validate.OneOf(mapping.values(), labels=mapping.keys()))
class TestNestedSchema(Schema): myfield = fields.String(description="Brown Cow") yourfield = fields.Integer(required=True)