graph

add_node

graph.add_node(id) # (1)
graph.add_node(id, value) # (2)
  1. Adds a node to the graph with the id and value set to id.

    Argument Accepted Types

    id

    str, int or float

  2. Adds a node to the graph with the id and value set to their corresponding arguments.

    Argument Accepted Types

    id

    str, int or float

    value

    str, int or float

remove_node

graph.remove_node(node)

Removes the node with the specified id from the graph.

Argument Accepted Types

node

str, int or float

node

graph.node(id) # (1)
graph.node(node) # (2)
  1. Returns the Node object of the node with the specified id. Raises a NodeDoesntExistError if that node could not be found in the graph.

    Argument Accepted Types

    id

    str, int or float

  2. Returns the Node object of the Node object given to it as long as that node is in the graph. Raises a NodeDoesntExistError if that node could not be found in the graph.

    Argument Accepted Types

    node

    Node

nodes

graph.nodes()

Returns a list of all the nodes currently in the graph as Node objects.

add_edge

graph.add_edge(source, target, weight="", directed=False) # (1)

Adds an edge between the source and target nodes. To make the edge directed, set directed to True. weight sets the label of the edge.

Argument Accepted Types

source

Node, str, int or float

target

Node, str, int or float

weight

None, bool, str, int or float

directed

bool

remove_edge

graph.remove_edge(nodeA, nodeB, directed=False)

Removes the edge(s) between nodeA and nodeB from the graph. If directed is set, only edges starting at nodeA will be removed.

Argument Accepted Types

nodeA

Node, str, int or float

nodeB

Node, str, int or float

directed

bool

has_node

graph.has_node(node)

Checks if a given node exists in the graph.

Argument Accepted Types

node

Node, str, int or float

has_edge

graph.has_edge(edge)

Checks if a given edge exists in the graph.

Argument Accepted Types

edge

Edge

add

graph.add(element)

Adds the specified element to the graph. Can either be a Node object, or an Edge object. Raises an error if the element has been added to the graph before.

Argument Accepted Types

element

Node or Edge

remove

graph.remove(element)

Removes the specified element from the graph. Can either be a Node or an Edge object, and will return said object.

Argument Accepted Types

element

Node or Edge

add_all

graph.add_all(elements)

Adds all Node and/or Edge objects in a list to the graph.

Argument Accepted Types

elements

A list (iterable) containing Node and/or Edge objects.

remove_all

graph.remove_all(elements)

Removes all Node and/or Edge objects in a list from the graph.

Argument Accepted Types

elements

A list (iterable) containing Node and/or Edge objects.

adjacent

graph.adjacent(nodeA, nodeB, directed=False)

Checks if nodeA and nodeB are adjacent. If directed is set, then the edge must start from nodeA.

Argument Accepted Types

nodeA

Node, str, int or float

nodeB

Node, str, int or float

directed

bool

clear

graph.clear()

Completely resets the graph by removing all edges and nodes.

set_directed

graph.set_directed(directed=True)

Sets whether the edges in the graph are directed or not.

Argument Accepted Types

directed

bool

order

graph.order()

Returns the order of the graph. That is, it returns the number of nodes in the graph.

size

graph.size()

Returns the size of the graph. That is, it returns the number of edges in the graph.

adjacency_matrix

graph.adjacency_matrix()

Returns the adjacency matrix of the graph as a dictionary.

Node

__init__

Node(id, value=id)

Creates a node with the specified id and value. If value is left blank, it defaults to the node’s id.

Argument Accepted Types

id

str, int or float

value

str, int or float

id

node.id()

Returns the id of the node.

set_value

node.set_value(value)

Sets the value of the node.

Argument Accepted Types

value

str, int or float

value

node.value()

Gets the value of the node.

incident_edges

node.incident_edges()

Returns a list of the node’s incident edges.

incoming_edges

node.incoming_edges()

Returns a list of the node’s incoming edges.

outgoing_edges

node.outgoing_edges()

Returns a list of the node’s outgoing edges.

adjacent_nodes

node.adjacent_nodes()

Returns a list of the node’s adjacent nodes.

predecessor_nodes

node.predecessor_nodes()

Returns a list of the node’s predecessor nodes.

successor_nodes

node.successor_nodes()

Returns a list of the node’s successor nodes.

degree

node.degree()

Returns the node’s degree.

indegree

node.indegree()

Returns the node’s indegree.

outdegree

node.outdegree()

Returns the node’s outdegree.

set_attribute

node.set_attribute(name, value)

Sets custom attributes for the node.

Argument Accepted Types

name

str, int or float

value

str, int or float

attribute

node.attribute(name)

Gets custom attributes for the node.

Argument Accepted Types

name

str, int or float

set_priority

node.set_priority(value)

Sets the node’s priority value.

Argument Accepted Types

value

int or float

priority

node.priority()

Gets the node’s priority value.

set_position

node.set_position(x, y)

Sets the node’s position on the canvas. Uses relative positions whereby (0, 0) is the bottom-left corner and (1, 1) is the top-right corner.

Argument Accepted Types

x

int or float between 0 and 1.

y

int or float between 0 and 1.

position

node.position()

Returns the node’s position that was set. Note that it will not return the position of the node if it has not been set. Returns the position as a list with the format [x, y].

set_label

node.set_label(value, label_id=0)

Sets additional labels for the node. The labels do not override the node’s value, but instead are placed next to the node.

Argument Accepted Types

value

str, int or float

label_id

One of either 0 or 1. (Use 0 for the top-right label and 1 for the top-left label)

label

node.label(label_id)

Gets additional labels for the node. The labels do not override the node’s value, but instead are placed next to the node.

Argument Accepted Types

label_id

One of either 0 or 1. (Use 0 for the top-right label and 1 for the top-left label)

set_size

node.set_size(size=12)

Sets the radius of the node.

Argument Accepted Types

size

int or float

size

node.size()

Gets the radius of the node.

set_color

node.set_color(color=Color.DARK_GREY)

Sets the node’s color.

Argument Accepted Types

color

Color

color

node.color()

Gets the node’s color.

set_value_style

node.set_value_style(size=13, color=Color.WHITE)

Sets the appearance of the node’s value text.

Argument Accepted Types

size

int or float

color

Color

set_label_style

node.set_label_style(size=10, color=Color.GREY, label_id=0)

Sets the appearance of the node’s label text.

Argument Accepted Types

size

int or float

color

Color

label_id

One of either 0 or 1. (Use 0 for the top-right label and 1 for the top-left label)

highlight

node.highlight(color=Color.RED, size=node.size()*1.5)

Plays a highlight animation where the node’s color changes to that specified.

Argument Accepted Types

color

Color

size

int or float

Edge

__init__

Edge(source, target, weight="", directed=False)

Creates an edge between the source and target nodes. If weight is set, the edge will display it as a label. If directed is set, the edge will be directed, starting at the source node.

Argument Accepted Types

source

Node, str, int or float

target

Node, str, int or float

weight

None, bool, str, int or float

directed

bool

source

edge.source()

Returns the edge’s source node.

target

edge.target()

Returns the edge’s target node.

set_weight

edge.set_weight(weight=None)

Sets the weight of the edge.

Argument Accepted Types

weight

None, bool, str, int or float

weight

edge.weight()

Returns the weight of the edge.

set_directed

edge.set_directed(directed=True)

Sets whether the edge is directed.

Argument Accepted Types

directed

bool

directed

edge.directed()

Returns whether the edge is directed or not.

other_node

edge.other_node(node)

Returns the node connected by the edge that isn’t the node specified.

Argument Accepted Types

node

Node, str, int or float

set_attribute

edge.set_attribute(name, value)

Sets custom attributes for the edge.

Argument Accepted Types

name

str, int or float

value

str, int or float

attribute

edge.attribute(name)

Gets custom attributes for the edge.

Argument Accepted Types

name

str, int or float

set_priority

edge.set_priority(value)

Sets the edge’s priority value.

priority

edge.priority()

Gets the edge’s priority value.

Argument Accepted Types

value

int or float

set_width

edge.set_width(width=2)

Sets the edge’s width/thickness.

Argument Accepted Types

width

int or float

width

edge.width()

Gets the edge’s width/thickness.

set_color

edge.set_color(color=Color.LIGHT_GREY)

Sets the edge’s color.

Argument Accepted Types

color

Color

color

edge.color()

Returns the edge’s color.

set_weight_style

edge.set_weight_style(size=10, color=Color.GREY)

Sets the appearance of the edge’s weight label.

Argument Accepted Types

size

int or float

color

Color

highlight

edge.highlight(color=edge.color(), width=edge.width()*2)

Performs a highlight animation by temporarily changing the width and color of the edge.

Argument Accepted Types

color

Color

width

int or float

traverse

edge.traverse(initial_node=edge.source(), color=Color.RED, keep_path=True)

Performs a traversal animation on the edge, beginning at initial_node, using the specified color. If keep_path is set, the edge will remain colored.

Argument Accepted Types

initial_node

Node, str, int or float

color

Color

keep_path

bool

Miscellaneous functions

Color

Color(red, green, blue)

Custom colour for use in node and edge animations, using 0-255 integers for each argument.

There are some predefined colours: Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.WHITE, Color.LIGHT_GREY, Color.GREY, Color.DARK_GREY, Color.BLACK, Color.TRANSPARENT

You can also create colours from their hex values by using the from_hex_int method. The hex value needs to be entered as a number in python (this is what the 0x does in the snippet below). The hex value needs to be 6 digits long.

# creates a blue colour
Color.from_hex_int(0x0055ff)

pause

pause(time)

Delays the next visual event for the specified number of milliseconds.

Note
This does not pause code execution.

alert

alert(message)

Displays the message as an alert notification on the PyNode Next interface.

delay

delay(func, time, args=[], repeat=False)

Runs the function after the specified number of milliseconds, with the optional args list of arguments for the func. If repeat is set to true, the function will run repeatedly (with the specified time delay) until cancelled. Returns the delay_id of the delay.

Argument Accepted Types

func

Any function

time

int or float

args

A List to pass as arguments to func

repeat

bool

cancel_delay

cancel_delay(delay_id)

Cancels the specified delay.

Argument Accepted Types

delay_id

A delay ID returned from the delay function

register_click_listener

register_click_listener(func)

Registers a function that will be called when a node is clicked.

Argument Accepted Types

func

Callable[[Node], None]. That is, a function that as its only input argument takes a Node object that was clicked, something like def on_click(node). And does not return anything.

begin_pynode_next

begin_pynode_next(func, open_browser=True, check_for_new_version=True)

Begins PyNode Next. And loads the func to run when the play button is pressed in the interface. If open_browser is set to true, it will automatically open the PyNode Next interface in the default browser. If check_for_new_version is set to true, it will check if there is a new version of PyNode Next version available.

Argument Accepted Types

func

Callable[[], None]. That is, a function that doesn’t have any input arguments and doesn’t return anything.

open_browser

bool

check_for_new_version

bool