Spaces:
Sleeping
Sleeping
Create graph.py
Browse files- genesis/graph.py +40 -0
genesis/graph.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## `genesis/graph.py`
|
2 |
+
|
3 |
+
from __future__ import annotations
|
4 |
+
import os, html
|
5 |
+
from typing import List
|
6 |
+
|
7 |
+
# Lightweight graph preview from citations (no DB required). If Neo4j is configured, you can extend here.
|
8 |
+
|
9 |
+
def build_preview_graph_html(citations: List[dict]) -> str:
|
10 |
+
# Simple HTML graph: papers as nodes, edges from a central topic node
|
11 |
+
if not citations:
|
12 |
+
return "<p><em>No citations to graph.</em></p>"
|
13 |
+
nodes = []
|
14 |
+
edges = []
|
15 |
+
nodes.append({"id": "topic", "label": "Topic", "color": "#0ea5e9"})
|
16 |
+
for i, c in enumerate(citations):
|
17 |
+
nid = f"p{i}"
|
18 |
+
label = (c.get("title") or "citation").strip()[:80]
|
19 |
+
url = html.escape(c.get("url", ""))
|
20 |
+
nodes.append({"id": nid, "label": label, "url": url})
|
21 |
+
edges.append({"from": "topic", "to": nid})
|
22 |
+
# Minimal vis.js rendering
|
23 |
+
return f"""
|
24 |
+
<div id='g' style='height:480px'></div>
|
25 |
+
<script src='https://unpkg.com/[email protected]/dist/vis-network.min.js'></script>
|
26 |
+
<script>
|
27 |
+
const nodes = new vis.DataSet({nodes});
|
28 |
+
const edges = new vis.DataSet({edges});
|
29 |
+
const container = document.getElementById('g');
|
30 |
+
const data = {{ nodes, edges }};
|
31 |
+
const options = {{ nodes: {{ shape: 'dot', size: 12 }}, edges: {{ arrows: {{to:false}} }} }};
|
32 |
+
const network = new vis.Network(container, data, options);
|
33 |
+
network.on('click', function (params) {{
|
34 |
+
if(params.nodes && params.nodes.length) {{
|
35 |
+
const n = nodes.get(params.nodes[0]);
|
36 |
+
if(n.url) window.open(n.url, '_blank');
|
37 |
+
}}
|
38 |
+
}});
|
39 |
+
</script>
|
40 |
+
""".replace("{nodes}", str(nodes)).replace("{edges}", str(edges))
|