1

I am trying to use the pyvis library to show py network using the following code:

import numpy as np
import networkx as nx
from pyvis.network import Network

adjacency_matrix = [[0.5, 0.2, 0.3, 0], [0.1, 0.1, 0, 0.8], [0.3, 0, 0.4, 0.3], [0, 0.2, 0.2, 0.6]]
A = np.array(adjacency_matrix)
G = nx.from_numpy_matrix(A, create_using=nx.MultiDiGraph)
G2 = Network()
G2.from_nx(G)
G2.show('network_map.html')

I am not getting any error but this code is supposed to open a browser with the html file network_map.html to show the network, but there is no response from the program.

I am using Atom, with a virtual python env I created on my mac using a simple python3.8 -m venv ...

when I run the program in the terminal nothing happens but when I click on the created html file in my folder I can see the network.

can anyone help me with making the network automatically open in browser?

Anatoly
  • 20,799
  • 3
  • 28
  • 42
mnzpython
  • 19
  • 1
  • 4
  • Can you edit the title to say pyvis instead of pyviz so that this post is not confused with PyViz.org? – James A. Bednar Dec 04 '20 at 12:55
  • Do you see any error in your web browser console ? (pressing F12 to view console error). On my case, it was due to the fact that there were missing libraries just next to my html file and these libraries (lib) were required to load the graph. – Pierre S. Feb 28 '23 at 13:49

2 Answers2

0

Where is your HTML file? If its not in the same folder as your program, you'll have to use a direct path such as /Desktop/Your-folder/your-file

RPIDevelop
  • 11
  • 2
  • the html file is in the same folder, so it should work but it doesnt do anything even though there is no error – mnzpython Dec 04 '20 at 23:16
0

If you want to display it in the same cell, use the option notebook=True (See below):

import networkx as nx
from pyvis import network as net

adjacency_matrix = [[0.5, 0.2, 0.3, 0], [0.1, 0.1, 0, 0.8], [0.3, 0, 0.4, 0.3], [0, 0.2, 0.2, 0.6]]
A = np.array(adjacency_matrix)
G = nx.from_numpy_matrix(A, create_using=nx.MultiDiGraph)
G2 = net.Network(notebook=True)
G2.from_nx(G)
G2.show('network_map.html')

Also, be sure that the notebook file (.ipynb) is in the same folder as the working directory. You can use the OS module to change directory: os.chdir(path_to_where_jupyter_notebook_is)

Dharman
  • 30,962
  • 25
  • 85
  • 135
mlcanales
  • 111
  • 1
  • 6