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

fixes #276 #277

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 14 additions & 18 deletions pyvis/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ def add_node(self, n_id, label=None, shape="dot", color='#97c2fc', **options):
:type x: num (optional)
:type y: num (optional)
"""
assert isinstance(n_id, str) or isinstance(n_id, int)
if not isinstance(n_id, (str, int)):
raise TypeError("Node IDs should be of type str or int")
if label:
node_label = label
else:
Expand Down Expand Up @@ -274,28 +275,23 @@ def add_nodes(self, nodes, **kwargs):
valid_args = ["size", "value", "title",
"x", "y", "label", "color", "shape"]
for k in kwargs:
assert k in valid_args, "invalid arg '" + k + "'"
if not k in valid_args:
raise ValueError("invalid arg '" + k + "'")

nd = defaultdict(dict)
for i in range(len(nodes)):
for i, node in enumerate(nodes):
for k, v in kwargs.items():
assert (
len(v) == len(nodes)
), "keyword arg %s [length %s] does not match" \
"[length %s] of nodes" % \
(
k, len(v), len(nodes)
)
nd[nodes[i]].update({k: v[i]})
if not len(v) == len(nodes):
raise ValueError(
f"keyword arg {k} [length {len(v)}] does not match "
f"[length {len(nodes)}] of nodes"
)
nd[node].update({k: v[i]})

for node in nodes:
# check if node is `number-like`
try:
node = int(node)
self.add_node(node, **nd[node])
except:
# or node could be string
assert isinstance(node, str)
if not isinstance(node, (str, int)):
raise TypeError("Node IDs should be of type str or int")
else:
self.add_node(node, **nd[node])

def num_nodes(self):
Expand Down