Skip to main content

Utilities for providing convenient methods to serve cached data in Plotly-Dash or Flask.

Project description

Dash File Cache

GitHub release (latest SemVer) GitHub all releases GitHub PyPI - Downloads

GitHub Actions (Build) GitHub Actions (Release)

Dash File Cache is a Dash extension library.

Utilities for providing convenient methods to serve cached data in Plotly-Dash or Flask.

The data cache enables the following features:

  1. Load a server-side file dynamically and send the file to users (the frontend).
  2. Support in-memory files (like io.BytesIO()) or on-disk files (specified by a path).
  3. Support different kinds of cache (single-thread, multi-processing, or file-based).
  4. A customized component helping the dashboard trigger a download event from the server side.

1. Install

Intall the latest released version of this package by using the PyPI source:

python -m pip install dash-file-cache

Or use the following commands to install the developing version from the GitHub Source when you have already installed Git :hammer::

git clone https://github.com/cainmagi/dash-file-cache
cd dash-file-cache
python -m pip install -r requirements.txt -r requirements-dev.txt
python -m pip install .

2. Usage

The following codes show a minimal example of this package

from typing import Optional

import io

import dash
from dash import html
from dash import Input, Output

from dash_file_cache import ServiceData, CachePlain

app = dash.Dash("demo")
service = ServiceData(CachePlain(1))
service.serve(app)

app.layout = html.Div(
    (
        html.Div(
            html.P(
                (
                    html.Span("Get Image:", style={"paddingRight": "0.5rem"}),
                    html.Button(id="btn", children="Image"),
                )
            )
        ),
        html.Div((html.P("Cache address:"), html.P(id="addr"))),
        html.Div((html.P("Cached Image:"), html.Img(id="cache"))),
    ),
)


@app.callback(
    Output("addr", "children"),
    Input("btn", "n_clicks"),
    prevent_initial_call=True,
)
def click_get_image(
    n_clicks: Optional[int],
):
    if not n_clicks:
        return dash.no_update

    addr = service.register(
        io.StringIO(
            R'<svg height="100" width="100" xmlns="http://www.w3.org/2000/svg">'
            R'<circle r="45" cx="50" cy="50" fill="red" /></svg>'
        ),
        content_type="image/svg+xml",
        mime_type="image/svg+xml",
        one_time_service=True,
    )
    return addr


@app.callback(
    Output("cache", "src"),
    Input("addr", "children"),
    prevent_initial_call=True,
)
def update_cache(addr):
    if not addr:
        return dash.no_update
    return addr


if __name__ == "__main__":
    app.run(host="127.0.0.1", port="8080", debug=True)

Check http://127.0.0.1:8080 to see the following results:

The minimal demo
pic-demo-minimal

3. Documentation

Check the documentation to find more details about the examples and APIs.

https://cainmagi.github.io/dash-file-cache/

4. Contributing

See CONTRIBUTING.md :book:

5. Changelog

See Changelog.md :book:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dash_file_cache-0.1.2.tar.gz (158.4 kB view details)

Uploaded Source

Built Distribution

dash_file_cache-0.1.2-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

Details for the file dash_file_cache-0.1.2.tar.gz.

File metadata

  • Download URL: dash_file_cache-0.1.2.tar.gz
  • Upload date:
  • Size: 158.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for dash_file_cache-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c4e2afd65412f07fa513f7adbe64913eab3e76617c0fd255e60283f46b4adb0f
MD5 9e8dff269e33bf1f652ca177ecd66aef
BLAKE2b-256 22f6b7f83642986f9f62b88e36774495b9ccdd457b35f86d0b9a5ee04b610035

See more details on using hashes here.

File details

Details for the file dash_file_cache-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for dash_file_cache-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2e658a01ec0e8fe94aabdbaf455b7955b64018e5a6b23e30c64bafa34822b96f
MD5 ee5d576aca9b81f7652d3115faf7787f
BLAKE2b-256 c9349341e38019a4f41f0337bca67f04122d982db0e0af47e7e42df0dda74789

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page