Brian Scott
Brian Scott

Reputation: 101

Pyvis labels do not display if nodes are integers?

I'm new to pyvis, so this is perhaps the expected behavior, although it doesn't seem so based on the docs. In order to get labels displayed by the nodes I either need to explicitly pass label= or use a string for the node. In other words, net.add_node(0) does not display a label for the node, but net.add_node("0") or net.add_node(0, label="0") do. The docs (>>> net.add_node(2) # node id and label = 2), as well as some tutorials I've seen, suggest otherwise.

FWIW I get the same result viewing the graph in the notebook or opening the resulting file in my browser.

Is there some default I'm missing?

I'm using pyvis 0.3.2 and python 3.11.5.

Upvotes: 0

Views: 533

Answers (1)

Dan Nagle
Dan Nagle

Reputation: 5425

The label needs to be a string. You can perform a check before adding a node.

from pyvis.network import Network

net = Network()

data = [("Apple", 1), (2, 2), ("Carrot", 3)]

for label, value in data:
    if not isinstance(label, str):
        label = str(label)
    net.add_node(value, label=label)

net.show("nodes.html", notebook=False)

Upvotes: 0

Related Questions