Skip to main content
Version: 0.2.0

ServiceData

ClassSource

service = ServiceData(
cache: CacheAbstract[CachedFileInfo, CachedData],
service_name: str = "/cached-data",
chunk_size: int = 1,
allowed_cross_origin: str | None = None,
)

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

ArgumentTypeRequired
Description
cacheCacheAbstract[CachedFileInfo, CachedData]The cache instance hosting the temporary data. This value can be CachePlain, CacheQueue, or CacheFile.
service_namestrThe name of this service.
chunk_sizeintThe chunk size when streaming the cached file to users. The unit is MB.
allowed_cross_originstr | NoneThe allowed cross origin when serving the data. The usage should be the same as "Access-Control-Allow-Origin". If this value is empty or None, the cross-origin will not be configured.

Methods

register_request

addr: str = service.register_request(
url: str | urllib.parse.ParseResult,
headers: Mapping[str, str] | None = None,
file_name_fallback: str = "",
download: bool = False,
)

Register the a remote request to the cache.

Requires

ArgumentTypeRequired
Description
urlstr | urllib.parse.ParseResultThe string-form or parsed URL of the remote file.
headersMapping[str, str] | NoneThe customized headers for this request. If not specified, will not use any headers.
file_name_fallbackstrThe fall-back file name. If specified, it will be used as the saved file name when the remote service does not provide the file name.
downloadboolIf specified, will mark the returned address as a downloadable link.

Returns

ArgumentType
Description
addrstrThe URL that would be used for accessing this temporarily cached file.

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

ArgumentTypeRequired
Description
fobjstr | os.PathLike | io.StringIO | io.BytesIOThe path or a file-io object of a specific file to be registered.
file_namestr

The file name of fobj. If not provided, will attempt to

  1. solve the name from fobj if it is a path.
  2. randomly generate a dummy file name.

Note that this value is used only when the file will be streamed as a downloadable file.

content_typestrThe 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_typestrThe mime-type of the response. Typically, it is only determined by the type of the file.
one_time_servicebool

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.

downloadboolIf specified, will mark the returned address as a downloadable link.

Returns

ArgumentType
Description
addrstrThe 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.

warning

Note that users do not need to explictly call this method. This method will be automatically called when the related service is accessed.

Requires

ArgumentTypeRequired
Description
uidstrThe UUID used for accessing the cached item. If not hit, raise FileNotFoundError.
downloadboolA flag. If enabled, will mark the streamed data as the data to be downloaded.

Returns

ArgumentType
Description
respflask.ResponseThe 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

ArgumentTypeRequired
Description
appdash.Dash | flask.FlaskThe application where the services will be bound.
endpointstr | NoneThe 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 = service.chunk_size

new_chunk_size: int
service.chunk_size = new_chunk_size

The chunk size when streaming the cached file to users. The unit is MB.


allowed_cross_origin

allowed_cross_origin: str = service.allowed_cross_origin

The allowed cross origin. If this value is an empty string, the cross-origin data delivery will not be used.

Examples

See

Change image

Flask services