Skip to content

Commit 3db7fa1

Browse files
author
Yi-Ting Lee
committed
add get element function
1 parent 1808f87 commit 3db7fa1

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

src/sagemaker/lineage/query.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,18 @@ def __str__(self):
235235
Format:
236236
{
237237
'edges':[
238-
"{
238+
{
239239
'source_arn': 'string', 'destination_arn': 'string',
240240
'association_type': 'string'
241-
}",
241+
},
242242
...
243243
],
244244
'vertices':[
245-
"{
245+
{
246246
'arn': 'string', 'lineage_entity': 'string',
247247
'lineage_source': 'string',
248248
'_session': <sagemaker.session.Session object>
249-
}",
249+
},
250250
...
251251
],
252252
'startarn':[
@@ -271,7 +271,7 @@ def _import_visual_modules(self):
271271

272272
return cyto, JupyterDash, html, Input, Output
273273

274-
def _get_verts(self):
274+
def _covert_vertices_to_tuples(self):
275275
"""Convert vertices to tuple format for visualizer."""
276276
verts = []
277277
# get vertex info in the form of (id, label, class)
@@ -283,24 +283,19 @@ def _get_verts(self):
283283
verts.append((vert.arn, vert.lineage_source, vert.lineage_entity))
284284
return verts
285285

286-
def _get_edges(self):
286+
def _covert_edges_to_tuples(self):
287287
"""Convert edges to tuple format for visualizer."""
288288
edges = []
289289
# get edge info in the form of (source, target, label)
290290
for edge in self.edges:
291291
edges.append((edge.source_arn, edge.destination_arn, edge.association_type))
292292
return edges
293293

294-
def visualize(self):
295-
"""Visualize lineage query result."""
296-
cyto, JupyterDash, html, Input, Output = self._import_visual_modules()
297-
298-
cyto.load_extra_layouts() # load "klay" layout (hierarchical layout) from extra layouts
299-
app = JupyterDash(__name__)
300-
294+
def _get_visualization_elements(self):
295+
"""Get elements for visualization."""
301296
# get vertices and edges info for graph
302-
verts = self._get_verts()
303-
edges = self._get_edges()
297+
verts = self._covert_vertices_to_tuples()
298+
edges = self._covert_edges_to_tuples()
304299

305300
nodes = [
306301
{"data": {"id": id, "label": label}, "classes": classes} for id, label, classes in verts
@@ -313,6 +308,17 @@ def visualize(self):
313308

314309
elements = nodes + edges
315310

311+
return elements
312+
313+
def visualize(self):
314+
"""Visualize lineage query result."""
315+
cyto, JupyterDash, html, Input, Output = self._import_visual_modules()
316+
317+
cyto.load_extra_layouts() # load "klay" layout (hierarchical layout) from extra layouts
318+
app = JupyterDash(__name__)
319+
320+
elements = self._get_visualization_elements()
321+
316322
app.layout = html.Div(
317323
[
318324
cyto.Cytoscape(
@@ -461,15 +467,17 @@ def visualize(self):
461467
)
462468

463469
@app.callback(Output("cytoscape-graph", "elements"),
464-
Input("cytoscape-graph", "tapNodeData"))
465-
def selectNode(data):
466-
for n in nodes:
467-
if data != None and n["data"]["id"] == data["id"]:
470+
Input("cytoscape-graph", "tapNodeData"),
471+
Input("cytoscape-graph", "elements"))
472+
def selectNode(tapData, elements):
473+
for n in elements:
474+
if tapData != None and n["data"]["id"] == tapData["id"]:
475+
# if is tapped node, add "select" class to node
468476
n["classes"] += " select"
469-
else:
477+
elif "classes" in n:
478+
# remove "select" class in "classes" if node not selected
470479
n["classes"] = n["classes"].replace("select", "")
471480

472-
elements = nodes + edges
473481
return elements
474482

475483
return app.run_server(mode="inline")

0 commit comments

Comments
 (0)