|
17 | 17 | from enum import Enum
|
18 | 18 | from typing import Optional, Union, List, Dict
|
19 | 19 |
|
20 |
| -from sagemaker.lineage._utils import get_resource_name_from_arn |
| 20 | +from sagemaker.lineage._utils import get_resource_name_from_arn, get_module |
21 | 21 |
|
22 | 22 |
|
23 | 23 | class LineageEntityEnum(Enum):
|
@@ -208,41 +208,14 @@ def __init__(self, graph_styles):
|
208 | 208 | """Init for PyvisVisualizer."""
|
209 | 209 | # import visualization packages
|
210 | 210 | (
|
211 |
| - self.pyvis, |
212 | 211 | self.Network,
|
213 | 212 | self.Options,
|
214 | 213 | ) = self._import_visual_modules()
|
215 | 214 |
|
216 | 215 | self.graph_styles = graph_styles
|
217 | 216 |
|
218 |
| - def _import_visual_modules(self): |
219 |
| - """Import modules needed for visualization.""" |
220 |
| - try: |
221 |
| - import pyvis |
222 |
| - except ImportError as e: |
223 |
| - print(e) |
224 |
| - print("Try: pip install pyvis") |
225 |
| - raise |
226 |
| - |
227 |
| - try: |
228 |
| - from pyvis.network import Network |
229 |
| - except ImportError as e: |
230 |
| - print(e) |
231 |
| - print("Try: pip install pyvis") |
232 |
| - raise |
233 |
| - |
234 |
| - try: |
235 |
| - from pyvis.options import Options |
236 |
| - except ImportError as e: |
237 |
| - print(e) |
238 |
| - print("Try: pip install pyvis") |
239 |
| - raise |
240 |
| - |
241 |
| - return pyvis, Network, Options |
242 |
| - |
243 |
| - def _get_options(self): |
244 |
| - """Get pyvis graph options.""" |
245 |
| - options = """ |
| 217 | + # pyvis graph options |
| 218 | + self._options = """ |
246 | 219 | var options = {
|
247 | 220 | "configure":{
|
248 | 221 | "enabled": false
|
@@ -271,29 +244,36 @@ def _get_options(self):
|
271 | 244 | }
|
272 | 245 | }
|
273 | 246 | """
|
274 |
| - return options |
275 | 247 |
|
276 |
| - def _node_color(self, n): |
| 248 | + def _import_visual_modules(self): |
| 249 | + """Import modules needed for visualization.""" |
| 250 | + get_module("pyvis") |
| 251 | + from pyvis.network import Network |
| 252 | + from pyvis.options import Options |
| 253 | + |
| 254 | + return Network, Options |
| 255 | + |
| 256 | + def _node_color(self, entity): |
277 | 257 | """Return node color by background-color specified in graph styles."""
|
278 |
| - return self.graph_styles[n[2]]["style"]["background-color"] |
| 258 | + return self.graph_styles[entity]["style"]["background-color"] |
279 | 259 |
|
280 | 260 | def render(self, elements, path="pyvisExample.html"):
|
281 | 261 | """Render graph for lineage query result."""
|
282 | 262 | net = self.Network(height="500px", width="100%", notebook=True, directed=True)
|
283 |
| - options = self._get_options() |
284 |
| - net.set_options(options) |
| 263 | + net.set_options(self._options) |
285 | 264 |
|
286 | 265 | # add nodes to graph
|
287 |
| - for n in elements["nodes"]: |
288 |
| - if n[3]: # startarn |
289 |
| - net.add_node(n[0], label=n[1], title=n[2], color=self._node_color(n), shape="star") |
| 266 | + for arn, source, entity, is_start_arn in elements["nodes"]: |
| 267 | + if is_start_arn: # startarn |
| 268 | + net.add_node( |
| 269 | + arn, label=source, title=entity, color=self._node_color(entity), shape="star" |
| 270 | + ) |
290 | 271 | else:
|
291 |
| - net.add_node(n[0], label=n[1], title=n[2], color=self._node_color(n)) |
| 272 | + net.add_node(arn, label=source, title=entity, color=self._node_color(entity)) |
292 | 273 |
|
293 | 274 | # add edges to graph
|
294 |
| - for e in elements["edges"]: |
295 |
| - print(e) |
296 |
| - net.add_edge(e[0], e[1], title=e[2]) |
| 275 | + for src, dest, asso_type in elements["edges"]: |
| 276 | + net.add_edge(src, dest, title=asso_type) |
297 | 277 |
|
298 | 278 | return net.show(path)
|
299 | 279 |
|
|
0 commit comments