You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Pyvis与NetworkX生成HTML时遭遇AttributeError渲染错误

解决Pyvis渲染时的AttributeError: 'NoneType' object has no attribute 'render'问题

报错信息

Traceback (most recent call last):
  File ~\anaconda3\envs\needle\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)
  File c:\users\riskvm_user\documents\python scripts\needle\test1.py:36
    got_net.show("gameofthrones.html")
  File ~\anaconda3\envs\needle\Lib\site-packages\pyvis\network.py:546 in show
    self.write_html(name, open_browser=False,notebook=True)
  File ~\anaconda3\envs\needle\Lib\site-packages\pyvis\network.py:515 in write_html
    self.html = self.generate_html(notebook=notebook)
  File ~\anaconda3\envs\needle\Lib\site-packages\pyvis\network.py:479 in generate_html
    self.html = template.render(height=height,
AttributeError: 'NoneType' object has no attribute 'render'

已尝试操作

  • 重装Pyvis
  • 重启内核

问题代码

from pyvis.network import Network
import pandas as pd
got_net = Network(height="750px", width="100%", bgcolor="#222222", font_color="white")
# set the physics layout of the network
got_net.barnes_hut()
got_data = pd.read_csv("stormofswords.csv")
sources = got_data['Source']
targets = got_data['Target']
weights = got_data['Weight']
edge_data = zip(sources, targets, weights)
for e in edge_data:
                src = e[0]
                dst = e[1]
                w = e[2]
                got_net.add_node(src, src, title=src)
                got_net.add_node(dst, dst, title=dst)
                got_net.add_edge(src, dst, value=w)
neighbor_map = got_net.get_adj_list()
# add neighbor data to node hover data
for node in got_net.nodes:
                node["title"] += " Neighbors:<br>" + "<br>".join(neighbor_map[node["id"]])
                node["value"] = len(neighbor_map[node["id"]])
                
print(got_net.nodes)

got_net.show("gameofthrones.html")

解决方法

1. 关闭Notebook模式运行

报错中show方法默认传递了notebook=True,Spyder环境对该模式兼容性较差,修改代码最后一行:

got_net.show("gameofthrones.html", notebook=False)

或直接使用write_html替代:

got_net.write_html("gameofthrones.html", open_browser=True)

2. 检查Pyvis模板文件完整性

该错误本质是Pyvis找不到渲染用的HTML模板文件。前往Pyvis安装目录(如~\anaconda3\envs\needle\Lib\site-packages\pyvis\templates),确认存在network.html文件。若缺失:

  • 卸载Pyvis后执行pip install pyvis --no-cache-dir重新安装,避免缓存问题
  • 手动获取模板文件放入上述目录

3. 降级Pyvis版本

新版本可能存在环境兼容性问题,尝试降级到稳定版本:

pip uninstall pyvis -y
pip install pyvis==0.3.2

4. 手动指定模板路径

若模板文件存在但Pyvis无法识别,初始化Network时手动指定路径:

from pyvis.network import Network
import os

template_path = "你的模板文件路径/network.html"
got_net = Network(height="750px", width="100%", bgcolor="#222222", font_color="white", template=template_path)

内容的提问来源于stack exchange,提问作者Nick Christensen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:07:11