From 7a0e2efba175c9ab3035edcd4ac208a14d388ea1 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Wed, 15 Jul 2026 00:18:27 +0200 Subject: [PATCH] Integrate Dash background callbacks to handle computations in async --- main.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/main.py b/main.py index 3735861..2249de6 100644 --- a/main.py +++ b/main.py @@ -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