in src/graph_notebook/magics/graph_magic.py [0:0]
def handle_opencypher_query(self, line, cell, local_ns):
"""
This method in its own handler so that the magics %%opencypher and %%oc can both call it
"""
parser = argparse.ArgumentParser()
parser.add_argument('-g', '--group-by', type=str, default='~labels',
help='Property used to group nodes (e.g. code, ~id) default is ~labels')
parser.add_argument('mode', nargs='?', default='query', help='query mode [query|bolt]',
choices=['query', 'bolt'])
parser.add_argument('-d', '--display-property', type=str, default='~labels',
help='Property to display the value of on each node, default is ~labels')
parser.add_argument('-de', '--edge-display-property', type=str, default='~labels',
help='Property to display the value of on each edge, default is ~type')
parser.add_argument('-t', '--tooltip-property', type=str, default='',
help='Property to display the value of on each node tooltip. If not specified, tooltip '
'will default to the node label value.')
parser.add_argument('-te', '--edge-tooltip-property', type=str, default='',
help='Property to display the value of on each edge tooltip. If not specified, tooltip '
'will default to the edge label value.')
parser.add_argument('-l', '--label-max-length', type=int, default=10,
help='Specifies max length of vertex label, in characters. Default is 10')
parser.add_argument('-rel', '--rel-label-max-length', type=int, default=10,
help='Specifies max length of edge labels, in characters. Default is 10')
parser.add_argument('--store-to', type=str, default='', help='store query result to this variable')
parser.add_argument('--ignore-groups', action='store_true', default=False, help="Ignore all grouping options")
parser.add_argument('-sp', '--stop-physics', action='store_true', default=False,
help="Disable visualization physics after the initial simulation stabilizes.")
parser.add_argument('-sd', '--simulation-duration', type=int, default=1500,
help='Specifies maximum duration of visualization physics simulation. Default is 1500ms')
parser.add_argument('--silent', action='store_true', default=False, help="Display no query output.")
parser.add_argument('--no-scroll', action='store_true', default=False,
help="Display the entire output without a scroll bar.")
args = parser.parse_args(line.split())
logger.debug(args)
res = None
if args.no_scroll:
oc_layout = UNRESTRICTED_LAYOUT
else:
oc_layout = DEFAULT_LAYOUT
if not args.silent:
tab = widgets.Tab()
titles = []
children = []
force_graph_output = None
if args.mode == 'query':
query_start = time.time() * 1000 # time.time() returns time in seconds w/high precision; x1000 to get in ms
oc_http = self.client.opencypher_http(cell)
query_time = time.time() * 1000 - query_start
oc_http.raise_for_status()
res = oc_http.json()
if not args.silent:
oc_metadata = build_opencypher_metadata_from_query(query_type='query', results=res,
query_time=query_time)
try:
gn = OCNetwork(group_by_property=args.group_by, display_property=args.display_property,
edge_display_property=args.edge_display_property,
tooltip_property=args.tooltip_property,
edge_tooltip_property=args.edge_tooltip_property,
label_max_length=args.label_max_length,
edge_label_max_length=args.rel_label_max_length,
ignore_groups=args.ignore_groups)
gn.add_results(res)
logger.debug(f'number of nodes is {len(gn.graph.nodes)}')
if len(gn.graph.nodes) > 0:
self.graph_notebook_vis_options['physics']['disablePhysicsAfterInitialSimulation'] \
= args.stop_physics
self.graph_notebook_vis_options['physics']['simulationDuration'] = args.simulation_duration
force_graph_output = Force(network=gn, options=self.graph_notebook_vis_options)
except (TypeError, ValueError) as network_creation_error:
logger.debug(f'Unable to create network from result. Skipping from result set: {res}')
logger.debug(f'Error: {network_creation_error}')
elif args.mode == 'bolt':
res = self.client.opencyper_bolt(cell)
# Need to eventually add code to parse and display a network for the bolt format here
if not args.silent:
rows_and_columns = opencypher_get_rows_and_columns(res, True if args.mode == 'bolt' else False)
display(tab)
table_output = widgets.Output(layout=oc_layout)
# Assign an empty value so we can always display to table output.
table_html = ""
# Display Console Tab
# some issues with displaying a datatable when not wrapped in an hbox and displayed last
hbox = widgets.HBox([table_output], layout=oc_layout)
children.append(hbox)
titles.append('Console')
if rows_and_columns is not None:
table_id = f"table-{str(uuid.uuid4())[:8]}"
table_html = opencypher_table_template.render(columns=rows_and_columns['columns'],
rows=rows_and_columns['rows'], guid=table_id)
# Display Graph Tab (if exists)
if force_graph_output:
titles.append('Graph')
children.append(force_graph_output)
# Display JSON tab
json_output = widgets.Output(layout=oc_layout)
with json_output:
print(json.dumps(res, indent=2))
children.append(json_output)
titles.append('JSON')
# Display Query Metadata Tab
metadata_output = widgets.Output(layout=oc_layout)
titles.append('Query Metadata')
children.append(metadata_output)
tab.children = children
for i in range(len(titles)):
tab.set_title(i, titles[i])
if table_html != "":
with table_output:
display(HTML(table_html))
with metadata_output:
display(HTML(oc_metadata.to_html()))
store_to_ns(args.store_to, res, local_ns)