@@ -596,21 +596,33 @@ def _handle_result(result, args):
596
596
return result
597
597
598
598
599
- def _is_colab () -> bool :
600
- """Check if code is running in Google Colab"""
601
- try :
602
- import google .colab # noqa: F401
603
-
604
- return True
605
- except ImportError :
606
- return False
607
-
608
-
609
- def _colab_callback (query : str , params : str ):
599
+ def _colab_query_callback (query : str , params : str ):
610
600
return IPython .core .display .JSON (
611
601
graph_server .convert_graph_data (query_results = json .loads (params ))
612
602
)
613
603
604
+ def _colab_node_expansion_callback (request : dict , params_str : str ):
605
+ """Handle node expansion requests in Google Colab environment
606
+
607
+ Args:
608
+ request: A dictionary containing node expansion details including:
609
+ - uid: str - Unique identifier of the node to expand
610
+ - node_labels: List[str] - Labels of the node
611
+ - node_properties: List[Dict] - Properties of the node with key, value, and type
612
+ - direction: str - Direction of expansion ("INCOMING" or "OUTGOING")
613
+ - edge_label: Optional[str] - Label of edges to filter by
614
+ params_str: A JSON string containing connection parameters
615
+
616
+ Returns:
617
+ JSON: A JSON-serialized response containing either:
618
+ - The query results with nodes and edges
619
+ - An error message if the request failed
620
+ """
621
+ try :
622
+ return IPython .core .display .JSON (graph_server .execute_node_expansion (params_str , request ))
623
+ except BaseException as e :
624
+ return IPython .core .display .JSON ({"error" : e })
625
+
614
626
615
627
singleton_server_thread : threading .Thread = None
616
628
@@ -628,11 +640,12 @@ def _add_graph_widget(query_result):
628
640
# visualizer widget. In colab, we are not able to create an http server on a
629
641
# background thread, so we use a special colab-specific api to register a callback,
630
642
# to be invoked from Javascript.
631
- if _is_colab () :
643
+ try :
632
644
from google .colab import output
633
645
634
- output .register_callback ("graph_visualization.Query" , _colab_callback )
635
- else :
646
+ output .register_callback ("graph_visualization.Query" , _colab_query_callback )
647
+ output .register_callback ("graph_visualization.NodeExpansion" , _colab_node_expansion_callback )
648
+ except ImportError :
636
649
global singleton_server_thread
637
650
alive = singleton_server_thread and singleton_server_thread .is_alive ()
638
651
if not alive :
0 commit comments