labthings_fastapi.outputs.blob
BLOB Output Module
The BlobOutput class is used when you need to return something file-like that can’t easily (or efficiently) be converted to JSON. This is useful for returning large objects like images, especially where an existing file-type is the obvious way to handle it.
To return a file from an action, you should declare its return type as a BlobOutput
subclass, defining the media_type attribute.
The output from the class should be an instance of that subclass, with data supplied
either as a bytes object or a file on disk. If files are used, it’s your
responsibility to ensure the file is deleted after the BlobOutput object is
garbage-collected. Constructing it using the class methods from_bytes or
from_temporary_directory will ensure this is done for you.
Bear in mind a tempfile object only holds a file descriptor and is not safe for
concurrent use: action outputs may be retrieved multiple times after the action has
completed. Creating a temp folder and making a file inside it is the safest way to
deal with this.
Module Contents
Classes
A Protocol for a BlobOutput object |
|
A BlobOutput protocol for server-side use, i.e. including |
|
A BlobOutput that holds its data in memory as a |
|
A BlobOutput that holds its data in a file |
|
An output from LabThings best returned as binary data, not JSON |
|
A class to manage BlobData objects |
Functions
Create a BlobOutput subclass for a given media type |
|
Set context variables to allow blobs to be serialised |
Data
API
- class labthings_fastapi.outputs.blob.BlobData
Bases:
typing_extensions.ProtocolA Protocol for a BlobOutput object
- class labthings_fastapi.outputs.blob.ServerSideBlobData
Bases:
labthings_fastapi.outputs.blob.BlobData,typing_extensions.ProtocolA BlobOutput protocol for server-side use, i.e. including
response()- response() fastapi.responses.Response
- class labthings_fastapi.outputs.blob.BlobBytes(data: bytes, media_type: str)
A BlobOutput that holds its data in memory as a
bytesobjectInitialization
- response() fastapi.responses.Response
- class labthings_fastapi.outputs.blob.BlobFile(file_path: str, media_type: str, **kwargs)
A BlobOutput that holds its data in a file
Only the filepath is retained by default. If you are using e.g. a temporary directory, you should add the temporary directory as a property, to stop it being garbage collected.
Initialization
- response() fastapi.responses.Response
- class labthings_fastapi.outputs.blob.Blob
Bases:
pydantic.BaseModelAn output from LabThings best returned as binary data, not JSON
This may be instantiated either using the class methods
from_bytesorfrom_temporary_directory, which will use abytesobject to store the output, or return a file on disk in a temporary directory. In the latter case, the temporary directory will be deleted when the object is garbage collected.- description: str
‘The output from this action is not serialised to JSON, so it must be retrieved as a file. This link …’
- retrieve_data()
- property data: labthings_fastapi.outputs.blob.ServerSideBlobData
- property content: bytes
Return the the output as a
bytesobjectThis property may return the
bytesobject, or if we have a file it will read the file and return the contents. Client objects may use this property to download the output.This property is read-only. You should also only read it once, as no guarantees are given about cacheing - reading it many times risks reading the file from disk many times, or re-downloading an artifact.
- save(filepath: str) None
Save the output to a file.
This may remove the need to hold the output in memory.
- classmethod from_temporary_directory(folder: tempfile.TemporaryDirectory, file: str) typing_extensions.Self
Create a BlobOutput from a file in a temporary directory
The TemporaryDirectory object will persist as long as this BlobOutput does, which will prevent it from being cleaned up until the object is garbage collected.
- classmethod from_file(file: str) typing_extensions.Self
Create a BlobOutput from a regular file
The file should exist for at least as long as the BlobOutput does; this is assumed to be the case and nothing is done to ensure it’s not temporary. If you are using temporary files, consider creating your Blob with
from_temporary_directoryinstead.
- response()
“Return a suitable response for serving the output
- labthings_fastapi.outputs.blob.blob_type(media_type: str) type[labthings_fastapi.outputs.blob.Blob]
Create a BlobOutput subclass for a given media type
- class labthings_fastapi.outputs.blob.BlobDataManager
A class to manage BlobData objects
The BlobManager is responsible for serving
Blobobjects to clients. It holds weak references: it will not retainBlobs that are no longer in use. MostBlobs will be retainedInitialization
- _blobs: weakref.WeakValueDictionary[uuid.UUID, labthings_fastapi.outputs.blob.ServerSideBlobData]
None
- add_blob(blob: labthings_fastapi.outputs.blob.ServerSideBlobData) uuid.UUID
Add a BlobOutput to the manager
- get_blob(blob_id: uuid.UUID) labthings_fastapi.outputs.blob.ServerSideBlobData
Retrieve a BlobOutput from the manager
- attach_to_app(app: fastapi.FastAPI)
Attach the BlobDataManager to a FastAPI app
- labthings_fastapi.outputs.blob.blobdata_to_url_ctx
‘(…)’
- labthings_fastapi.outputs.blob.url_to_blobdata_ctx
‘(…)’
- async labthings_fastapi.outputs.blob.blob_serialisation_context_manager(request: fastapi.Request)
Set context variables to allow blobs to be serialised
- labthings_fastapi.outputs.blob.BlobIOContextDep: typing_extensions.TypeAlias
None