Panel.interact does not work with label objects

Hello everyone,

I’m trying to make a graph where one can enable node labels interactively. However, the pn.interact() method does not seem to work with holoviews label objects.

Below an example that changes edge and node colors, which works just fine and an analogous example with edge color and node lables, which somehow does not work (the node lables appear regardless of the parameter), even though the setup is largely the same.

I hope one of you can help me out. Thanks in advance!

Btw: The working example with node and edge color changes was taken from another discussion https://discourse.holoviz.org/t/bug-when-saving-dynamic-map/4027.

import panel as pn
import numpy as np
import holoviews as hv
pn.extension()

# graph with variable node and edge color
def hv_graph(edge_color, node_color):
	# preparations for the graph
    node_indices = np.arange(2, dtype=np.int32)
    source = np.zeros(2, dtype=np.int32)
    target = node_indices
    # modify the edge and node color
    graph = hv.Graph(((source, target),)).opts(edge_color=edge_color, node_color=node_color)
    return graph
    
# graph with variable edge color and potential node labels
def hv_graph_2(edge_color, with_labels):
	# preparations for the graph
    node_indices = np.arange(2, dtype=np.int32)
    source = np.zeros(2, dtype=np.int32)
    target = node_indices
    # modify the edge color only
    graph = hv.Graph(((source, target),)).opts(edge_color=edge_color)
    # add some label, e.g. node lables
    if with_labels: 
    	pos = dict(zip(graph.nodes.columns()["index"], map(list, zip(graph.nodes.columns()["x"], graph.nodes.columns()["y"]))))
    	label_text = graph.nodes.columns()["index"]
    	node_labels = hv.Labels({('x', 'y'): np.array(list(pos.values())), 'text': label_text}, ['x', 'y'], 'text')
    	graph = graph * node_labels
    return graph

# It works with some parameters like node color
h0 = pn.interact(hv_graph,edge_color=['green','black'], node_color=["blue", "white"])
h0.save("works.html", embed=True)

# But it doesn't work with others, like edge labels
h = pn.interact(hv_graph_2,edge_color=['green','black'], with_labels=['True','False'])
h.save('does_not_work.html', embed=True)

Hi @optmat

The issue seems to be the with_labels=['True', 'False']. If you change it to with_labels=True it works.

import panel as pn
import numpy as np
import holoviews as hv
pn.extension()

def hv_graph(edge_color, with_labels):
	# preparations for the graph
    node_indices = np.arange(2, dtype=np.int32)
    source = np.zeros(2, dtype=np.int32)
    target = node_indices
    # modify the edge color only
    graph = hv.Graph(((source, target),)).opts(edge_color=edge_color)
    # add some label, e.g. node lables
    if with_labels: 
    	pos = dict(zip(graph.nodes.columns()["index"], map(list, zip(graph.nodes.columns()["x"], graph.nodes.columns()["y"]))))
    	label_text = graph.nodes.columns()["index"]
    	node_labels = hv.Labels({('x', 'y'): np.array(list(pos.values())), 'text': label_text}, ['x', 'y'], 'text')
    	graph = graph * node_labels
    return graph

ihv_graph = pn.interact(hv_graph,edge_color=['green','black'], with_labels=True)

ihv_graph.servable()
1 Like

Thanks a lot :slight_smile:

1 Like