Skip to content

Commit ce96f22

Browse files
committed
Fix visualization in colab.
Problem is that, even though the port is not used in colab mode, the javascript still throws an error if it's undefined.
1 parent 148a74c commit ce96f22

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

bigquery_magics/bigquery.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -640,23 +640,30 @@ def _add_graph_widget(query_result):
640640
# visualizer widget. In colab, we are not able to create an http server on a
641641
# background thread, so we use a special colab-specific api to register a callback,
642642
# to be invoked from Javascript.
643+
port = None
643644
try:
644645
from google.colab import output
645646

646647
output.register_callback("graph_visualization.Query", _colab_query_callback)
647648
output.register_callback(
648649
"graph_visualization.NodeExpansion", _colab_node_expansion_callback
649650
)
651+
652+
# In colab mode, the Javascript doesn't use the port value we pass in, as there is no
653+
# graph server, but it still has to be set to avoid triggering an exception.
654+
# TODO: Clean this up when the Javascript is fixed on the spanner-graph-notebook side.
655+
port = 0
650656
except ImportError:
651657
global singleton_server_thread
652658
alive = singleton_server_thread and singleton_server_thread.is_alive()
653659
if not alive:
654660
singleton_server_thread = graph_server.graph_server.init()
661+
port = graph_server.graph_server.port
655662

656663
# Create html to invoke the graph server
657664
html_content = generate_visualization_html(
658665
query="placeholder query",
659-
port=graph_server.graph_server.port,
666+
port=port,
660667
params=query_result.to_json().replace("\\", "\\\\").replace('"', '\\"'),
661668
)
662669
IPython.display.display(IPython.core.display.HTML(html_content))

0 commit comments

Comments
 (0)