'How do I let a user delete a node or edge in a PyVis network graph?

Our network graph is noisy and needs to be understood, broken up in to separate clusters, and generally cleaned up. How do people do that is my general question and my specific question regards simply deleting an object.

I'd like to design a mini-app to hand a PyVis network graph to a user and let them delete ( drop, remove ) edges or nodes somehow, preferably with a clean button or keyboard-shortcut to affect the currently selected objects.

I can't find in the PyVis documentation a command or example to drop/delete/remove a node or edge. I can't find any question let alone answer on StackOverflow tagged [pyvis] for how to do this. ( I can manage lists and queues etc in Python, this is about altering a visualized graph in real-time without having to secretly rebuild the whole thing.

Am I missing something obvious? Didn't anyone else ever want to do this?

But, I can't find any documentation on how to delete an existing node or edge from a visualized network. I'm looking at having to capture input, figure out the X, Y coordinates and current list of objects, back way up and remove what I want gone from the lists, and regenerating the whole display. Seriously?



Solution 1:[1]

That's right dropping nodes and edges doesn't exist in pyvis. I used Networkx for removing nodes and edges. This package is in my opinion better suited for playing around with the graph, although it contains minimal support for visualizations.

Pyvis has a from_nx function for importing it from Networkx. Since you want to do it in realtime however, I guess the problem is that there is an absence of this feature within pyvis. In a pyvis approach you could destroy the node object (part of network). If you want to do it in the interface by clicking I suggest combining it with networkx as it can help you check conditions and feed these node ids.

I hope that my answers helps you. I also posted some code below for the people interested in converting networkx to pyvis.

import networkx as nx
from pyvis.network import Network
foo = nx.DiGraph()
foo.add_nodes_from(list_with_node_ids)
foo.add_edge(child_node_id), parent_node_id)

foo.remove_nodes_from(list_with_nodes_to_remove)

bar = Network('500px', '500px'
Network.from_nx(bar, foo)
bar.show('bar.html')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Niels