def set_label_style(self, size=None, color=None, outline=None, label_id=None): if label_id is None or (label_id != 0 and label_id != 1): style1 = CustomStyle(self._label_styles[0]._size if size is None else size, self._label_styles[0]._color if color is None else color, self._label_styles[0]._outline if outline is None else outline) style2 = CustomStyle(self._label_styles[1]._size if size is None else size, self._label_styles[1]._color if color is None else color, self._label_styles[1]._outline if outline is None else outline) self._label_styles[0] = style1 self._label_styles[1] = style2 pynode_core.add_event(pynode_core.Event(pynode_core.js_node_set_label_style, [self._internal_id, self._label_styles[0].data(self), 0]), self) pynode_core.add_event(pynode_core.Event(pynode_core.js_node_set_label_style, [self._internal_id, self._label_styles[1].data(self), 1]), self) else: style = CustomStyle(self._label_styles[label_id]._size if size is None else size,Color.WHITE if color is None else color, outline) self._label_styles[label_id] = style pynode_core.add_event(pynode_core.Event(pynode_core.js_node_set_label_style, [self._internal_id, self._label_styles[label_id].data(self), label_id]), self) return self
def set_color(self, color=Color.LIGHT_GREY): self._color = color pynode_core.add_event( pynode_core.Event( pynode_core.js_edge_set_color, [self._internal_id, color.hex_string()]), self) return self
def add_edge(self, *args, **kwds): if "edge" in kwds: e = kwds["edge"] elif len(args) > 0 and isinstance(args[0], Edge): e = args[0] else: arg_source = kwds["source"] if "source" in kwds else args[0] arg_target = kwds["target"] if "target" in kwds else args[1] arg_weight = kwds["weight"] if "weight" in kwds else args[ 2] if len(args) > 2 else None arg_directed = kwds["directed"] if "directed" in kwds else args[ 3] if len(args) > 3 else False e = Edge(arg_source, arg_target, arg_weight, arg_directed) if self.has_edge(e): raise Exception("Instance of edge '" + str(e) + "' already in graph.") original_source = e._source original_target = e._target e._source = graph.node(e._source) e._target = graph.node(e._target) if e._source is None: raise Exception("Node '" + str(original_source) + "' doesn't exist.") if e._target is None: raise Exception("Node '" + str(original_target) + "' doesn't exist.") e._source._incident_edges.append(e) e._target._incident_edges.append(e) self._edges.append(e) self._has_edge_cache[e] = True pynode_core.add_event( pynode_core.Event(pynode_core.js_add_edge, [e._data()])) return e
def set_text_color(self, color): self._value_style._color = color pynode_core.add_event( pynode_core.Event( pynode_core.js_node_set_value_style, [self._internal_id, self._value_style.data(self)]), self) return self
def set_label(self, text, label_id=0): self._labels[label_id] = text pynode_core.add_event( pynode_core.Event(pynode_core.js_node_set_label, [ self._internal_id, str(text) if text is not None else "", label_id ]), self) return self
def set_position(self, x, y=None, relative=False): self._position = [x, y] if x is None or y is None: self._position = None self._is_pos_relative = relative pynode_core.add_event( pynode_core.Event(pynode_core.js_node_set_position, [self._internal_id, x, y, relative]), self) return self
def set_value(self, value): self._value = value pynode_core.add_event( pynode_core.Event( pynode_core.js_node_set_value, [self._internal_id, str(value) if value is not None else ""]), self) return self
def set_weight(self, weight=None): self._weight = weight pynode_core.add_event( pynode_core.Event( pynode_core.js_edge_set_weight, [self._internal_id, str(weight) if weight is not None else ""]), self) return self
def set_text_size(self, size): self._value_style._size = size pynode_core.add_event( pynode_core.Event( pynode_core.js_node_set_value_style, [self._internal_id, self._value_style.data(self)]), self) return self
def position(self): data = pynode_core.get_data((pynode_core.Event(pynode_core.js_node_get_position, [self._internal_id]))) if graph.has_node(self) and data is not None and data[0] is not None and data[1] is not None: return int(data[0]), int(data[1]) elif self._position is None: return None else: if self._is_pos_relative and data is not None and data[2] is not None and data[3] is not None: return int(self._position[0] * data[2]), int(self._position[1] * data[3]) else: return int(self._position[0]), int(self._position[1])
def add_node(self, *args, **kwds): if "node" in kwds: n = kwds["node"] elif len(args) > 0 and isinstance(args[0], Node): n = args[0] else: n = Node(*args, **kwds) if n._id in self._nodes: raise Exception("Duplicate node '" + str(n._id) + "'") self._nodes[n._id] = n pynode_core.add_event(pynode_core.Event(pynode_core.js_add_node, [n._data()])) pause(25) return n
def highlight(self, *args, **kwds): arg_color = kwds["color"] if "color" in kwds else args[0] if len( args) > 0 else None arg_size = kwds["size"] if "size" in kwds else args[1] if len( args) > 1 else self._size * 1.5 pynode_core.add_event( pynode_core.Event(pynode_core.js_node_highlight, [ self._internal_id, arg_size, arg_color.hex_string() if arg_color is not None else None ]), self)
def remove_all(self, elements): new_elements = [] pynode_core.enable_events(False) for x in elements: if isinstance(x, Node): new_elements.append((0, self.remove_node(x)._data())) elif isinstance(x, Edge): new_elements.append((1, self.remove_edge(x)._data())) else: new_elements.append((0, self.remove_node(self.node(x))._data())) pynode_core.enable_events(True) pynode_core.add_event(pynode_core.Event(pynode_core.js_remove_all, [new_elements])) pause(55)
def remove_node(self, node): n = self.node(node) pynode_core.enable_events(False) for e in n.incident_edges(): self.remove_edge(e) pynode_core.enable_events(True) del self._nodes[n._id] pynode_core.add_event(pynode_core.Event(pynode_core.js_remove_node, [n._internal_id])) pause(25) return n
def traverse(self, initial_node=None, color=Color.RED, keep_path=True): if not graph.has_edge(self): return start = graph.node( initial_node) if initial_node is not None else self._source if not graph.has_node(start): return pynode_core.add_event( pynode_core.Event(pynode_core.js_edge_traverse, [ self._internal_id, start._internal_id, color.hex_string(), keep_path ]), self)
def highlight(self, *args, **kwds): arg_color = kwds["color"] if "color" in kwds else args[0] if len( args) > 0 else None arg_width = kwds["width"] if "width" in kwds else args[1] if len( args) > 1 else self._width * 2 pynode_core.add_event( pynode_core.Event(pynode_core.js_edge_highlight, [ self._internal_id, arg_width, arg_color.hex_string() if arg_color is not None else None ]), self)
def set_weight_style(self, size=None, color=None, outline=None): self._weight_style = CustomStyle( self._weight_style._size if size is None else size, self._weight_style._color if color is None else color, self._weight_style._outline if outline is None else outline) pynode_core.add_event( pynode_core.Event( pynode_core.js_edge_set_weight_style, [self._internal_id, self._weight_style.data(self)]), self) return self
def remove_edge(self, *args, **kwds): remove_multiple = False if "edge" in kwds: edge = kwds["edge"] elif len(args) > 0 and isinstance(args[0], Edge): edge = args[0] else: arg_source = kwds["node1"] if "node1" in kwds else args[0] arg_target = kwds["node2"] if "node2" in kwds else args[1] arg_directed = kwds["directed"] if "directed" in kwds else args[2] if len(args) > 2 else False remove_multiple = True if remove_multiple: edge_list = self.edges_between(arg_source, arg_target, arg_directed) self.remove_all(edge_list) return edge_list else: edge._source._incident_edges.remove(edge) edge._target._incident_edges.remove(edge) self._edges.remove(edge) del self._has_edge_cache[edge] pynode_core.add_event(pynode_core.Event(pynode_core.js_remove_edge, [edge._internal_id])) return edge
def clear(self): self._reset() pynode_core.add_event(pynode_core.Event(pynode_core.js_clear, []))
def set_spread(self, spread=80): self._spread = spread pynode_core.add_event( pynode_core.Event(pynode_core.js_set_spread, [spread]))
def set_size(self, size): self._size = size pynode_core.add_event( pynode_core.Event(pynode_core.js_node_set_size, [self._internal_id, size]), self) return self
def set_width(self, width=2): self._width = width pynode_core.add_event( pynode_core.Event(pynode_core.js_edge_set_width, [self._internal_id, width]), self) return self
def set_directed(self, directed=True): self._directed = directed pynode_core.add_event( pynode_core.Event(pynode_core.js_edge_set_directed, [self._internal_id, self._directed]), self) return self