Skip to content

Commit

Permalink
Fix OC node tooltips display (#163)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Chin <[email protected]>
  • Loading branch information
michaelnchin and michaelnchin authored Jul 29, 2021
1 parent f501e72 commit d0f0496
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
13 changes: 9 additions & 4 deletions src/graph_notebook/network/EventfulNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import collections
from networkx import MultiDiGraph
from .Network import Network
from typing import Tuple

EVENT_ADD_NODE = 'add_node'
EVENT_ADD_NODE_DATA = 'add_node_data'
Expand Down Expand Up @@ -39,11 +40,15 @@ def __init__(self, graph: MultiDiGraph = None, callbacks: dict = None):
graph = MultiDiGraph()
super().__init__(graph)

def strip_and_truncate_label(self, old_label: str, max_len: int):
label = str(old_label).strip("[]'")
return label if len(label) <= max_len else label[:max_len - 3] + '...'
def strip_and_truncate_label_and_title(self, old_label: str, max_len: int) -> Tuple[str, str]:
title = str(old_label).strip("[]'")
if len(title) <= max_len:
label = title
else:
label = title[:max_len - 3] + '...'
return title, label

def flatten(self, d:dict, parent_key='', sep='_') -> dict:
def flatten(self, d: dict, parent_key='', sep='_') -> dict:
"""Flattens dictionaries including nested dictionaties
Args:
Expand Down
15 changes: 3 additions & 12 deletions src/graph_notebook/network/gremlin/GremlinNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,6 @@ def get_id(element):
return str(element)


def strip_and_truncate_label_and_title(old_label: str, max_len: int):
title = str(old_label).strip("[]'")
if len(title) <= max_len:
label = title
else:
label = title[:max_len - 3] + '...'
return title, label


class GremlinNetwork(EventfulNetwork):
"""
GremlinNetwork extends the Network class and uses the add_results method to parse two specific types of responses
Expand Down Expand Up @@ -350,7 +341,7 @@ def add_vertex(self, v):
# Since it is needed for checking for the vertex label's desired grouping behavior in group_by_property
if T.label in v.keys():
title = str(v[T.label])
title_plc, label = strip_and_truncate_label_and_title(title, self.label_max_length)
title_plc, label = self.strip_and_truncate_label_and_title(title, self.label_max_length)
for k in v:
if str(k) == T_ID:
node_id = str(v[k])
Expand All @@ -366,11 +357,11 @@ def add_vertex(self, v):
if isinstance(self.display_property, dict):
try:
if str(k) == self.display_property[title]:
title, label = strip_and_truncate_label_and_title(str(v[k]), self.label_max_length)
title, label = self.strip_and_truncate_label_and_title(str(v[k]), self.label_max_length)
except KeyError:
continue
elif str(k) == self.display_property:
title, label = strip_and_truncate_label_and_title(str(v[k]), self.label_max_length)
title, label = self.strip_and_truncate_label_and_title(str(v[k]), self.label_max_length)

# handle when there is no id in a node. In this case, we will generate one which
# is consistently regenerated so that duplicate dicts will be reduced to the same vertex.
Expand Down
9 changes: 5 additions & 4 deletions src/graph_notebook/network/opencypher/OCNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ def parse_node(self, node: dict):
title = ""
for key in node:
title += str(node[key])

label = title if len(title) <= self.label_max_length else title[:self.label_max_length - 3] + '...'

if not isinstance(self.group_by_property, dict): # Handle string format group_by
if self.group_by_property in [LABEL_KEY, 'labels'] and len(node[LABEL_KEY]) > 0:
group = node[LABEL_KEY][0]
Expand Down Expand Up @@ -103,6 +102,7 @@ def parse_node(self, node: dict):
else:
label = str(props)
except KeyError:
label = title
pass
elif self.display_property in [ID_KEY, 'id']:
label = str(node[ID_KEY])
Expand All @@ -112,9 +112,10 @@ def parse_node(self, node: dict):
label = str(node[VERTEX_TYPE_KEY])
elif self.display_property in props:
label = props[self.display_property]
else:
label = title

title = label
label = self.strip_and_truncate_label(label, self.label_max_length)
title, label = self.strip_and_truncate_label_and_title(label, self.label_max_length)
data = {'properties': props, 'label': label, 'title': title, 'group': group}
self.add_node(node[ID_KEY], data)

Expand Down
16 changes: 15 additions & 1 deletion test/unit/network/opencypher/test_opencypher_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_add_node_with_callback(self):
"~labels": ['airport'],
'code': 'SEA',
'runways': 3},
'title': "['airport']"},
'title': "airport"},
'node_id': '22'}

def add_node_callback(network, event_name, data):
Expand Down Expand Up @@ -626,6 +626,7 @@ def test_do_not_set_vertex_label_property(self):
gn.add_results(res)
node1 = gn.graph.nodes.get('22')
self.assertEqual(node1['label'], 'airport')
self.assertEqual(node1['title'], 'airport')

def test_set_vertex_label_property_string(self):
res = {
Expand All @@ -649,6 +650,7 @@ def test_set_vertex_label_property_string(self):
gn.add_results(res)
node1 = gn.graph.nodes.get('22')
self.assertEqual(node1['label'], 'SEA')
self.assertEqual(node1['title'], 'SEA')

def test_set_vertex_label_property_string_id(self):
res = {
Expand All @@ -672,6 +674,7 @@ def test_set_vertex_label_property_string_id(self):
gn.add_results(res)
node1 = gn.graph.nodes.get('22')
self.assertEqual(node1['label'], '22')
self.assertEqual(node1['title'], '22')

def test_set_vertex_label_property_string_label(self):
res = {
Expand All @@ -695,6 +698,7 @@ def test_set_vertex_label_property_string_label(self):
gn.add_results(res)
node1 = gn.graph.nodes.get('22')
self.assertEqual(node1['label'], 'airport')
self.assertEqual(node1['title'], 'airport')

def test_set_vertex_label_property_string_type(self):
res = {
Expand All @@ -718,6 +722,7 @@ def test_set_vertex_label_property_string_type(self):
gn.add_results(res)
node1 = gn.graph.nodes.get('22')
self.assertEqual(node1['label'], 'node')
self.assertEqual(node1['title'], 'node')

def test_set_vertex_label_property_json(self):
res = {
Expand All @@ -741,6 +746,7 @@ def test_set_vertex_label_property_json(self):
gn.add_results(res)
node1 = gn.graph.nodes.get('22')
self.assertEqual(node1['label'], 'SEA')
self.assertEqual(node1['title'], 'SEA')

def test_set_vertex_label_property_invalid_json(self):
res = {
Expand All @@ -764,6 +770,7 @@ def test_set_vertex_label_property_invalid_json(self):
gn.add_results(res)
node1 = gn.graph.nodes.get('22')
self.assertEqual(node1['label'], 'airport')
self.assertEqual(node1['title'], 'airport')

def test_set_vertex_label_property_invalid_key(self):
res = {
Expand All @@ -787,6 +794,7 @@ def test_set_vertex_label_property_invalid_key(self):
gn.add_results(res)
node1 = gn.graph.nodes.get('22')
self.assertEqual(node1['label'], 'airport')
self.assertEqual(node1['title'], 'airport')

def test_set_vertex_label_length(self):
res = {
Expand All @@ -810,6 +818,7 @@ def test_set_vertex_label_length(self):
gn.add_results(res)
node1 = gn.graph.nodes.get('22')
self.assertEqual(node1['label'], 'ai...')
self.assertEqual(node1['title'], 'airport')

def test_set_vertex_label_property_invalid_value(self):
res = {
Expand All @@ -833,6 +842,7 @@ def test_set_vertex_label_property_invalid_value(self):
gn.add_results(res)
node1 = gn.graph.nodes.get('22')
self.assertEqual(node1['label'], 'airport')
self.assertEqual(node1['title'], 'airport')

def test_set_label_property_multiple_vertices_property_string(self):
res = {
Expand Down Expand Up @@ -868,7 +878,9 @@ def test_set_label_property_multiple_vertices_property_string(self):
node1 = gn.graph.nodes.get('11')
node2 = gn.graph.nodes.get('22')
self.assertEqual(node1['label'], 'JFK')
self.assertEqual(node1['title'], 'JFK')
self.assertEqual(node2['label'], 'SEA')
self.assertEqual(node2['title'], 'SEA')

def test_set_label_property_multiple_types(self):
path = {
Expand Down Expand Up @@ -923,7 +935,9 @@ def test_set_label_property_multiple_types(self):
node1 = gn.graph.nodes.get('2')
node2 = gn.graph.nodes.get('3670')
self.assertEqual(node1['label'], 'ANC')
self.assertEqual(node1['title'], 'ANC')
self.assertEqual(node2['label'], 'United ...')
self.assertEqual(node2['title'], 'United States')

def test_add_edge_without_property(self):
path = {
Expand Down

0 comments on commit d0f0496

Please sign in to comment.