Skip to content

Commit 3cdf1ac

Browse files
committed
Implement stub callback for node expansion
1 parent c929e33 commit 3cdf1ac

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

bigquery_magics/bigquery.py

+27-14
Original file line numberDiff line numberDiff line change
@@ -596,21 +596,33 @@ def _handle_result(result, args):
596596
return result
597597

598598

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):
610600
return IPython.core.display.JSON(
611601
graph_server.convert_graph_data(query_results=json.loads(params))
612602
)
613603

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+
614626

615627
singleton_server_thread: threading.Thread = None
616628

@@ -628,11 +640,12 @@ def _add_graph_widget(query_result):
628640
# visualizer widget. In colab, we are not able to create an http server on a
629641
# background thread, so we use a special colab-specific api to register a callback,
630642
# to be invoked from Javascript.
631-
if _is_colab():
643+
try:
632644
from google.colab import output
633645

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:
636649
global singleton_server_thread
637650
alive = singleton_server_thread and singleton_server_thread.is_alive()
638651
if not alive:

bigquery_magics/graph_server.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
from typing import Dict, List
2121

2222

23+
def execute_node_expansion(params, request):
24+
return {"error": "Node expansion not yet implemented"}
25+
26+
2327
def convert_graph_data(query_results: Dict[str, Dict[str, str]]):
2428
"""
2529
Converts graph data to the form expected by the visualization framework.
@@ -75,7 +79,7 @@ def convert_graph_data(query_results: Dict[str, Dict[str, str]]):
7579
row_json = json.loads(value_value)
7680
data[column_name].append(row_json)
7781
tabular_data[column_name].append(row_json)
78-
except:
82+
except (ValueError, TypeError):
7983
# Non-JSON columns cannot be visualized, but we still want them
8084
# in the tabular view.
8185
tabular_data[column_name].append(str(value_value))
@@ -113,6 +117,7 @@ class GraphServer:
113117
endpoints = {
114118
"get_ping": "/get_ping",
115119
"post_ping": "/post_ping",
120+
"post_node_expansion": '/post_node_expansion',
116121
"post_query": "/post_query",
117122
}
118123

tests/unit/test_bigquery.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -891,8 +891,8 @@ def test_bigquery_graph_colab(monkeypatch):
891891
graph_visualization is None or bigquery_storage is None,
892892
reason="Requires `spanner-graph-notebook` and `google-cloud-bigquery-storage`",
893893
)
894-
def test_colab_callback():
895-
result = bigquery_magics.bigquery._colab_callback(
894+
def test_colab_query_callback():
895+
result = bigquery_magics.bigquery._colab_query_callback(
896896
"query", json.dumps({"result": {}})
897897
)
898898
assert result.data == {
@@ -905,6 +905,23 @@ def test_colab_callback():
905905
}
906906

907907

908+
@pytest.mark.usefixtures("ipython_interactive")
909+
@pytest.mark.skipif(
910+
graph_visualization is None or bigquery_storage is None,
911+
reason="Requires `spanner-graph-notebook` and `google-cloud-bigquery-storage`",
912+
)
913+
def test_colab_node_expansion_callback():
914+
result = bigquery_magics.bigquery._colab_node_expansion_callback(
915+
request={"uid": "test_uid",
916+
"node_labels": ["label1, label2"],
917+
"node_properites": {},
918+
"direction": "INCOMING",
919+
"edge_label": None},
920+
params_str="{}")
921+
922+
assert result.data == {"error": "Node expansion not yet implemented"}
923+
924+
908925
@pytest.mark.usefixtures("ipython_interactive")
909926
@pytest.mark.skipif(
910927
graph_visualization is not None or bigquery_storage is None,

0 commit comments

Comments
 (0)