Switching between network graph and subgraphs using buttons

Hey,

I’ve got a figure and I’ve added 2 traces: a nodes trace and an edges trace. I have a function that when given a node returns the networkx subgraph view of nodes contained within its scope (as I have a scope parameter that is used to identify nesting of nodes within the graph).

I’ve visualized the network graph as a scatter plot with dictionaries defining the coloration, shape, and scale of the components.

Now I’m trying to figure out how to get my custom buttons I’ve created to select or update my traces or figure data, such that the plot changes to showcase either the entire graph or a given subgraph. I don’t mind dynamically constructing the data for each subgraph ahead of time if necessary. Any help is appreciated.

I’d copy some of my code over but I’m on an air gapped system (it’s a big hassle). Thank you.

Sorry, this seemed complicated to figure out on my own since I’m new at plotly but I eventually figured it out!

I added each trace to the figure using a unique name parameter and with the visibility set to False for everything except for the default full graph.

Then when I process the nodes, I create my buttons, and do an update_layout(). I set the “buttons” parameter to use my list of buttons, where each button dict contains label, method, and also an args of:

"args=[{"visible":[x.name == f"subgraph_{node_name}_nodes" or x.name == f"subgraph_{node_name}_edges" for x in fig.data]}]

The value of visible uses a list comprehension to return a list that uses the node names associated with each button to toggle the specific subgraph’s node and edge traces on and all other traces off. Cheers!