WestHealth / pyvis

Python package for creating and visualizing interactive network graphs.
http://pyvis.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
975 stars 165 forks source link

HTML output not rendering when used with ipywidgets in Jupyter/Colab #93

Open tbloch1 opened 3 years ago

tbloch1 commented 3 years ago

When calling a function that creates a network through interact(), the network is not rendered/loaded.

MWE:

import ipywidgets as widgets
from ipywidgets import interact
import numpy as np
from IPython.core.display import HTML
import networkx as nx
from pyvis.network import Network

def pyvis_test(x):
    G = nx.Graph()
    G.add_nodes_from([1,2,3])
    G.add_edges_from([[1,2],[2,3],[3,1]])

    net = Network(notebook=True)
    net.from_nx(G)
    net.toggle_drag_nodes(False)

    net.prep_notebook()

    net.save_graph('graph.html')
    display(HTML('graph.html'))

widg = widgets.Dropdown(options = np.arange(10))

interact(pyvis_test, x=widg)

Output: image

Expected output (what I get from just calling pyvis_test(x=1)): image

Having not found many similar issues online, my only guess is that it has something to do with a bad interaction between how the widgets are created/displayed (which I think is HTML) and then displaying the network.

If I load the saved graph, then it loads fine. So it's constructing that fine, it's just an issue with displaying it.

felipemello1 commented 2 years ago

Hi, any update on this? I would like to put a slider to control the minimum value of edge weights to show. Thanks in advance! :)

IgnacioHeredia commented 2 years ago

Found a workaround: wrap the net.show() inside a display. Here's some demo code:

from ipywidgets import interact, Dropdown
from pyvis.network import Network

widg = widgets.Dropdown(options = range(10))

@interact(x=widg)
def pyvis_test(x):
    net = Network(notebook=True)
    net.add_nodes([1,2,3])
    net.add_edges([[1,2],[2,3],[3,1]])
#     net.show('graph.html')
    display(net.show('graph.html'))