|
7 | 7 | import anywidget |
8 | 8 | import traitlets |
9 | 9 |
|
10 | | -from .node import Node |
| 10 | +from .node import Node, NodeIdType |
11 | 11 | from .options import RenderOptions |
12 | | -from .relationship import Relationship |
| 12 | +from .relationship import Relationship, RelationshipIdType |
13 | 13 |
|
14 | 14 |
|
15 | 15 | def _serialize_entity(entity: Union[Node, Relationship]) -> dict[str, Any]: |
@@ -79,3 +79,72 @@ def from_graph_data( |
79 | 79 | options=options.to_js_options() if options else {}, |
80 | 80 | theme=theme, |
81 | 81 | ) |
| 82 | + |
| 83 | + def add_data( |
| 84 | + self, nodes: Node | list[Node] | None = None, relationships: Relationship | list[Relationship] | None = None |
| 85 | + ) -> None: |
| 86 | + """ |
| 87 | + Add nodes or relationships to the graph widget. |
| 88 | +
|
| 89 | + Parameters |
| 90 | + ----------- |
| 91 | + nodes: |
| 92 | + Nodes to add to the graph widget. |
| 93 | + relationships: |
| 94 | + Relationships to add to the graph widget. |
| 95 | + """ |
| 96 | + if isinstance(nodes, Node): |
| 97 | + nodes = [nodes] |
| 98 | + if isinstance(relationships, Relationship): |
| 99 | + relationships = [relationships] |
| 100 | + |
| 101 | + if nodes: |
| 102 | + self.nodes = self.nodes + [_serialize_entity(n) for n in nodes] |
| 103 | + if relationships: |
| 104 | + self.relationships = self.relationships + [_serialize_entity(r) for r in relationships] |
| 105 | + |
| 106 | + def remove_data( |
| 107 | + self, |
| 108 | + nodes: Node | list[Node | NodeIdType] | NodeIdType | None = None, |
| 109 | + relationships: Relationship | list[Relationship | RelationshipIdType] | RelationshipIdType | None = None, |
| 110 | + ) -> None: |
| 111 | + """ |
| 112 | + Remove nodes or relationships from the graph widget. |
| 113 | +
|
| 114 | + Parameters |
| 115 | + ----------- |
| 116 | + nodes: |
| 117 | + Nodes to remove from the graph widget. |
| 118 | + relationships: |
| 119 | + Relationships to remove from the graph widget. |
| 120 | + """ |
| 121 | + if isinstance(nodes, Node): |
| 122 | + node_ids_to_remove = {nodes.id} |
| 123 | + elif isinstance(nodes, NodeIdType): |
| 124 | + node_ids_to_remove = {nodes} |
| 125 | + elif nodes is None: |
| 126 | + node_ids_to_remove = set() |
| 127 | + else: |
| 128 | + node_ids_to_remove = {n.id if isinstance(n, Node) else n for n in nodes} |
| 129 | + |
| 130 | + if isinstance(relationships, Relationship): |
| 131 | + rel_ids_to_remove = {relationships.id} |
| 132 | + elif isinstance(relationships, RelationshipIdType): |
| 133 | + rel_ids_to_remove = {relationships} |
| 134 | + elif relationships is None: |
| 135 | + rel_ids_to_remove = set() |
| 136 | + else: |
| 137 | + rel_ids_to_remove = {r.id if isinstance(r, Relationship) else r for r in relationships} |
| 138 | + |
| 139 | + if node_ids_to_remove: |
| 140 | + self.nodes = [n for n in self.nodes if n["id"] not in node_ids_to_remove] |
| 141 | + |
| 142 | + def keep_rel(r: dict[str, Any]) -> bool: |
| 143 | + return ( |
| 144 | + r["id"] not in rel_ids_to_remove |
| 145 | + and r["from"] not in node_ids_to_remove |
| 146 | + and r["to"] not in node_ids_to_remove |
| 147 | + ) |
| 148 | + |
| 149 | + if rel_ids_to_remove: |
| 150 | + self.relationships = [r for r in self.relationships if keep_rel(r)] |
0 commit comments