File size: 1,640 Bytes
8e7e06c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
## `genesis/graph.py`

from __future__ import annotations
import os, html
from typing import List

# Lightweight graph preview from citations (no DB required). If Neo4j is configured, you can extend here.

def build_preview_graph_html(citations: List[dict]) -> str:
    # Simple HTML graph: papers as nodes, edges from a central topic node
    if not citations:
        return "<p><em>No citations to graph.</em></p>"
    nodes = []
    edges = []
    nodes.append({"id": "topic", "label": "Topic", "color": "#0ea5e9"})
    for i, c in enumerate(citations):
        nid = f"p{i}"
        label = (c.get("title") or "citation").strip()[:80]
        url = html.escape(c.get("url", ""))
        nodes.append({"id": nid, "label": label, "url": url})
        edges.append({"from": "topic", "to": nid})
    # Minimal vis.js rendering
    return f"""
    <div id='g' style='height:480px'></div>
    <script src='https://unpkg.com/[email protected]/dist/vis-network.min.js'></script>
    <script>
      const nodes = new vis.DataSet({nodes});
      const edges = new vis.DataSet({edges});
      const container = document.getElementById('g');
      const data = {{ nodes, edges }};
      const options = {{ nodes: {{ shape: 'dot', size: 12 }}, edges: {{ arrows: {{to:false}} }} }};
      const network = new vis.Network(container, data, options);
      network.on('click', function (params) {{
        if(params.nodes && params.nodes.length) {{
          const n = nodes.get(params.nodes[0]);
          if(n.url) window.open(n.url, '_blank');
        }}
      }});
    </script>
    """.replace("{nodes}", str(nodes)).replace("{edges}", str(edges))