Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions bigquery_magics/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,9 +680,11 @@ def _add_graph_widget(query_result):
try:
from google.colab import output

output.register_callback("graph_visualization.Query", _colab_query_callback)
output.register_callback(
"graph_visualization.NodeExpansion", _colab_node_expansion_callback
"bigquery.graph_visualization.Query", _colab_query_callback
)
output.register_callback(
"bigquery.graph_visualization.NodeExpansion", _colab_node_expansion_callback
)

# In colab mode, the Javascript doesn't use the port value we pass in, as there is no
Expand All @@ -703,6 +705,13 @@ def _add_graph_widget(query_result):
port=port,
params=query_result.to_json().replace("\\", "\\\\").replace('"', '\\"'),
)
html_content = html_content.replace(
'"graph_visualization.Query"', '"bigquery.graph_visualization.Query"'
)
html_content = html_content.replace(
Comment on lines 706 to +711

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The callback names are hardcoded here and also when registering the callbacks on lines 683 and 685. To improve maintainability and prevent potential bugs if these names need to be changed in the future, it would be better to define these strings as constants at the module level and reuse them in all relevant places.

For example:

# At module level
_BQ_GRAPH_QUERY_CALLBACK = "bigquery.graph_visualization.Query"
_BQ_GRAPH_NODE_EXPANSION_CALLBACK = "bigquery.graph_visualization.NodeExpansion"
_SPANNER_GRAPH_QUERY_CALLBACK = "graph_visualization.Query"
_SPANNER_GRAPH_NODE_EXPANSION_CALLBACK = "graph_visualization.NodeExpansion"

# In _add_graph_widget()
...
output.register_callback(_BQ_GRAPH_QUERY_CALLBACK, _colab_query_callback)
...
html_content = html_content.replace(
    f'\"{_SPANNER_GRAPH_QUERY_CALLBACK}\"', f'\"{_BQ_GRAPH_QUERY_CALLBACK}\"'
)
...

'"graph_visualization.NodeExpansion"',
'"bigquery.graph_visualization.NodeExpansion"',
)
IPython.display.display(IPython.core.display.HTML(html_content))


Expand Down
Loading