Files
cowork-local/core/d3_graph.py
T
f9f6bc01fd
CI / test (push) Canceled after 0s
Feature/delta team/epic r04 (#7)
## Summary

epic r04 - begin refactor

## Change Type

- [x] Cowork feature
- [ ] Bug fix
- [ ] Core AI contribution
- [ ] Test / hardening
- [ ] Performance
- [ ] Documentation

## Related Work

Cowork Task:

Core Repo: http://34.143.229.138/gitea-admin/fsg-ai-core-assets

Core AI Issue:

Core Task:

Related PR:

## Scope

What is intentionally included?

What is intentionally NOT included?

## Validation

- [ ] Unit tests
- [ ] Integration tests
- [ ] Manual verification
- [ ] Regression check

Commands / evidence:

## Security Impact

Permission / credential / network / customer data impact:

## Compatibility

- [ ] No breaking change
- [ ] Breaking change documented

## Reviewer Notes

Anything Cowork reviewers should pay attention to.

---------

Co-authored-by: Anh Tran Nguyen Minh <anhtnm1@fpt.com>
Co-authored-by: Huong Le Thi Thien <huongltt35@fpt.com>
Co-authored-by: Nam Pham Dinh Thanh <nampdt@fpt.com>
Co-authored-by: Vu Dam Tuan <vudt15@fpt.com>
Co-authored-by: Hiep Ha Van <hiephv3@fpt.com>
Co-authored-by: Lam Hoang Van <lamhv7@fpt.com>
Reviewed-on: #7
Co-authored-by: Duy Le Huu <duylh19@fpt.com>
2026-08-31 05:15:13 +00:00

47 lines
1.7 KiB
Python

"""Render a StructureGraph into the bundled D3 knowledge-graph template.
The template (assets/graph_template.html) contains a ``GRAPH_DATA_PLACEHOLDER``
and a ``COLOR_MAP`` object; we inject our nodes/edges and the kind→colour map.
"""
from __future__ import annotations
import json
import re
from pathlib import Path
from .structure_graph import NODE_KIND_COLORS
TEMPLATE = Path(__file__).resolve().parent.parent / "assets" / "graph_template.html"
_CDN_D3 = '<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.8.5/d3.min.js"></script>'
def build_html(graph) -> str:
"""Dựng trang HTML D3 cho đồ thị: nhét dữ liệu node/cạnh vào bản mẫu."""
html = TEMPLATE.read_text(encoding="utf-8")
# Inline a bundled d3 (offline) if present; else keep the CDN reference.
d3_local = TEMPLATE.parent / "d3.min.js"
if d3_local.exists():
try:
d3_src = d3_local.read_text(encoding="utf-8")
html = html.replace(_CDN_D3, f"<script>{d3_src}</script>")
except OSError:
pass
nodes = [{"id": n.id, "label": n.label, "type": n.kind,
"description": n.detail, "path": getattr(n, "path", "")}
for n in graph.nodes]
links = [{"source": e.source, "target": e.target, "label": e.type}
for e in graph.edges]
data_js = json.dumps({"nodes": nodes, "links": links}, ensure_ascii=False)
data_js = data_js.replace("</", "<\\/") # never break the <script> block
html = html.replace("GRAPH_DATA_PLACEHOLDER", data_js)
color_js = "const COLOR_MAP = " + json.dumps(NODE_KIND_COLORS, ensure_ascii=False) + ";"
html = re.sub(r"const COLOR_MAP = \{.*?\};", lambda _m: color_js, html,
count=1, flags=re.DOTALL)
return html