MHCXGraph.utils.dashboard_writer

Constant-memory writer for the self-contained MHCXGraph dashboard.

The original create_master_dashboard built the entire master_export dict, serialized it in one json.dumps, and injected it into a template via a chain of ~20 str.replace calls. At the peak that held, simultaneously: the full dict, its JSON string (a second full copy as text), and the giant HTML string being reallocated on every replace. Peak RAM therefore scaled with the total data and with pair count.

This module keeps the output byte-for-byte equivalent and still fully self-contained (everything embedded in one HTML), but never materializes the whole graph payload at once:

  • Each pair is streamed to its own temp file during the association loop and dropped from memory immediately (see app.run_*_mode).

  • The HTML is assembled as an ordered sequence of segments written directly to the output file. The huge graph_data object is emitted incrementally: static header fields, then each pair read back from its temp file one at a time, then the deduplicated filtered_graphs. No single giant string, no repeated full-string copies, and at most one pair resident at a time.

The JS still receives exactly the same masterData object shape it did before, so no frontend change is required.

class MHCXGraph.utils.dashboard_writer.DashboardStreamWriter(assets_dir, log)[source]

Bases: object

Assemble the dashboard HTML with constant memory.

Usage

writer = DashboardStreamWriter(assets_dir, log) writer.write(

output_path=…, file_name=…, header={…}, # top-level scalar/small fields of masterData pair_files=[(key, path)], # each path holds one pair’s JSON object filtered_graphs={…}, # deduped per-protein filtered graphs actual_mode=…,

)

write(output_path, file_name, header, pair_files, filtered_graphs, actual_mode=None)[source]

Stream the full dashboard to disk with constant memory.

Parameters:
  • header (dict) – Top-level masterData fields (mode, run_name, metadata, proteins, protein_paths, actual_mode, reference_structure…). Small.

  • pair_files (list[tuple[str, str|Path]]) – (pair_key, path) where each path holds one pair’s JSON object.

  • filtered_graphs (dict) – Deduplicated per-protein filtered graph payloads. Modest.

Classes

DashboardStreamWriter(assets_dir, log)

Assemble the dashboard HTML with constant memory.