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

Show edge weights #126

Merged
merged 2 commits into from
Feb 8, 2022
Merged
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
39 changes: 21 additions & 18 deletions pyvis/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,10 @@ def __init__(self,
self.template = None
self.conf = False
self.path = os.path.dirname(__file__) + "/templates/template.html"

if notebook:
self.prep_notebook()



def __str__(self):
"""
override print to show readable graph data
Expand Down Expand Up @@ -210,7 +209,8 @@ def add_node(self, n_id, label=None, shape="dot", **options):
else:
node_label = n_id
if n_id not in self.node_ids:
n = Node(n_id, shape, label=node_label, font_color=self.font_color, **options)
n = Node(n_id, shape, label=node_label,
font_color=self.font_color, **options)
self.nodes.append(n.options)
self.node_ids.append(n_id)
self.node_map[n_id] = n.options
Expand Down Expand Up @@ -239,7 +239,8 @@ def add_nodes(self, nodes, **kwargs):

:type nodes: list
"""
valid_args = ["size", "value", "title", "x", "y", "label", "color", "shape"]
valid_args = ["size", "value", "title",
"x", "y", "label", "color", "shape"]
for k in kwargs:
assert k in valid_args, "invalid arg '" + k + "'"

Expand All @@ -264,7 +265,7 @@ def add_nodes(self, nodes, **kwargs):
# or node could be string
assert isinstance(node, str)
self.add_node(node, **nd[node])

def num_nodes(self):
"""
Return number of nodes
Expand Down Expand Up @@ -354,7 +355,7 @@ def add_edge(self, source, to, **options):
if not edge_exists:
e = Edge(source, to, self.directed, **options)
self.edges.append(e.options)

def add_edges(self, edges):
"""
This method serves to add multiple edges between existing nodes
Expand Down Expand Up @@ -585,7 +586,7 @@ def neighbors(self, node):
return self.get_adj_list()[node]

def from_nx(self, nx_graph, node_size_transf=(lambda x: x), edge_weight_transf=(lambda x: x),
default_node_size=10, default_edge_weight=1):
default_node_size =10, default_edge_weight=1, show_edge_weights=True):
"""
This method takes an exisitng Networkx graph and translates
it to a PyVis graph format that can be accepted by the VisJs
Expand Down Expand Up @@ -614,23 +615,25 @@ def from_nx(self, nx_graph, node_size_transf=(lambda x: x), edge_weight_transf=(
>>> nt.show("nx.html")
"""
assert(isinstance(nx_graph, nx.Graph))
edges = nx_graph.edges(data=True)
nodes = nx_graph.nodes(data=True)
edges=nx_graph.edges(data = True)
nodes=nx_graph.nodes(data = True)

if len(edges) > 0:
for e in edges:
if 'size' not in nodes[e[0]].keys():
nodes[e[0]]['size'] = default_node_size
nodes[e[0]]['size'] = int(node_size_transf(nodes[e[0]]['size']))
nodes[e[0]]['size']=default_node_size
nodes[e[0]]['size']=int(node_size_transf(nodes[e[0]]['size']))
if 'size' not in nodes[e[1]].keys():
nodes[e[1]]['size'] = default_node_size
nodes[e[1]]['size'] = int(node_size_transf(nodes[e[1]]['size']))
nodes[e[1]]['size']=default_node_size
nodes[e[1]]['size']=int(node_size_transf(nodes[e[1]]['size']))
self.add_node(e[0], **nodes[e[0]])
self.add_node(e[1], **nodes[e[1]])

if 'weight' not in e[2].keys():
e[2]['weight'] = default_edge_weight
e[2]['weight'] = edge_weight_transf(e[2]['weight'])
e[2]['weight']=default_edge_weight
e[2]['weight']=edge_weight_transf(e[2]['weight'])
if show_edge_weights:
e[2]["label"] = e[2]["weight"]
self.add_edge(e[0], e[1], **e[2])

for node in nx.isolates(nx_graph):
Expand All @@ -649,9 +652,9 @@ def get_nodes(self):
def get_node(self, n_id):
"""
Lookup node by ID and return it.

:param n_id: The ID given to the node.

:returns: dict containing node properties
"""
return self.node_map[n_id]
Expand Down