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

Network not handling objects: assert isinstance(n_id, str) or isinstance(n_id, int) #282

Open
matriculus opened this issue May 2, 2024 · 1 comment

Comments

@matriculus
Copy link

I am building a networkx graph with objects based on custom class. The objects __str__ and __repr__ functions output only unique strings.
The error arises when I migrate networkx graph to pyvis.

import networkx as nx
from pyvis.network import Network

class Account:
      def __init__(self, id):
            self.id =id
      
      def __str__(self) -> str:
            return f"Account: {id}"
      
      def __repr__(self) -> str:
            return self.__str__()
      

g = nx.Graph()
g.add_node(Account(1))
g.add_node(Account(2))
g.add_node(Account(3))
g.add_node(Account(4))
net = Network(
      directed=True,
      select_menu=True,
      filter_menu=True,
)
net.show_buttons()
net.from_nx(g)
net.toggle_physics(True)
net.show("test.html")

this gives an error

********************************
    assert isinstance(n_id, str) or isinstance(n_id, int)
AssertionError

It feels like Network is artificially forced to take in only str or int as nodes while networkx can take objects.

@matriculus
Copy link
Author

add_node method could take the object and use the object's __str__ method to call and get id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant