Skip to content

Commit 383fe1e

Browse files
author
Neel Shah
committed
Add enable/disable magic for showing graph tab
1 parent b2f2b55 commit 383fe1e

File tree

1 file changed

+117
-114
lines changed

1 file changed

+117
-114
lines changed
Lines changed: 117 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,117 @@
1-
"""
2-
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
SPDX-License-Identifier: Apache-2.0
4-
"""
5-
6-
import json
7-
8-
from networkx import MultiDiGraph
9-
from networkx.readwrite import json_graph
10-
11-
ERROR_EDGE_NOT_FOUND = ValueError("Edge was not found on network graph")
12-
ERROR_INVALID_DATA = ValueError("Data must be a dict")
13-
14-
15-
class Network:
16-
"""
17-
Network wraps a Networkx MultiDiGraph and provides some utilities
18-
to add nodes and edges to the graph. For use each language meant to use it,
19-
the Network class will be extended to ensure that we are adding the data needed to ensure that
20-
we maintain the properties inside each node and edge appropriately.
21-
"""
22-
23-
def __init__(self, graph: MultiDiGraph = None):
24-
if graph is None:
25-
graph = MultiDiGraph()
26-
self.graph = graph
27-
28-
def add_node_property(self, node_id: str, key: str, value: str):
29-
"""
30-
updates the "properties" key on the given :param node_id. For instance, if key=foo, and value=bar,
31-
then the given node would now be guaranteed to have the entry node['properties']['foo'] = bar
32-
:param node_id: id of the node to update
33-
:param key: the key to update under this nodes' properties dict
34-
:param value: the value to set
35-
"""
36-
node = self.graph.nodes.get(node_id)
37-
if node is None:
38-
node = self.graph.add_node(node_id)
39-
40-
if 'properties' not in node:
41-
node['properties'] = {key: value}
42-
else:
43-
node['properties'][key] = value
44-
45-
def add_node(self, node_id: str, data=None):
46-
if data is None:
47-
data = {}
48-
self.graph.add_node(node_id, **data)
49-
50-
def add_edge(self, from_id: str, to_id: str, edge_id: str, label: str, data: dict = None):
51-
if data is None:
52-
data = {}
53-
54-
data['label'] = label
55-
self.graph.add_edge(from_id, to_id, edge_id, **data)
56-
57-
def add_node_data(self, node_id: str, data: dict):
58-
"""
59-
overrides the keys on a node with the data found in :param data
60-
:param node_id: the id of the node to update
61-
:param data: key-value dictionary to update node with
62-
"""
63-
64-
if type(data) is not dict:
65-
raise ERROR_INVALID_DATA
66-
67-
node = self.graph.nodes.get(node_id)
68-
if node is None:
69-
self.add_node(node_id, data)
70-
return
71-
72-
for key in data:
73-
node[key] = data[key]
74-
75-
def add_edge_data(self, from_id: str, to_id: str, edge_id, data: dict):
76-
if not self.graph.has_edge(from_id, to_id, edge_id):
77-
raise ERROR_EDGE_NOT_FOUND
78-
79-
if type(data) is not dict:
80-
raise ERROR_INVALID_DATA
81-
82-
edge = self.graph.edges[from_id, to_id, edge_id]
83-
for key in data:
84-
edge[key] = data[key]
85-
86-
def add_results(self, results):
87-
"""
88-
base method to be overridden by implementations to add results.
89-
For SPARQL, these results are a dict with bindings, for Gremlin, they are paths
90-
:param results:
91-
:return:
92-
"""
93-
pass
94-
95-
def to_json(self) -> dict:
96-
try:
97-
# NetworkX 2.6+
98-
graph_data = json_graph.node_link_data(self.graph, edges="links")
99-
except TypeError:
100-
# NetworkX < 2.6
101-
graph_data = json_graph.node_link_data(self.graph)
102-
return {'graph': graph_data}
103-
104-
105-
def network_to_json(network: Network) -> str:
106-
return json.dumps(network.to_json())
107-
108-
109-
def network_from_json(raw) -> Network:
110-
data = json.loads(raw)
111-
network = Network()
112-
if 'graph' in data:
113-
network.graph = json_graph.node_link_graph(data['graph'], directed=True)
114-
return network
1+
"""
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: Apache-2.0
4+
"""
5+
6+
import json
7+
8+
from networkx import MultiDiGraph
9+
from networkx.readwrite import json_graph
10+
11+
ERROR_EDGE_NOT_FOUND = ValueError("Edge was not found on network graph")
12+
ERROR_INVALID_DATA = ValueError("Data must be a dict")
13+
14+
15+
class Network:
16+
"""
17+
Network wraps a Networkx MultiDiGraph and provides some utilities
18+
to add nodes and edges to the graph. For use each language meant to use it,
19+
the Network class will be extended to ensure that we are adding the data needed to ensure that
20+
we maintain the properties inside each node and edge appropriately.
21+
"""
22+
23+
def __init__(self, graph: MultiDiGraph = None):
24+
if graph is None:
25+
graph = MultiDiGraph()
26+
self.graph = graph
27+
28+
def add_node_property(self, node_id: str, key: str, value: str):
29+
"""
30+
updates the "properties" key on the given :param node_id. For instance, if key=foo, and value=bar,
31+
then the given node would now be guaranteed to have the entry node['properties']['foo'] = bar
32+
:param node_id: id of the node to update
33+
:param key: the key to update under this nodes' properties dict
34+
:param value: the value to set
35+
"""
36+
node = self.graph.nodes.get(node_id)
37+
if node is None:
38+
node = self.graph.add_node(node_id)
39+
40+
if 'properties' not in node:
41+
node['properties'] = {key: value}
42+
else:
43+
node['properties'][key] = value
44+
45+
def add_node(self, node_id: str, data=None):
46+
if data is None:
47+
data = {}
48+
self.graph.add_node(node_id, **data)
49+
50+
def add_edge(self, from_id: str, to_id: str, edge_id: str, label: str, data: dict = None):
51+
if data is None:
52+
data = {}
53+
54+
data['label'] = label
55+
self.graph.add_edge(from_id, to_id, edge_id, **data)
56+
57+
def add_node_data(self, node_id: str, data: dict):
58+
"""
59+
overrides the keys on a node with the data found in :param data
60+
:param node_id: the id of the node to update
61+
:param data: key-value dictionary to update node with
62+
"""
63+
64+
if type(data) is not dict:
65+
raise ERROR_INVALID_DATA
66+
67+
node = self.graph.nodes.get(node_id)
68+
if node is None:
69+
self.add_node(node_id, data)
70+
return
71+
72+
for key in data:
73+
node[key] = data[key]
74+
75+
def add_edge_data(self, from_id: str, to_id: str, edge_id, data: dict):
76+
if not self.graph.has_edge(from_id, to_id, edge_id):
77+
raise ERROR_EDGE_NOT_FOUND
78+
79+
if type(data) is not dict:
80+
raise ERROR_INVALID_DATA
81+
82+
edge = self.graph.edges[from_id, to_id, edge_id]
83+
for key in data:
84+
edge[key] = data[key]
85+
86+
def add_results(self, results):
87+
"""
88+
base method to be overridden by implementations to add results.
89+
For SPARQL, these results are a dict with bindings, for Gremlin, they are paths
90+
:param results:
91+
:return:
92+
"""
93+
pass
94+
95+
def to_json(self) -> dict:
96+
try:
97+
# NetworkX 2.6+
98+
graph_data = json_graph.node_link_data(self.graph, edges="links")
99+
except TypeError:
100+
# NetworkX < 2.6
101+
graph_data = json_graph.node_link_data(self.graph)
102+
return {'graph': graph_data}
103+
104+
105+
def network_to_json(network: Network) -> str:
106+
return json.dumps(network.to_json())
107+
108+
109+
def network_from_json(raw) -> Network:
110+
data = json.loads(raw)
111+
network = Network()
112+
if 'graph' in data:
113+
try:
114+
network.graph = json_graph.node_link_graph(data['graph'], directed=True, edges="links")
115+
except:
116+
network.graph = json_graph.node_link_graph(data['graph'], directed=True)
117+
return network

0 commit comments

Comments
 (0)