Integrate Dash background callbacks to handle computations in async

This commit is contained in:
2026-07-15 00:18:27 +02:00
parent a2ac49c79f
commit 7a0e2efba1
+48
View File
@@ -1,3 +1,51 @@
# File: main.py
# Copyright (C) 2026 Erick Ahmed
# SPDX-License-Identifier: AGPL-3.0-or-later
import diskcache
import flask_caching
import dash
from dash import Input, Output, State, dcc, html, no_update
import dash_bootstrap_components as dbc
CACHE_DATA_DIR = ".cache_data"
background_callback_manager = dash.DiskcacheManager(cache_dir=CACHE_DATA_DIR)
data_cache = flask_caching.Cache(config={'CACHE_TYPE': 'FileSystemCache', 'CACHE_DIR': CACHE_DATA_DIR})
app = dash.Dash(
__name__,
external_stylesheets=[dbc.themes.BOOTSTRAP],
background_callback_manager=background_callback_manager
)
data_cache.init_app(app.server)
def _get_df(session_data):
if not session_data or "token" not in session_data:
return None
return data_cache.get(session_data["token"])
@app.callback(
Output("graph-correlation", "figure"),
Input("corr-method", "value"),
Input("corr-target", "value"),
Input("session-store", "data"),
background=True,
prevent_initial_call=True,
)
def update_correlation(method, target, session_data):
df = _get_df(session_data)
if df is None or not method:
return no_update
target_id = None if target == "all" else target
try:
corr_df = calculate_correlation(df, method=method, target_id=target_id)
title = f"Inter-Byte Correlation ({method.capitalize()})"
if target_id:
title += f" - {target_id}"
return plot_correlation_heatmap(corr_df, target_id=target_id, title=title)
except Exception as exc:
fig = dash.go.Figure()
fig.update_layout(title=f"Error: {exc}")
return fig