1- import os
21import sys
32import json
4-
5- # Universal imports
63import dash_cytoscape as cyto
74from dash import html , dcc
85from dash .dependencies import Input , Output
96
107# Use JupyterDash in Colab, Dash locally
11- try :
12- if "google.colab" in sys .modules :
13- from jupyter_dash import JupyterDash as DashApp
14- else :
15- from dash import Dash as DashApp
16- except ImportError :
17- from dash import Dash as DashApp
8+ if "google.colab" in sys .modules :
9+ from jupyter_dash import JupyterDash
10+ DashApp = JupyterDash
11+ else :
12+ from dash import Dash
13+ DashApp = Dash
1814
1915
20- def render_knowledge_graph (data : dict , save : bool = False , filename : str = "kg.html" ) -> None :
16+ def render_knowledge_graph (data : dict ) -> None :
2117 """
2218 Render an interactive knowledge graph using Dash and Dash Cytoscape.
2319
2420 Args:
2521 data (dict): Knowledge graph data with nodes and edges.
26- save (bool): If True, saves the graph as a static HTML file.
27- filename (str): Output filename for HTML export.
2822
2923 Returns:
3024 None
3125 """
32- data = data ["data" ].get ("knowledge_graphs" , {})
26+ graph_data = data ["data" ].get ("knowledge_graphs" , {})
3327
34- # Convert data to Cytoscape elements
28+ # Convert to Cytoscape elements
3529 elements = []
3630
37- for node in data ["nodes" ]:
31+ for node in graph_data ["nodes" ]:
3832 elements .append ({
3933 "data" : {
4034 "id" : node ["id" ],
@@ -47,7 +41,7 @@ def render_knowledge_graph(data: dict, save: bool = False, filename: str = "kg.h
4741 }
4842 })
4943
50- for edge in data ["edges" ]:
44+ for edge in graph_data ["edges" ]:
5145 elements .append ({
5246 "data" : {
5347 "source" : edge ["fromId" ],
@@ -56,8 +50,8 @@ def render_knowledge_graph(data: dict, save: bool = False, filename: str = "kg.h
5650 }
5751 })
5852
53+ # Build the app
5954 app = DashApp (__name__ )
60-
6155 app .layout = html .Div ([
6256 html .H3 ("Interactive Knowledge Graph" ),
6357 html .Div (id = "node-info" , style = {"margin" : "10px" , "font-size" : "14px" , "color" : "#F58634" }),
@@ -98,12 +92,8 @@ def display_click_info(node_data):
9892 return f"Selected Node: { node_data ['hover_text' ]} "
9993 return "Click on a node to see details"
10094
101- if save :
102- app .run_server (mode = "inline" , debug = False )
103- # Use selenium or dash-export to save actual HTML content if needed
104- print ("Note: Static export not implemented in this version." )
95+ # Show app inline if in Colab, else open in browser
96+ if "google.colab" in sys .modules :
97+ app .run_server (mode = 'inline' , debug = True )
10598 else :
106- if "google.colab" in sys .modules :
107- app .run_server (mode = "inline" , debug = True )
108- else :
109- app .run_server (debug = True )
99+ app .run_server (debug = True )
0 commit comments