pyvis报错AttributeError: ‘NoneType‘ object has no attribute ‘render‘

使用 pyvis 结合 networkx 来读取 .graphml 文件,并利用 pyvis 的内置物理引擎(模拟类似 Gephi 的 ForceAtlas2 布局)进行交互式图谱展示。

代码如下:

from networkx import read_graphml
from pyvis.network import Network

# 1️⃣ 读取 GraphML 文件
G = read_graphml("./graph_chunk_entity_relation.graphml")

# 2️⃣ 创建 PyVis 网络对象
net = Network(
    notebook=False,          # 如果你在 Jupyter Notebook 中运行设为 True
    height="800px",
    width="100%",
    bgcolor="#222222",      # 深色背景更清晰
    font_color="white"
)

# 3️⃣ 将 NetworkX 图转换为 PyVis 图
net.from_nx(G)

# 4️⃣ 设置物理引擎参数,模拟 ForceAtlas2 风格
net.set_options("""
{
  "physics": {
    "enabled": true,
    "solver": "forceAtlas2Based",
    "options": {
      "barnesHut": {
        "gravitationalConstant": -5000,
        "centralGravity": 0.005,
        "springLength": 200,
        "springConstant": 0.05,
        "damping": 0.09
      },
      "maxVelocity": 146,
      "minVelocity": 0.75,
      "stabilization": {
        "enabled": true,
        "iterations": 1000
      }
    }
  }
}
""")

# 5️⃣ 生成 HTML 文件并打开浏览器查看
net.show("graph_visualization.html")

代码运行报错

Traceback (most recent call last):
  File "D:\project\python\nano-graphrag-main\examples\nano_graphrag_cache_ollama_TEST\view.py", line 45, in 
    net.show("graph_visualization.html")
  File "D:\conda_envs\ocr_graph\lib\site-packages\pyvis\network.py", line 546, in show
    self.write_html(name, open_browser=False,notebook=True)
  File "D:\conda_envs\ocr_graph\lib\site-packages\pyvis\network.py", line 515, in write_html
    self.html = self.generate_html(notebook=notebook)
  File "D:\conda_envs\ocr_graph\lib\site-packages\pyvis\network.py", line 479, in generate_html
    self.html = template.render(height=height,
AttributeError: 'NoneType' object has no attribute 'render'
graph_visualization.html

原因:
pyvis的0.3.2版本有问题。安装0.3.1版本即可。

pip install pyvis==0.3.1

你可能感兴趣的:(Python,python,开发语言)