我正在尝试使用python打开HTML文件-pyviz [英] I am trying to open an HTML file using python - pyviz

查看:67
本文介绍了我正在尝试使用python打开HTML文件-pyviz的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pyvis库通过以下代码显示py网络:

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

我没有收到任何错误,但是此代码应该打开带有html文件 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.

我正在使用Atom,并使用一个简单的 python3.8 -m venv ...

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

当我在终端中运行程序时,什么都没有发生,但当我单击文件夹中创建的html文件时,我可以看到网络.

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?

推荐答案

如果要将其显示在同一单元格中,请使用选项 notebook = True (请参见下文):

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

此外,请确保笔记本文件(.ipynb)与工作目录位于同一文件夹中.您可以使用OS模块来更改目录: os.chdir(path_to_where_jupyter_notebook_is)

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)

这篇关于我正在尝试使用python打开HTML文件-pyviz的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆