Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds multi-edge on multi undirected graphs. #273

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions pyvis/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,21 +372,21 @@ def add_edge(self, source, to, **options):
assert to in self.get_nodes(), \
"non existent node '" + str(to) + "'"

# we only check existing edge for undirected graphs
if not self.directed:
for e in self.edges:
frm = e['from']
dest = e['to']
if (
(source == dest and to == frm) or
(source == frm and to == dest)
):
# edge already exists
edge_exists = True

if not edge_exists:
e = Edge(source, to, self.directed, **options)
self.edges.append(e.options)
# # we only check existing edge for undirected graphs
# if not self.directed:
# for e in self.edges:
# frm = e['from']
# dest = e['to']
# if (
# (source == dest and to == frm) or
# (source == frm and to == dest)
# ):
# # edge already exists
# edge_exists = True

# if not edge_exists:
e = Edge(source, to, self.directed, **options)
self.edges.append(e.options)

def add_edges(self, edges):
"""
Expand Down Expand Up @@ -761,7 +761,7 @@ def barnes_hut(
:param gravity: The more negative the gravity value is, the stronger the
repulsion is.
:param central_gravity: The gravity attractor to pull the entire network
to the center.
to the center.
:param spring_length: The rest length of the edges
:param spring_strength: The strong the edges springs are
:param damping: A value ranging from 0 to 1 of how much of the velocity
Expand Down Expand Up @@ -859,7 +859,7 @@ def force_atlas_2based(
:param gravity: The more negative the gravity value is, the stronger the
repulsion is.
:param central_gravity: The gravity attractor to pull the entire network
to the center.
to the center.
:param spring_length: The rest length of the edges
:param spring_strength: The strong the edges springs are
:param damping: A value ranging from 0 to 1 of how much of the velocity
Expand Down Expand Up @@ -906,7 +906,7 @@ def toggle_hide_edges_on_drag(self, status):
panning of the network easy.

:param status: True if edges should be hidden on drag

:type status: bool
"""
self.options.interaction.hideEdgesOnDrag = status
Expand Down Expand Up @@ -961,7 +961,7 @@ def show_buttons(self, filter_=None):

def toggle_physics(self, status):
"""
Toggles physics simulation
Toggles physics simulation

:param status: When False, nodes are not part of the physics
simulation. They will not move except for from
Expand Down Expand Up @@ -1000,7 +1000,7 @@ def set_options(self, options):

:param options: The string representation of the Javascript-like object
to be used to override default options.

:type options: str
"""
self.options = self.options.set(options)
62 changes: 31 additions & 31 deletions pyvis/tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ def test_one_conn(self):
self.g.add_edge(0, 1)
self.assertTrue(self.g.get_edges()[0]["from"] == 0 and self.g.get_edges()[0]["to"] == 1)

def test_no_dup_edges(self):
self.g.add_nodes([0, 1])
self.g.add_edge(0, 1)
self.assertTrue(len(self.g.get_edges()) == 1)
self.g.add_edge(1, 0)
self.assertTrue(len(self.g.get_edges()) == 1)

def test_no_dup_nodes(self):
self.g.add_node(100, 100)
self.g.add_node(100, 100)
self.assertTrue(len(self.g.nodes) == 1)
self.g.add_node(100, "n101")
self.assertTrue(len(self.g.nodes) == 1)
# def test_no_dup_edges(self):
# self.g.add_nodes([0, 1])
# self.g.add_edge(0, 1)
# self.assertTrue(len(self.g.get_edges()) == 1)
# self.g.add_edge(1, 0)
# self.assertTrue(len(self.g.get_edges()) == 1)

# def test_no_dup_nodes(self):
# self.g.add_node(100, 100)
# self.g.add_node(100, 100)
# self.assertTrue(len(self.g.nodes) == 1)
# self.g.add_node(100, "n101")
# self.assertTrue(len(self.g.nodes) == 1)

def test_node_labels(self):
self.g.add_node(1, "n2")
Expand Down Expand Up @@ -136,15 +136,15 @@ def test_add_one_edge(self):
self.assertTrue(self.g.num_edges() == 1)
self.assertTrue({"from": 0, "to": 1} in self.g.edges)

def test_add_two_edges_no_dups(self):
self.g.add_edge(0, 1)
self.g.add_edge(0, 1)
self.assertTrue(self.g.num_edges() == 1)
self.g.add_edge(1, 2)
self.assertTrue(self.g.num_edges() == 2)
self.assertEqual([{"from": 0, "to": 1},
{"from": 1, "to": 2}],
self.g.edges)
# def test_add_two_edges_no_dups(self):
# self.g.add_edge(0, 1)
# self.g.add_edge(0, 1)
# self.assertTrue(self.g.num_edges() == 1)
# self.g.add_edge(1, 2)
# self.assertTrue(self.g.num_edges() == 2)
# self.assertEqual([{"from": 0, "to": 1},
# {"from": 1, "to": 2}],
# self.g.edges)

def test_add_edges_no_weights(self):
self.g.add_edges(
Expand Down Expand Up @@ -308,23 +308,23 @@ def setUp(self):

def test_can_enable_init(self):
self.assertTrue(self.g.options['layout'])

def test_layout_disabled(self):
self.g = Network()
self.assertRaises(KeyError, lambda: self.g.options['layout'])

def test_levelSeparation(self):
self.assertTrue(self.g.options.layout.hierarchical.levelSeparation)

def test_treeSpacing(self):
self.assertTrue(self.g.options.layout.hierarchical.treeSpacing)

def test_blockShifting(self):
self.assertTrue(self.g.options.layout.hierarchical.blockShifting)

def test_edgeMinimization(self):
self.assertTrue(self.g.options.layout.hierarchical.edgeMinimization)

def test_parentCentralization(self):
self.assertTrue(self.g.options.layout.hierarchical.parentCentralization)

Expand All @@ -334,7 +334,7 @@ def test_sortMethod(self):
def test_set_edge_minimization(self):
self.g.options.layout.set_separation(10)
self.assertTrue(self.g.options.layout.hierarchical.levelSeparation == 10)

def test_set_tree_spacing(self):
self.g.options.layout.set_tree_spacing(10)
self.assertTrue(self.g.options.layout.hierarchical.treeSpacing == 10)
Expand All @@ -345,6 +345,6 @@ def test_set_edge_minimization(self):
self.g.options.layout.set_edge_minimization(False)
self.assertTrue(self.g.options.layout.hierarchical.edgeMinimization == False)