pyvis icon indicating copy to clipboard operation
pyvis copied to clipboard

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

Open tbloch1 opened this issue 3 years ago • 2 comments

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.

tbloch1 avatar May 03 '21 17:05 tbloch1

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! :)

felipemello1 avatar Jul 21 '22 20:07 felipemello1

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'))

IgnacioHeredia avatar Aug 26 '22 11:08 IgnacioHeredia