mgbam's picture
Create graph.py
8e7e06c verified
raw
history blame
1.64 kB
## `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))