ServiceData
ClassSource
service = ServiceData(
cache: CacheAbstract[CachedFileInfo, CachedData],
service_name: str = "/cached-data",
chunk_size: int = 1,
)
Service provider for streaming data.
This instance provides a cache for storing temporary data. A temporary data item will be accessed by the directly or downloaded.
Aliases
This class can be acquired by
import dash_file_cache as dfc
dfc.ServiceData
dfc.services.ServiceData
dfc.services.data.ServiceData
Arguments
Argument | Type | Required | |
---|---|---|---|
cache | CacheAbstract[CachedFileInfo, CachedData] | The cache instance hosting the temporary data. This value can be CachePlain , CacheQueue , or CacheFile . | |
service_name | str | The name of this service. | |
chunk_size | int | The chunk size when streaming the cached file to users. The unit is MB . |
Methods
register
addr: str = service.register(
fobj: str | os.PathLike | io.StringIO | io.BytesIO,
file_name: str = "",
content_type: str = "",
mime_type: str = "image/jpeg",
one_time_service: bool = False,
download: bool = False,
)
Register the a new file (a path or a file-like object) to the cache.
Requires
Argument | Type | Required | |
---|---|---|---|
fobj | str | os.PathLike | io.StringIO | io.BytesIO | The path or a file-io object of a specific file to be registered. | |
file_name | str | The file name of
Note that this value is used only when the file will be streamed as a downloadable file. | |
content_type | str | The content-type in the response. An optional encoding can be provided here. If this value is not specified, will use mime_type to fill this value. | |
mime_type | str | The mime-type of the response. Typically, it is only determined by the type of the file. | |
one_time_service | bool | A flag. When it is enabled, will use the on-exit mechanism to remove this cached data once it is served for once. This option is recommended if this cached item is used for one-time downloading. | |
download | bool | If specified, will mark the returned address as a downloadable link. |
Returns
Argument | Type | |
---|---|---|
addr | str | The URL that would be used for accessing this temporarily cached file. |
stream
resp: flask.Response = service.stream(uid: str, download: bool = False)
Wrap a cached data item with streaming data provider.
Note that users do not need to explictly call this method. This method will be automatically called when the related service is accessed.
Requires
Argument | Type | Required | |
---|---|---|---|
uid | str | The UUID used for accessing the cached item. If not hit, raise FileNotFoundError . | |
download | bool | A flag. If enabled, will mark the streamed data as the data to be downloaded. |
Returns
Argument | Type | |
---|---|---|
resp | flask.Response | The flask Response used for forwarding the data to the frontend. |
serve
service.serve(app: dash.Dash | flask.Flask, endpoint: str | None = None)
Add the service to the Flask application.
Requires
Argument | Type | Required | |
---|---|---|---|
app | dash.Dash | flask.Flask | The application where the services will be bound. | |
endpoint | str | None | The endpoint of this serivce. If not specified, will use the service address to infer it. |
Properties
cache
cache: CacheAbstract[CachedFileInfo, CachedData] = service.cache
The cache of this service.
chunk_size
chunk_size: int = cache.chunk_size
new_chunk_size: int
cache.chunk_size = new_chunk_size
The chunk size when streaming the cached file to users. The unit is MB
.
Examples
See