You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/blobs.rst
+26-2Lines changed: 26 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,12 +14,12 @@ A `.Blob` consists of some data and a MIME type, which sets how the data should
14
14
Creating and using `.Blob` objects
15
15
------------------------------------------------
16
16
17
-
Blobs can be created from binary data that is in memory (a `bytes` object) with `.Blob.from_bytes`, on disk (with `.Blob.from_temporary_directory` or `.Blob.from_file`). A `.Blob` may also point to remote data (see `.Blob.from_url`). Code that uses a `.Blob` should not need to know how the data is stored, as the interface is the same in each case.
17
+
Blobs can be created from binary data that is in memory (a `bytes` object) with `.Blob.from_bytes` or on disk (with `.Blob.from_temporary_directory` or `.Blob.from_file`). A `.Blob` may also point to remote data (see `.Blob.from_url`). Code that uses a `.Blob` should not need to know how the data is stored, as the interface is the same in each case.
18
18
19
19
Blobs offer three ways to access their data:
20
20
21
21
* A `bytes` object, obtained via the `.Blob.data` property. For blobs created with a `bytes` object, this simply returns the original data object with no copying. If the data is stored in a file, the file is opened and read when the `.Blob.data` property is accessed. If the `.Blob` references a URL, it is retrieved and returned when `.Blob.data` is accessed.
22
-
* An `.Blob.open` method providing a file-like object. This returns a `~io.BytesIO` wrapper if the `.Blob` was created from a `bytes` object or the file if the data is stored on disk. URLs are retrieved, stored as `bytes` and returned wrapped in a :class:`~io.BytesIO` object.
22
+
* An `~.Blob.open` method providing a file-like object. This returns a `~io.BytesIO` wrapper if the `.Blob` was created from a `bytes` object or the file if the data is stored on disk. URLs are retrieved, stored as `bytes` and returned wrapped in a :class:`~io.BytesIO` object.
23
23
* A `.Blob.save` method will either save the data to a file, or copy the existing file on disk. This should be more efficient than loading `.Blob.data` and writing to a file, if the `.Blob` is pointing to a file rather than data in memory.
24
24
25
25
The intention here is that `.Blob` objects may be used identically with data in memory or on disk or even at a remote URL, and the code that uses them should not need to know which is the case.
@@ -59,6 +59,26 @@ The corresponding client code might look like this:
59
59
img = Image.open(f)
60
60
img.show() # This will display the image in a window
61
61
62
+
Another `.Thing` on the same server can also make use of this action, in which case the image data will not be copied:
63
+
64
+
.. code-block:: python
65
+
66
+
from datetime import datetime
67
+
import os.path
68
+
69
+
classGallery(lt.Thing):
70
+
_camera: Camera = lt.thing_slot()
71
+
72
+
@lt.action
73
+
defcapture_to_gallery(self) -> str:
74
+
"""Capture an image and save it in the gallery."""
When the ``JPEGBlob`` is returned by the camera, nothing is copied - and when it is saved, the `bytes` object is written to disk. Note that, if the data had been backed by a file, ``save`` would have copied the file on disk, with no requirement for Python to load it back into memory.
81
+
62
82
Using `.Blob` objects as inputs
63
83
--------------------------------------
64
84
@@ -119,6 +139,10 @@ On the client, we can use the `capture_image` action directly (as before), or we
119
139
120
140
raw_blob.save("raw_image.raw") # Download and save the raw image to a file
121
141
142
+
.. warning::
143
+
144
+
Currently, `.Blob` objects are only retained on the server as part of the output of the action that created them. This means they will expire after a timeout (by default, a few minutes). This mechanism is likely to change in future releases: the use of `.Blob` objects as inputs should be considered experimental.
0 commit comments