Skip to content

Commit b61d478

Browse files
authored
Merge pull request #66 from wey-gu/fix_ng_draw_raw_resultset
feat: ng_draw support raw result style
2 parents f0b2fdf + 854fcd9 commit b61d478

File tree

3 files changed

+53
-39
lines changed

3 files changed

+53
-39
lines changed

ngql/magic.py

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from nebula3.gclient.net import ConnectionPool as NebulaConnectionPool
2424
from nebula3.Config import Config as NebulaConfig
2525
from nebula3.Config import SSL_config
26+
from nebula3.data.ResultSet import ResultSet
2627

2728
from ngql.ng_load import ng_load
2829
from ngql.types import LoadDataArgsModel
@@ -315,7 +316,7 @@ def _remember_space(self, result):
315316
if last_space_used != "":
316317
self.space = last_space_used
317318

318-
def _stylized(self, result, style=None):
319+
def _stylized(self, result: ResultSet, style=None):
319320
style = style or self.ngql_result_style
320321
if style == STYLE_PANDAS:
321322
try:
@@ -423,6 +424,44 @@ def _help_info():
423424
fancy_print(help_info, color="green")
424425
return
425426

427+
def _draw_graph(self, g: Any) -> Any:
428+
try:
429+
from IPython.display import display, IFrame, HTML
430+
431+
# import get_ipython
432+
from IPython import get_ipython
433+
except ImportError:
434+
raise ImportError("Please install IPython to draw the graph")
435+
436+
g.repulsion(
437+
node_distance=90,
438+
central_gravity=0.2,
439+
spring_length=200,
440+
spring_strength=0.05,
441+
damping=0.09,
442+
)
443+
# g.show_buttons(filter_='physics')
444+
# return g.show("nebulagraph.html", notebook=True)
445+
cell_num = get_ipython().execution_count
446+
graph_render_filename = f"nebulagraph_cell_{cell_num}.html"
447+
g_html_string = g.generate_html(graph_render_filename)
448+
with open(graph_render_filename, "w", encoding="utf-8") as f:
449+
f.write(g_html_string)
450+
# detect if we are in colab or not
451+
try:
452+
if "google.colab" in str(get_ipython()):
453+
display(HTML(g_html_string))
454+
else:
455+
display(IFrame(src=graph_render_filename, width="100%", height="500px"))
456+
except Exception as e:
457+
print(f"[WARN]: failed to display the graph\n { e }")
458+
try:
459+
display(IFrame(src=graph_render_filename, width="100%", height="500px"))
460+
except Exception as e:
461+
print(f"[WARN]: failed to display the graph\n { e }")
462+
463+
return g
464+
426465
@needs_local_scope
427466
@line_cell_magic
428467
@magic_arguments()
@@ -434,10 +473,6 @@ def ng_draw(self, line, cell=None, local_ns={}):
434473
try:
435474
import pandas as pd
436475
from pyvis.network import Network
437-
from IPython.display import display, IFrame, HTML
438-
439-
# import get_ipython
440-
from IPython import get_ipython
441476

442477
except ImportError:
443478
raise ImportError("Please install pyvis to draw the graph")
@@ -454,16 +489,20 @@ def ng_draw(self, line, cell=None, local_ns={}):
454489
result_df = local_ns[variable_name]
455490

456491
if not isinstance(result_df, pd.DataFrame):
457-
try:
492+
if isinstance(result_df, ResultSet):
458493
result_df = self._stylized(result_df, style=STYLE_PANDAS)
459-
except Exception as e:
494+
elif isinstance(result_df, Network):
495+
# A rerun of %ng_draw with the last execution result
496+
g = self._draw_graph(result_df)
497+
return g
498+
else:
460499
fancy_print(
461-
f"[ERROR]: the last execution result is not a %ngql query, make a query first "
462-
f"or use %ng_draw <some query> instead, please. Something wrong with the result "
463-
f"parsing: \n { result_df }\n"
464-
f"Error: \n { e }",
500+
"[ERROR]: No valid %ngql query result available. \n"
501+
"Please execute a valid query before using %ng_draw. \n"
502+
"Or pass a query as an argument to %ng_draw or %%ng_draw(multiline).",
465503
color="red",
466504
)
505+
return ""
467506

468507
else:
469508
# Arguments provided, execute the query and draw the graph
@@ -516,32 +555,7 @@ def ng_draw(self, line, cell=None, local_ns={}):
516555
f"[WARN]: failed to calculate PageRank, left graph node unsized. Reason:\n { e }"
517556
)
518557

519-
g.repulsion(
520-
node_distance=90,
521-
central_gravity=0.2,
522-
spring_length=200,
523-
spring_strength=0.05,
524-
damping=0.09,
525-
)
526-
# g.show_buttons(filter_='physics')
527-
# return g.show("nebulagraph.html", notebook=True)
528-
cell_num = get_ipython().execution_count
529-
graph_render_filename = f"nebulagraph_cell_{cell_num}.html"
530-
g_html_string = g.generate_html(graph_render_filename)
531-
with open(graph_render_filename, "w", encoding="utf-8") as f:
532-
f.write(g_html_string)
533-
# detect if we are in colab or not
534-
try:
535-
if "google.colab" in str(get_ipython()):
536-
display(HTML(g_html_string))
537-
else:
538-
display(IFrame(src=graph_render_filename, width="100%", height="500px"))
539-
except Exception as e:
540-
print(f"[WARN]: failed to display the graph\n { e }")
541-
try:
542-
display(IFrame(src=graph_render_filename, width="100%", height="500px"))
543-
except Exception as e:
544-
print(f"[WARN]: failed to display the graph\n { e }")
558+
g = self._draw_graph(g)
545559

546560
return g
547561

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="jupyter_nebulagraph",
8-
version="0.13.1",
8+
version="0.13.2",
99
author="Wey Gu",
1010
author_email="[email protected]",
1111
description="Jupyter extension for NebulaGraph",

setup_ipython.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="ipython-ngql",
8-
version="0.13.1",
8+
version="0.13.2",
99
author="Wey Gu",
1010
author_email="[email protected]",
1111
description="Jupyter extension for NebulaGraph",

0 commit comments

Comments
 (0)