Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in pyvis I always get this error: "AttributeError: 'NoneType' object has no attribute 'render'"

I want to do a network visualisation using pyvis in the latest version and the python version 3.9.6:

from pyvis.network import Network
g = Network()
g.add_node(0)
g.add_node(1)
g.add_edge(0, 1)
g.show('test.html')

every time I execute g.show() i get this error:

Traceback (most recent call last):
  File "/Users/tom/Library/Mobile Documents/com~apple~CloudDocs/Projekte/Coding_/f1 standings/test2.py", line 3, in <module>
    g.show('nx.html')
  File "/Users/tom/Library/Python/3.9/lib/python/site-packages/pyvis/network.py", line 546, in show
    self.write_html(name, open_browser=False,notebook=True)
  File "/Users/tom/Library/Python/3.9/lib/python/site-packages/pyvis/network.py", line 515, in write_html
    self.html = self.generate_html(notebook=notebook)
  File "/Users/tom/Library/Python/3.9/lib/python/site-packages/pyvis/network.py", line 479, in generate_html
    self.html = template.render(height=height,
AttributeError: 'NoneType' object has no attribute 'render'

I tried updating pyvis, I changed all sorts of details in my code and I imported all of pyvis.network without any results.

like image 413
Tom Neuburg Avatar asked Sep 12 '25 15:09

Tom Neuburg


2 Answers

In 0.3.2 that gets pushed to pip, for some reason the Network .show() function have notebook=True as default, even though the Network() constructor has notebook=False as default. I changed my show function and specify notebook=False then it works properly again.

https://github.com/WestHealth/pyvis/blob/ccb7ce745ee4159ce45eac70b9848ab965fc0906/pyvis/network.py#L537

from pyvis.network import Network
g = Network()
g.add_node(0)
g.add_node(1)
g.add_edge(0, 1)
g.show('test.html', notebook=False)
like image 55
PenguinTD Avatar answered Sep 14 '25 05:09

PenguinTD


I was able to fix a similar issue, where I was running pyvis with networkx in a Jupyter Notebook.

The minimal code example

from pyvis import network as net
import networkx as nx

#%%
g=net.Network()
nxg = nx.complete_graph(5)
g.from_nx(nxg) 

#%%
g.show("example.html")

Error

AttributeError: 'NoneType' object has no attribute 'render'

Solution

When initializing the Network, I added notebook=True, this fixed the issue for me. The new code is:

from pyvis import network as net
import networkx as nx

#%%
g=net.Network(notebook=True)
nxg = nx.complete_graph(5)
g.from_nx(nxg)

#%%
g.show("example.html")

Hope this helps!

like image 43
HeinDuijf Avatar answered Sep 14 '25 05:09

HeinDuijf