Skip to main content

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

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.

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

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)

Serve the page to the flask app.

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 = 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

Change image

Flask services